hi,
will someone point me the reasons why a call to malloc fails even if we have enough memory ?
This is a discussion on reasons why malloc fails? within the C Programming forums, part of the General Programming Boards category; hi, will someone point me the reasons why a call to malloc fails even if we have enough memory ?...
hi,
will someone point me the reasons why a call to malloc fails even if we have enough memory ?
Segfault from calloc
Or maybe you're using crusty old turbo-c which is limited to the 640K of original DOS memory, and has no idea about the other GB the rest of your machine knows about, or that any decent new compiler can access freely.
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
If at first you don't succeed, try writing your phone number on the exam paper.
I support http://www.ukip.org/ as the first necessary step to a free Europe.
There can be many reasons, but usually the pointer wasn't NULL before using malloc. Do a couple of checks like this:
Code:// example pointer int *p; // free dangling pointer if(p) free(p); // ensure pointer is NULL before using malloc if(!p) malloc... // check it again to make sure it worked // before doing anything with it if(!p) error...
Originally Posted by joed
I don't see why using a pointer that's already pointing to allocated memory to store the return value of a new malloc call would cause malloc to fail.
Granted it's not a good thing since you'll effectively lose track of your original memory and cause a memory leak if the OS your on isn't smart enough to recover properly.
It shouldn't cause malloc to fail however although it would be nice if it did.
Actually it would be pretty crappy if it did.The above code would cause a malloc failure by your definition.Code:char *foo = malloc( 100 );
Quzah.
Hope is the first step on the road to disappointment.