3

realloc(void *ptr, size_t new_size) returns NULL in two cases:

  • If there is not enough memory, the old memory block is not freed and NULL is returned.
  • NULL is also returned if error has occurred.

How do I know what type of problem occurred?

If we are short of memory, I might page some memory to disk.

How do I know whether I should do free(ptr)? (maybe it was already freed by realloc).

Suvarna Pattayil
  • 5,136
  • 5
  • 32
  • 59
user206334
  • 850
  • 1
  • 8
  • 18
  • There's no other indication to memory flow in c during realloc method call back. You have to manage memory by deallocating of unused memory. And you missed one point, if your first argument `ptr` doesn't have null terminator which also lead to return NULL. – Mani Apr 12 '13 at 07:11
  • 2
    @mani ok. the only word I can think of is.. "Huh??" – WhozCraig Apr 12 '13 at 07:12
  • @Mani ptr is *void, realloc does not deal with strings per se – user206334 Apr 12 '13 at 07:21

6 Answers6

4

Quoting from here

RETURN VALUE

Upon successful completion with a size not equal to 0, realloc() returns a pointer 
to the (possibly moved) allocated space. If size is 0, either a null pointer or a 
unique pointer that can be successfully passed to free() is returned. If there is
not enough available memory, realloc() returns a null pointer  and sets errno to 
[ENOMEM].
Suvarna Pattayil
  • 5,136
  • 5
  • 32
  • 59
1

From http://linux.die.net/man/3/realloc

Notes:

The UNIX 98 standard requires malloc(), calloc(), and realloc() to set errno to ENOMEM upon failure. Glibc assumes that this is done (and the glibc versions of these routines do this); if you use a private malloc implementation that does not set errno, then certain library routines may fail without having a reason in errno.

Community
  • 1
  • 1
RedX
  • 14,749
  • 1
  • 53
  • 76
1

How do I know whether I should free(ptr)? (maybe it was already freed by realloc)

The only realistic reason* realloc might return NULL is when there's not enough memory available, and there's little you can do about it.

Also in case it fails realloc never frees your memory, so you always have to take care of that on your own.


*Or when called with size 0

cnicutar
  • 178,505
  • 25
  • 365
  • 392
  • +1 just for the footnote-answer-model =P (ok, its a solid answer regardless). – WhozCraig Apr 12 '13 at 07:13
  • @WhozCraig Thanks. Actually I think stackoverflow-flavored markdown supports true footnotes, I just don't know how to format them. – cnicutar Apr 12 '13 at 07:14
0

If realloc() fails (returns NULL), you are supposed to free() the pointer yourself. The only weird case to worry about is when the new size is 0.

Community
  • 1
  • 1
Alok Singhal
  • 93,253
  • 21
  • 125
  • 158
  • Why is that of concern? Your program will get either a null pointer or a pointer to a region of space that can store zero bytes. As long as it doesn't dereference either of those, it's fine... – autistic Apr 12 '13 at 07:26
0

If you're out of memory errno should be ENOMEM. Also, if realloc fails it will not free the memory you passed in the pointer to it.

Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
0

Mostly - It depends on the implementation of realloc.

In general, though, you won't have to free(ptr) because realloc never allocated it in the first place (hence the NULL being returned).

To determine if realloc just couldn't increase your heap size, just check the ERRNO number. Malloc/Calloc/Realloc set ERRNO to ENOMEM (Insufficient storage space is available to service the request)

psidhu
  • 143
  • 1
  • 1
  • 7