I’m using gcc4.4.4 and gdb on 64bit Linux centos 5.7, compiling to ansi C. I’m not sure why my code tests true for PDF == NULL below, and calls exit(2).
void func(...)
...
double *PDF;
...
PDF = malloc( sizeof(double) * 1 );
if (PDF == NULL) {
exit(2);
}
Using free -m, I observe before the program starts:
total used free shared buffers cached
Mem: 2001 1955 46 0 71 975
-/+ buffers/cache: 907 1094
Swap: 4008 688 3319
and when the program sits on the exit(2); line of code, free -m reads:
total used free shared buffers cached
Mem: 2001 1970 31 0 72 976
-/+ buffers/cache: 921 1080
Swap: 4008 688 3319
In both cases, there’s plenty of memory available in the cache row, free column (certainly enough for one byte).
What are other possible reasons PDF would become NULL? What coding bugs could cause this?
In case it matters, I’ve been using gdb a lot in the last week, exiting the program using “q” then “y” instead of letting it complete (figuring all malloc memory would be freed by the program terminating thus not needing to execute the free() code).
If you’ve written beyond the bounds of a buffer somewhere, you may have corrupted the heap, in which case all bets are off.
I suggest using e.g. Valgrind to check that you haven’t done something like this.