question
stringlengths
24
425
answer
stringlengths
5
405
WHen specifying library_dirs in a Python distutils.core.Extension I get this error when trying to build: ``` /Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/distutils/dist.py:263: UserWarning: Unknown distribution option: 'library_dirs' warnings.warn(msg) ``` Why is this? I am using Python 2.5 on Mac OS X.
The error means you're not passinglibrary_dirstodistutils.core.Extension, but to thedistutils.core.setupfunction.
I have a program that generates a variable amount of data that it has to store to use later. When should I choose to use mallod+realloc and when should I choose to use temporary files?
mmap(2,3p)(orfile mappings) means never having to choose between the two.
I had found a strange output when I write the following lines in very simple way: Code: ``` printf("LOL??!\n"); printf("LOL!!?\n"); ``` Output: It happens even the code is compiled under both MBCS and UNICODE. The output varies on the sequence of "?" and "!"... Any idea?
??!is atrigraphthat gets replaced by|. As a rule, you should never place two question mark characters together anywhere in a source file.
I have come across a few lines of coding I do not understand, and would be grateful for clarification: if(!(counter&7))ds->direction = ts->direction;
ifcounteris a multiple of 8set thedirectionelement of*dsequal to thedirectionelement of*ts
Is there a C library available for operations such as file operations, getting system information and the like which is generic, which can be used when compiled in different platforms and which behaves in a similar way? Edit: Something like Java or .NET platform abstracting the hardware.
Have you tried the standard library? It should be implemented on any system that has an ISO compliant C runtime.
I have a XML document which is received as a character stream. I wish to parse this using libxml2. Well one way would be to save it as an .xml and then open it using one of the libxml2 API's. Is there a way i can directly build a tree on this stream and parse it ? Env is purely c++/c. Cheers!
You can usexlmCtxtReadFdfrom parser.h. There's alsoxmlCtxtReadMemory, if you would rather use a block of memory than a stream.
I had found a strange output when I write the following lines in very simple way: Code: ``` printf("LOL??!\n"); printf("LOL!!?\n"); ``` Output: It happens even the code is compiled under both MBCS and UNICODE. The output varies on the sequence of "?" and "!"... Any idea?
??!is atrigraphthat gets replaced by|. As a rule, you should never place two question mark characters together anywhere in a source file.
I can attach a file with libesmtp? Does anyone have a sample code? Thanks FrankIT
No. The library only covers the SMTP protocol, the communication with the server. It does nothing to help you construct a valid MIME document.
What is the proper way to name my revisions? ``` v1.0.3.20 Alpha ``` or ``` Alpha v1.0.3.20 ```
There isn't really any "proper" way. While most people use the dot pattern "x.x.x", it isn't necessarily the proper way to do it. Likewise with your question, it is up to you. I would suppose that the former is more natural to say, and I would prefer it, but it doesn't really matter either way.
I was wondering how to, using C, find out what port a client is currently listening to. ie, I want to know what the source port is, not the destination port. Thanks in advance!
The socket address structure should be filled-in by the connect() system-call. Check it after a successful return from that call.
I can attach a file with libesmtp? Does anyone have a sample code? Thanks FrankIT
No. The library only covers the SMTP protocol, the communication with the server. It does nothing to help you construct a valid MIME document.
What is the proper way to name my revisions? ``` v1.0.3.20 Alpha ``` or ``` Alpha v1.0.3.20 ```
There isn't really any "proper" way. While most people use the dot pattern "x.x.x", it isn't necessarily the proper way to do it. Likewise with your question, it is up to you. I would suppose that the former is more natural to say, and I would prefer it, but it doesn't really matter either way.
I was wondering how to, using C, find out what port a client is currently listening to. ie, I want to know what the source port is, not the destination port. Thanks in advance!
The socket address structure should be filled-in by the connect() system-call. Check it after a successful return from that call.
Is there a way to find out if the machine is 32-bit or 64-bit by writing some code in C?
``` #include <limits.h> /* ... */ printf("This machine's pointers to void, in a program " "compiled with the same options as this one and " "with the same compiler, use %d bits\n", (int)sizeof (void*) * CHAR_BIT); ```
Is it possible to change a process parent?ex: parent A has Child B can I make the parent of B is the Init process without killing A?
Not from outside of process B. From inside process B, you can call fork which makes a copy of your process, then let the original exit. When that happens the new copy B2 will not be a child of A, its parent will be set to 1 (the init process).
I don't want to use system() in my C program, because system(3) blocks and this is not what I want. What is the optimal way to do it?
I think that a quick and dirty action is to call sytem(command &). the & will spawn the new process.
What is the fastest way to count lines and words in a text file in pure ANSI C? A word is terminated by a space or period. Line is terminated by'\n'. This seems to be in C++.
Read the file inIterate over charactersincrement character counterCheck if space/end of lineincrement word counterRepeat second and third steps untilEOF
Can I call another function in a thread runner function, called by apthread_create()? Are there any restrictions on such functions?
Yes, you can (and doing so is fairly frequent). The main restriction is that you need to synchronize threads when two or more access the same data (at least if there's any chance that any of them might modify that data).
I'm wondering what sort of algorithm could be used to take something like "4.72" into a float data type, equal to ``` float x = 4.72; ```
scanf,operator>>for istreams, andstrtofwould be the obvious choices. Thereisalsoatof, but, likeatoi, it lacks a way to tell you there was an error in the input, so it's generally best to avoid both.
I've wrote CGI script for www. This script expects two parameters through GET method, multiply these numbers and write result into the file. ``` mult.cgi?m=1&n=2 ``` But now I want to use this script from console too. I'tried something like ``` ./mult.cgi?m=1&n=2 ``` But it didnt work, how can I send parameters to script? thanks.
``` QUERY_STRING="m=2&n=4" ./mult.cgi ```
Can anyone give me an example of a Java program which has two native methods declared and defined in C and C++. Actually, I need a procedure as well as the code, so that I can run it and learn from it, thanks.
You can do it with JNI [Java Native Interface] Check this tutorial WIN env LinuxEnvironment :-) better one
I've wrote CGI script for www. This script expects two parameters through GET method, multiply these numbers and write result into the file. ``` mult.cgi?m=1&n=2 ``` But now I want to use this script from console too. I'tried something like ``` ./mult.cgi?m=1&n=2 ``` But it didnt work, how can I send parameters to script? thanks.
``` QUERY_STRING="m=2&n=4" ./mult.cgi ```
Can anyone give me an example of a Java program which has two native methods declared and defined in C and C++. Actually, I need a procedure as well as the code, so that I can run it and learn from it, thanks.
You can do it with JNI [Java Native Interface] Check this tutorial WIN env LinuxEnvironment :-) better one
I want to forward packets by netfilter, so I want to get some c demos to get start, thanks
Did you look at thenetfiltersite? The documentation seems a bit dated, but there's a "Linux netfilter Hacking HOWTO" there. Then play with thesource:)
What packages do i need to install in order to use a Makefile. I've already installed cmake, would i need ocamlmakefile? My problem is that whenever i type 'make' into the terminal, i'm told "make command not found".
You need GNU make. CMake and other make programs use other filenames generally.
Addresses of 1d arrays are actually taken as ``` a[i]=*(a+i); ``` Are the addresses of 2d arrays calculated as ``` a[i][j]=**(a+i+j); ```
The other answers are not quite correct. It's more like: ``` *(*(a+i)+j) ```
I've never worked with a language that didn't provide for some form of memory management, and thus managed to get by without ever really groking pointers. I can dabble inCI guess, as a result of coding inObjective-Cfor a little while.
Hmmm, maybe it's the single linked list. Try it:create, populate, reverse, releasea single linked list ``` struct node { int data; struct node *next; }; ```
I've never worked with a language that didn't provide for some form of memory management, and thus managed to get by without ever really groking pointers. I can dabble inCI guess, as a result of coding inObjective-Cfor a little while.
Hmmm, maybe it's the single linked list. Try it:create, populate, reverse, releasea single linked list ``` struct node { int data; struct node *next; }; ```
This question already has answers here:Closed13 years ago. Possible Duplicate:“C subset of C++” -> Where not ? examples ? I'm aware that C++ is not a strict superset of C. What language features prevent C++ from being a superset of C?
The elephant in the room: the following is valid C but not valid C++. ``` int typename = 1; ``` Substitute your favorite C++ reserved word.
``` size_t pixelsWidth = (size_t)bitmapSize.width; ``` Or is it totally fine to do without the casting to size_t? bitmapSize is of type CGSize...
You should use the proper type, which is probably CGFloat. size_t is something int'ish and inappropriate.
What libraries for C do I need to play a wav file on a Win32 system? And what is the code to use those libraries?
Use the Win32 API:PlaySound Include library:Winmm.libHeader include:Mmsystem.h(includeWindows.h)Example: ``` PlaySound(TEXT("recycle.wav"), NULL, SND_FILENAME); ```
e.g. one has a couple of arrays of ints or floats and a few integers to store. Is there a simplistic way to save them for later reloading without having to write a data format from scratch etc.?
netcdf (simpler interface)hdf5 (more powerful) they are not simple, however few hours investment is worth it
My professor and a couple of students are arguing about whetherargvis null terminated or not. My friend wrote a small program and it printed outnullbut another kid said that he is probably simply reading into blank memory. Can someone solve this discussion?
From the Standard: 5.1.2.2.1 Program startup...-- argv[argc] shall be a null pointer. So, yes; argv is null terminated
That is, why doesunsigned short var= L'ÿ'work, butunsigned short var[]= L"ÿ";does not?
L'ÿ'is of typewchar_t, which can be implicitly converted into anunsigned short.L"ÿ"is of typewchar_t[2], which cannot be implicitly converted intounsigned short[2].
For debugging reasons, how can one in a C program print some memory address? For instance, how should I do to print the content of address 0x611268 in form of a 4 byte float? I know I could use a debugger, but I mean, to print out in screen.
More correctly,printf("Value = %f\n", *((float*)0x611268)); Of course this assumes the address you've given is in the address space of the process running theprintf.
How can I generate a random boolean with a probability ofp(where 0 <= p <= 1.0) using the C standard libraryrand()function? i.e. ``` bool nextBool(double probability) { return ... } ```
``` bool nextBool(double probability) { return (rand() / (double)RAND_MAX) < probability; } ``` or (after seeing other responses) ``` bool nextBool(double probability) { return rand() < probability * ((double)RAND_MAX + 1.0); } ```
Is it posible to use the type of a prefiously declared function as a function pointer without using a typedef? function declaration: ``` int myfunc(float); ``` use the function declaration by some syntax as function pointer ``` myfunc* ptrWithSameTypeAsMyFunc = 0; ```
Not as per the 2003 standard. Yes, with the upcoming C++0x standard and MSVC 2010 and g++ 4.5: ``` decltype(myfunc)* ptrWithSameTypeAsMyFunc = 0; ```
I wish to profile CPU (sample if possible), with as small a performance impact as possible (hence similar to GCC's-pg), binaries compiled with Clang. Is there an alternative that uses instrumentation of the code, or produces output similar to gprof?
I received agood answeron theClang mailing list. To summarize, the use ofGoogle Performance Toolswas the best fit.
I have a loop as follows ``` while(1) { int i; } ``` Doesiget destroyed and recreated on the stack each time the loop occurs?
Theoretically, it gets recreated. In practice, it might be kept alive and reinitalized for optimization reasons. But from your point of view,it gets recreated, and the compiler handles the optimization (i.e, keep it at it's innermost scope, as long as it's apodtype).
I have an IP address stored inin_addr_tand I want to create the corresponding string representation of this data type (e.g.in_addr_tto10.0.0.1). How can I do that?
Useinet_ntop()- convert IPv4 and IPv6 addresses from binary to text form.
Suppose you have a string which is NOTnullterminated and you know its exact size, so how can you print that string withprintfin C? I recall such a method but I can not find out now...
There is a possibility with printf, it goes like this: ``` printf("%.*s", stringLength, pointerToString); ``` No need to copy anything, no need to modify the original string or buffer.
is there any nice GNU way how to measure average (worst case, best case) execution time of some command line program? I have image filter, unspecified amount of pictures, filtering them using for-loop in bash. So far I am usingtime, but I can't find a way how to get some statistics.
You can send the output of time to some file, and then "work" that file ``` echo "some info" >> timefile.txt time ( ./yourprog parm1 parm2 ) 2>> timefile.txt ```
While debugging a C program in Visual Studio 2008, is it possible somehow to get the size of variables (in bytes)? P.D. Of course I can print sizeof(var) for each of them
You can use theImmediate Windowto evaluatesizeof(...)or any other expression while debugging.
I am starting to learn to debug C programs with Visual Studio 2008. When running in debug mode, how is it possible to know the address of a initialized variable? I choose to watch it but can only know about the value. Thanks
You can type in a&in front of the variable, which will display it's address.
i m looking for a bayesian network library that work on the iphone. any tip ?
I've never used it, but there's one on Github called BayesianKit:https://github.com/lok/BayesianKit
Could not find a direct answer to this anywhere. Can someone shed some lights. Thanks.
There's no such command built into the language. Sockets need to be used but they are platform dependent.
I'm writing a program to read a file and display the number of lines and words in said file, simple stuff. What I want is to be able to run the program from terminal (running Ubuntu) by simply typing: count But I'm not sure how to get the filename into a variable in the C program. Little help please? Thanks in advance.
I think you are looking forargv.
iOS / Objective-C: I have a large array of boolean values. This is an inefficient way to store these values – at least eight bits are used for each element when only one is needed. How can I optimise?
see CFMutableBitVector/CFBitVector for a CFType option
i m looking for a bayesian network library that work on the iphone. any tip ?
I've never used it, but there's one on Github called BayesianKit:https://github.com/lok/BayesianKit
Could not find a direct answer to this anywhere. Can someone shed some lights. Thanks.
There's no such command built into the language. Sockets need to be used but they are platform dependent.
I'm writing a program to read a file and display the number of lines and words in said file, simple stuff. What I want is to be able to run the program from terminal (running Ubuntu) by simply typing: count But I'm not sure how to get the filename into a variable in the C program. Little help please? Thanks in advance.
I think you are looking forargv.
iOS / Objective-C: I have a large array of boolean values. This is an inefficient way to store these values – at least eight bits are used for each element when only one is needed. How can I optimise?
see CFMutableBitVector/CFBitVector for a CFType option
I am just doing a little work this morning making some static libraries. Why do static libraries end with '.a'? No one in my office knew, so I thought I would ask around on Stack Overflow. We are writing code in C++, C, and Objective-C.
I think the .a convention comes from using an "archiver" to place the object files into a static library.
How can i make that the console, with the output, will not disapear after the program ends in VS 2010 express C++? i write in c and not in c++, soo i need a function and include path to library. Thanks
You can simply poll for input. This performs a block so that the function only returns when the user gives more input - usually enter. If you're on Windows you can also use system("PAUSE").
How to Generate the random number from 0.5 to 1.0 .
You can try: ``` float RandomBetween(float smallNumber, float bigNumber) { float diff = bigNumber - smallNumber; return (((float) rand() / RAND_MAX) * diff) + smallNumber; } ```
I could see from MSDN documentations that a new windows service will be stored in the registryHKLM\System\CurrentControlSet\Services However the services registry key does not hold a value for the "service running status" Can anyone let me know where the service running status will be stored?
The service running status is not stored in the registry - it's a runtime property which you can query withControlService()service management function.
How can I make a copy of a tree data structure in memory to disk in C programming language?
You need to serialize it, i.e. figure out a way to go through it serially that includes all nodes. These are often calledtraversal methods. Then figure out a way to store the representation of each node, together with references to other nodes, so that it can all be loaded in again. One way of representing the references is implicitly, by nesting like XML does.
which visual studio project type should i choose that will best for c basic library(uni studies). the problem in visual studio 2010 express that its gives _tmain instead int main(). Thanks
I usually select "Win32 Console Application" and then "Empty project". Add a new source file and type in the main() by hand. I have no idea what R is talking about with his absurd comment.
Under Linux what would be the best way for a program to restart itself on a crash by catching the exception in a crashhandler (for example on a segfault)?
simplest is ``` while [ 1 ]; do ./program && break; done ``` basically, you run program until it is return 0, then you break.
I am working on a C project and can not figure out how to pass command line arguments to my main function in Visual Studio 2010 Express Edition. I want to debug - how do these command line arguments work?
Right click your project in Solution Explorer and select Properties from the menuGo to Configuration Properties -> DebuggingSet the Command Arguments in the property list.
In C, can I say a white space in a string has a NULL type, or something else?In C, which character represents the termination of a string?
Thenull character(ASCII value 0,'\0'as a character literal) terminates a string in C. This character isnotusually regarded as a whitespace character. The space in the middle of this string for example:"Hello world"is the character with ASCII value 32. This is not the same as NULL or\0.
which visual studio project type should i choose that will best for c basic library(uni studies). the problem in visual studio 2010 express that its gives _tmain instead int main(). Thanks
I usually select "Win32 Console Application" and then "Empty project". Add a new source file and type in the main() by hand. I have no idea what R is talking about with his absurd comment.
Under Linux what would be the best way for a program to restart itself on a crash by catching the exception in a crashhandler (for example on a segfault)?
simplest is ``` while [ 1 ]; do ./program && break; done ``` basically, you run program until it is return 0, then you break.
I am working on a C project and can not figure out how to pass command line arguments to my main function in Visual Studio 2010 Express Edition. I want to debug - how do these command line arguments work?
Right click your project in Solution Explorer and select Properties from the menuGo to Configuration Properties -> DebuggingSet the Command Arguments in the property list.
In C, can I say a white space in a string has a NULL type, or something else?In C, which character represents the termination of a string?
Thenull character(ASCII value 0,'\0'as a character literal) terminates a string in C. This character isnotusually regarded as a whitespace character. The space in the middle of this string for example:"Hello world"is the character with ASCII value 32. This is not the same as NULL or\0.
Dolfind/lsearchperform better than a typical looping solution that checks each item until it matches? Is there any special sauce/reason that these functions exist?
Probably they are not more efficient that a homebrew version, maybe even a bit less since the comparison function can't be inlined. But this is certainly not the point with them. They complete the API of the other search functions, in particularbsearchandtsearch.
Dolfind/lsearchperform better than a typical looping solution that checks each item until it matches? Is there any special sauce/reason that these functions exist?
Probably they are not more efficient that a homebrew version, maybe even a bit less since the comparison function can't be inlined. But this is certainly not the point with them. They complete the API of the other search functions, in particularbsearchandtsearch.
I would like to have a function written in C, but callable from C++ which takes a restricted pointer. This is only available in c99, so g++ doesn't like it, even inextern "C"blocks. How can I get around this limitation?
``` #ifdef __cplusplus # ifdef __GNUC__ # define restrict __restrict__ // G++ has restrict # else # define restrict // C++ in general doesn't # endif #endif ```
I read that pointers passed by malloc() & calloc() get allocated memory dynamically from the heap. ``` char *Name="Ann"; ``` In this case, is the static string {'A','n','n','\0'} also stored in the heap?Can I modify the string using the pointer?
No, the string is allocated statically. (C99, §6.4.5/5)Attempting to modify a string literal gives undefined behavior. (§6.4.5/6)
This question already has answers here:Closed13 years ago. Possible Duplicate:How do you set, clear and toggle a single bit in C? Can some one help me how to toggle a bit at ith position. One way is to do((n>>i) ^ 1) << i. Are there any other ways ?
n ^= 1U << iis easy enough, isn't it?
in a C program I need to define ``` float (*d_i)[3]; ``` but later I realized that I need to define NMAX variables of this type. I tried with ``` float (*d_i)[3][NMAX]; ``` but it does not work. what would be the right syntax? Thanks
Don't guess. Just use atypedef. ``` typedef float (*someType)[3]; someType d_i[NMAX]; ``` (In case you really don't want thetypedef, ``` float (*d_i[NMAX])[3]; ``` )
I have a C code written. When I compile it on Linux then in the header file it says the following error:storage class specified for parameter i32 , i8and so on ``` typedef int i32; typedef char i8; ```
Chances are you've forgotten a semicolon in a header file someplace. Make sure each line ends in;
in a C program I need to define ``` float (*d_i)[3]; ``` but later I realized that I need to define NMAX variables of this type. I tried with ``` float (*d_i)[3][NMAX]; ``` but it does not work. what would be the right syntax? Thanks
Don't guess. Just use atypedef. ``` typedef float (*someType)[3]; someType d_i[NMAX]; ``` (In case you really don't want thetypedef, ``` float (*d_i[NMAX])[3]; ``` )
I have a C code written. When I compile it on Linux then in the header file it says the following error:storage class specified for parameter i32 , i8and so on ``` typedef int i32; typedef char i8; ```
Chances are you've forgotten a semicolon in a header file someplace. Make sure each line ends in;
Is there such a thing as Javadoc-type documentation available for C/C++ libraries?
Yes,doxygenfor documenting your code. If you mean documentation of existing libraries : For the STL, check out thesgisite. For a general c/ c++ reference seehere. For a specific library, check its site.
I am writing a program in C for file compression. The method i am trying to use involves doing math on the file as if it were one long number. Can anyone recommend a bignum library that would not try to do all of that in ram, but rather let me do math with file pointers. any help would be appreciated, thanks in advance.
I doubt such a library exists. You could try mmap()ing the files in memory and see if you can do it that way.
Does a quine print the ACTUAL code of the program i.e not obfuscated or does it print the obfuscated program?
I don't think obfuscation has anything to do with it. Usually a quine prints the actual source code of the program itself.
This question already has answers here:Closed13 years ago. Possible Duplicate:How would you set a variable to the largest number possible in C? Can we find the maximum size of a data type in C langauge?
If you want to know the maximum and minimum values you can store in a variable of a given data type, you can check with these different constants: LONG_MIN, LONG_MAX, seehere, for the rest.
I need to kill such user processes that are taking longer time than a said expected interval on UNIX (Solaris) operating system. This needs to be done inside the process that is currently being executed. Please suggest how this can be achieved in C or in UNIX?
See thealarm()system call. It provides you with aSIGALRMsignal which your process can handle, and use to quit.
I'm looking for an R-Tree implementation, in C, Objective-c and even C++, which shall be efficient for searching the 2d rectangle in which a point falls ( memory efficiency would also be great, but I can sacrifice a bit more memory for time even while I am on an iPhone ). A good documentation will be appreciated too
Check outthis page, it provides implementations (in C, C++, Java, etc.) for several variants (R*, R+, etc.).
This question already has answers here:Closed13 years ago. Possible Duplicate:How would you set a variable to the largest number possible in C? Can we find the maximum size of a data type in C langauge?
If you want to know the maximum and minimum values you can store in a variable of a given data type, you can check with these different constants: LONG_MIN, LONG_MAX, seehere, for the rest.
I need to kill such user processes that are taking longer time than a said expected interval on UNIX (Solaris) operating system. This needs to be done inside the process that is currently being executed. Please suggest how this can be achieved in C or in UNIX?
See thealarm()system call. It provides you with aSIGALRMsignal which your process can handle, and use to quit.
I'm looking for an R-Tree implementation, in C, Objective-c and even C++, which shall be efficient for searching the 2d rectangle in which a point falls ( memory efficiency would also be great, but I can sacrifice a bit more memory for time even while I am on an iPhone ). A good documentation will be appreciated too
Check outthis page, it provides implementations (in C, C++, Java, etc.) for several variants (R*, R+, etc.).
I have written a source code with a time delay.In unix I have used the #include <unistd.h>header file andusleepfunction. What is the equivalent to this on windows? What library and function should I use if I write the same code on windows.
``` #include <windows.h> ``` and ``` ::Sleep( 1000 /* milliseconds */ ); ```
I see a lot of templates and complicated data structures for implementing a circular buffer. How do I code a simple integer circular buffer for 5 numbers? I'm thinking in C is the most straightforward? Thanks.
Have an array,buffer, of 5 integers. Have an indexindto the next element. When you add, do ``` buffer[ind] = value; ind = (ind + 1) % 5; ```
How to complete disable Drag'n Drop from a GtkEntry ?
I'd discovered that if you set the propertygtk-dnd-drag-thresholdto a value larger than the screen size, it will block the DnD for the whole application.
C parent program does some processing and allocates memory, then calls execvp(). What will happen with all the allocated but not freed memory? Is it automatically freed or stays as a garbage?
exec*()replaced the memory of the old process completely with the new program. This includes all allocated memory, so there is no garbage staying behind. But note that other resources like file descriptors are not automatically freed or closed.
i'm looking for a ready-made grammar and parser for php (at least 5.2), ideally an utility/library that can parse php code into a readable AST, e.g. xml. The parser itself doesn't have to be written in php, the source language doesn't matter much.
To answer my own question I've managed to compilephcon my OSX box, the parser part seems to work well ``` phc --dump-xml=ast foo.php > bar.xml ``` creates an xml representation of the AST.
Question is simple, i have a object file and i want to read the symbols of the object file via code. I am aware that the linux command "nm" would be able to do this, but i want to be able to do it inside code. Also note id like to do this either via C or Python. Regards Paul
It would be nice to provide more details. For some pointers: http://sourceforge.net/projects/python-elf/http://www.grant-olson.net/python/pyasm
``` #define int_p int* int_p p1,p2,p3; // only p1 is a pointer ! ``` can somebody exlplain why it is so.
#defineis just a textual substitution. The code above is equivalent to ``` int *p1, p2, p3; ``` so onlyp1is a pointer. You need ``` typedef int* int_p; ``` instead.
I need to do the following equation floor(e%100000) where e is a double. I know mod only accepts int values, how do I go about achieving this same result? Thanks
Use thefmod()function instead of%. It acceptsdoubleparameters, and returns adoubleresult.
I am looking for a way to make a program in C or C++ that detects if there was any files altered, renamed, moved or deleted in a specified directory for Linux systems. Is there a way to do that?
You wantinotify(and itsman page.)
Given ``` struct S { SomeType single_element_in_the_struct; }; ``` Is it always true that ``` sizeof(struct S) == sizeof(SomeType) ``` Or it may be implementation dependent?
This will usually be the case, but it's not guaranteed. Any struct may have unnamed padding bytes at the end of the struct, but these are usually used for alignment purposes, which isn't a concern if you only have a single element.
I thought I read about a C standard library function recently that was able to return a pointer to any extern variable whose name was passed to it as a const char *. I think that it works via linker symbols, if that helps.
You could be thinking ofdlsym, which is not part of the C standard library but part of the POSIX API.
Can someone please point me to some documentation on the virtual memory maps used for Linux and Windows. By that I mean what virtual addresses, code, writable static data, the stack and the heap (along with other kernel bits) will normally be placed in, in a typical process?
Since the advent of ASLR, it's mostly on random virtual addresses.
``` #include<stdio.h> void f(void) { int s = 0; s++; if(s == 10) return; f(); printf("%d ", s); } int main(void) { f(); } ``` what is the output of the programme!?? i m segmentation fault ...what is it?
Sincesis a local variable, each recursive call tof()gets its own copy of it. So every timeswill be 1 and the you get a stack overflow exception.
I have a test program which prompts for input from user(stdin), and depending on inputs, it asks for other inputs, which also need to be entered. is there a way I can have a script do all this work ?
There's a program calledexpectthat does pretty much exactly what you want -- you can script inputs and expected outputs and responses based on those outputs, as simple or complex as you need. See also thewikipedia entryfor expect
What's the equivalent to Windows'sVirtualAllocin OS X? That is, how can i reserve a contiguous address space without actually commiting it and then commit chunks of it later? Thanks, Alex
Themmap()function, called withMAP_ANON | MAP_PRIVATE, is very roughly equivalent toVirtualAlloc()with theMEM_RESERVEflag. Memory is then committed by touching each page in the mapping.
How do I use pow() and sqrt() function in Ubuntu? I have included the math.h header file but it still gives me an error.
Try adding-lmto your linker command. Most of the math functions live inlibmwhich needs to be explicitly linked in.
I want to find out for how long (approximately) some block of code executes. Something like this: ``` startStopwatch(); // do some calculations stopStopwatch(); printf("%lf", timeMesuredInSeconds); ``` How?
You can use theclockmethod intime.h Example: ``` clock_t start = clock(); /*Do something*/ clock_t end = clock(); float seconds = (float)(end - start) / CLOCKS_PER_SEC; ```