This is the mail archive of the libc-help@sourceware.org mailing list for the glibc project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: Realloc returns NULL


On Thu, Dec 18, 2008 at 5:11 PM, Jeff Baumes <jeff.baumes@kitware.com> wrote:
> Is there any reason that realloc() could return NULL without running
> out of memory on the system? My system memory is at 0.5GB out of 4GB
> when it crashes, so it doesn't appear to be an out-of-memory error.
> The requested realloc call is for 320K on a pointer that currently is
> allocated for 160K. I am running a multi-threaded application, where
> the failure occurs in a "worker" thread (not the main thread).
> Valgrind does not come up with any memory errors. If I replace the
> realloc with a malloc, memcpy, and free, the error does not appear. I
> am using libc version 2.7 on Ubuntu, gcc version 4.2.4.

Did you check the value of errno? It will be set to ENOMEM if you ran
out of memory.

"realloc"
http://www.opengroup.org/onlinepubs/009695399/functions/realloc.html

- returns null if the size is zero (defined behaviour).
- returns null if it is out of memory (defined behaviour).
- could return null if ptr was not the result of a previous malloc,
calloc, or realloc (undefined behaviour).
- could return null if ptr was already freed (undefined behaviour).

Cheers,
Carlos.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]