2

I have tried freeing memory without using free() as below

int *ptr = malloc(20);
realloc(ptr, 0);

Will it work?

amman059
  • 21
  • 1
  • It'd have been better if you gave reasons on why you don't want to use free... Interview question? –  Feb 16 '14 at 12:26
  • 1
    Please why is the question tagged `recursion` and `marco`? – alk Feb 16 '14 at 12:34

4 Answers4

12

C language standards differ on this one: in C90 passing zero size was the same as calling free:

If size is zero, the memory previously allocated at ptr is deallocated as if a call to free was made, and a null pointer is returned.

However, this changed in C99:

If size is zero, the return value depends on the particular library implementation: it may either be a null pointer or some other location that shall not be dereferenced.

Note that freeing is no longer a requirement; neither is returning a NULL when zero size is passed.

Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
  • 2
    +1 for catching the change in language, I didn't – Ed S. Feb 16 '14 at 12:11
  • 2
    +1. I think the summary here is: "if you want to free, then use `free()`, as you can't rely on `realloc(0)`". – Oliver Charlesworth Feb 16 '14 at 12:13
  • 1
    Neat. And it changes again in C11. – Kerrek SB Feb 16 '14 at 12:13
  • So conclusion is, it depends on C library implementations as per everyone's comments. – amman059 Feb 16 '14 at 12:18
  • @amman059 As Oli Charlesworth mentions above, the safest approach is to call `free` when you want the memory freed. Another consequence of this change is that unless you are sure on the version of the C language with which your code shall be compiled, you shouldn't call `realloc` with the size of zero. – Sergey Kalinichenko Feb 16 '14 at 12:30
  • @dasblinkenlight Agreed!! and thanks for your suggestions. – amman059 Feb 16 '14 at 16:06
  • @amman059 You are welcome! If you find the answer helpful, consider accepting it by clicking the grey check mark next to it. This would let other site visitors know that you are no longer actively looking for an improved solution, and earn you a badge on stack overflow. – Sergey Kalinichenko Feb 16 '14 at 16:23
2

realloc(ptr, 0) is not equivalent to free(ptr)

The standard C11 says:

If the size of the space requested is zero, the behavior is implementation-defined: either a null pointer is returned, or the behavior is as if the size were some nonzero value, except that the returned pointer shall not be used to access an object.

Why because realloc does below things,

  1. It creates new memory of size specified.
  2. Copies contents from old memory to newer.
  3. Frees the old memory
  4. Returns address of new memory
alk
  • 69,737
  • 10
  • 105
  • 255
Sunil Bojanapally
  • 12,528
  • 4
  • 33
  • 46
1

Maybe, but you can't count on it. From the docs:

If new_size is zero, the behavior is implementation defined (null pointer may be returned, or some non-null pointer may be returned that may not be used to access storage).

Ed S.
  • 122,712
  • 22
  • 185
  • 265
  • 1
    From manual pages : if size is equal to zero, and ptr is not NULL, then the call is equivalent to free(ptr) – delannoyk Feb 16 '14 at 12:08
  • @delannoyk: The linux man page is not the C standard. I did change what I had there originally though as I didn't know of that behavior beforehand. Learned something new. – Ed S. Feb 16 '14 at 12:10
0

At least for POSIX: realloc(p, 0) is not the same as free(p).

From the current POSIX documentation on realloc():

Previous versions explicitly permitted a call to realloc (p, 0) to free the space pointed to by p and return a null pointer. While this behavior could be interpreted as permitted by this version of the standard, the C language committee have indicated that this interpretation is incorrect.

alk
  • 69,737
  • 10
  • 105
  • 255