Freeing allocated memory: realloc() vs. free()
Asked Answered
D

7

16

so I have a piece of memory allocated with malloc() and changed later with realloc().

At some point in my code I want to empty it, by this I mean essentially give it memory of 0. Something which would intuitively be done with realloc(pointer,0). I have read on here that this is implementation defined and should not be used.

Should I instead use free(), and then do another malloc()?

Debose answered 10/2, 2014 at 9:10 Comment(1)
What do you mean by "empty it"?Built
S
17

It depends on what you mean: if you want to empty the memory used, but still have access to that memory, then you use memset(pointer, 0, mem_size);, to re-initialize the said memory to zeroes.
If you no longer need that memory, then you simply call free(pointer);, which'll free the memory, so it can be used elsewhere.

Using realloc(pointer, 0) may work like free on your system, but this is not standard behaviour. realloc(ptr, 0) is not specified by the C99 or C11 standards to be the equivalent of free(ptr).

realloc(pointer, 0) is not equivalent to free(pointer).

The standard (C99, §7.22.3.5):

The realloc function
Synopsis
1
#include <stdlib.h>
void *realloc(void *ptr, size_t size);

Description
2 The realloc function deallocates the old object pointed to by ptr and returns a
pointer to a new object that has the size specified by size. The contents of the new
object shall be the same as that of the old object prior to deallocation, up to the lesser of
the new and old sizes. Any bytes in the new object beyond the size of the old object have
indeterminate values.
3 If ptr is a null pointer, the realloc function behaves like the malloc function for the
specified size. Otherwise, if ptr does not match a pointer earlier returned by a memory
management function, or if the space has been deallocated by a call to the free or
realloc function, the behavior is undefined. If memory for the new object cannot be
allocated, the old object is not deallocated and its value is unchanged.
Returns
4
The realloc function returns a pointer to the new object (which may have the same
value as a pointer to the old object), or a null pointer if the new object could not be
allocated.

As you can see, it doesn't specify a special case for realloc calls where the size is 0. Instead, it only states that a NULL pointer is returned on failure to allocate memory, and a pointer in all other cases. A pointer that points to 0 bytes would, then, be a viable option.

To quote a related question:

More intuitively, realloc is "conceptually equivalent" to to malloc+memcpy+free on the other pointer, and malloc-ing a 0-byte chunk of memory returns either NULL either a unique pointer, not to be used for storing anything (you asked for 0 bytes), but still to be freeed. So, no, don't use realloc like that, it may work on some implementations (namely, Linux) but it's certainly not guaranteed.

As another answer on that linked question states, the behaviour of realloc(ptr, 0) is explicitly defined as implementation defined according to the current C11 standard:

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

Swordfish answered 10/2, 2014 at 9:19 Comment(5)
thx this is what I wanted, I want to be able realloc again at a later timeDebose
@user3021085: You chould free as much memory as possible, while still being certain of keeping the pointer by using realloc(pointer, 1), that'd free up close to all the memory, and you'd still have a pointer at the readySwordfish
yeah but I have some information I check. if I allocate to 1 I might get undefined behavierDebose
@user3021085: Then simply use realloc(pointer, sizeof *pointer)... I can't see how that'd result in undefined behaviour...Swordfish
It was the case in the C89/C90 standard. Freeing it with realloc(pointer,0) was mandatory back then. I would argue it is mandatory to not free it in the newer C and POSIX standards.Installation
F
5

realloc() is used to increase or decrease the memory and not to free the memory.

Check this, and use free() to release the memory (link).

Forerunner answered 10/2, 2014 at 9:15 Comment(0)
Y
4

I don't think you mean "empty"; that would mean "set it to some particular value that I consider to be empty" (often all bits zero). You mean free, or de-allocate.

The manual page says:

If ptr is NULL, then the call is equivalent to malloc(size), for all values of size; if size is equal to zero, and ptr is not NULL, then the call is equivalent to free(ptr).

Traditionally you could use realloc(ptr, 0); as a synonym for free(ptr);, just as you can use realloc(NULL, size); as a synonym for malloc(size);. I wouldn't recommend it though, it's a bit confusing and not the way people expect it to be used.

However, nowadays in modern C the definition has changed: now realloc(ptr, 0); will free the old memory, but it's not well-defined what will be done next: it's implementation-defined.

So: don't do this: use free() to de-allocate memory, and let realloc() be used only for changing the size to something non-zero.

Yonit answered 10/2, 2014 at 9:18 Comment(3)
This way to use realloc() seems to be outdated. Please have a look at my answer.Partizan
@Partizan Yeah, it seems to have become less well-defined. I edited, thanks.Yonit
"now realloc(ptr, 0); will free the old memory, but it's not well-defined what will be done next: it's implementation-defined." -- I'm not sure even that's right. If realloc returns NULL, then that indicates an allocation failure, and on an allocation failure, the old memory is not freed.Lomasi
P
4

Use free() to free, to release dynamically allocated memory.

Although former documentations state that realloc(p, 0) is equivalent to free(p), the lastest POSIX documentation explictly states that this is not the case:

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.

And more over:

Applications should assume that if realloc() returns a null pointer, the space pointed to by p has not been freed.

Partizan answered 10/2, 2014 at 9:23 Comment(0)
G
2

Use free(pointer); pointer = 0 instead of realloc(pointer, 0).

Gneiss answered 10/2, 2014 at 9:16 Comment(0)
R
2
void* realloc (void* ptr, size_t size);

In C90 :

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.

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.

Reactivate answered 10/2, 2014 at 9:19 Comment(0)
E
1

I would use realloc to give a pointer more or less memory, but not to empty it. To empty the pointer I would use free.

Evasive answered 10/2, 2014 at 9:12 Comment(1)
"to empty" a pointer isn't a common expression (at least not in the context of C Programming). More over it is misleading, and does not suite to what is happening when free()ing the memory a pointer references, points to.Partizan

© 2022 - 2024 — McMap. All rights reserved.