Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What happens if I re-alloc and the new size is 0. Is this equivalent with a free?

Tags:

c

realloc

Given the following code:

int *a = NULL;
a = calloc(1, sizeof(*a));
printf("%d\n", a);
a = realloc(a, 0);

printf("%d\n", a);
return (0);

It returns:

4078904
0

Is this realloc equivalent to a free ?

NOTE: I am using MinGW under WindowsXP.

like image 925
Andrei Ciobanu Avatar asked Mar 30 '10 15:03

Andrei Ciobanu


People also ask

What happens when you malloc size 0?

The result of calling malloc(0) to allocate 0 bytes is implementation-defined. In this example, a dynamic array of integers is allocated to store size elements. However, if size is 0, the call to malloc(size) may return a reference to a block of memory of size 0 instead of a null pointer.

What happens if you realloc 0?

Return Value If size is 0, the realloc() function returns NULL. If there is not enough storage to expand the block to the given size, the original block is unchanged and the realloc() function returns NULL. The storage to which the return value points is aligned for storage of any type of object.

Is realloc of 0 bytes is same as free () of that allocated pointer?

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

Can you malloc size 0?

An implementation for which malloc(0) always returns NULL is expressly permitted by the POSIX standard: If the size of the space requested is 0, the behavior is implementation-defined: the value returned shall be either a null pointer or a unique pointer.


3 Answers

http://www.cplusplus.com/reference/clibrary/cstdlib/realloc/ says yes.

like image 23
munissor Avatar answered Sep 28 '22 06:09

munissor


It may or may not be equivalent to calling free on the pointer; the result is implementation-defined.

From the C99 standard (§7.20.3/1):

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.

That applies to all of the memory management functions, including realloc.

like image 166
James McNellis Avatar answered Sep 28 '22 05:09

James McNellis


Not necessarily.

It often does as with the link that munissor posted, but the Mac OS 10.5 man page says:

If size is zero and ptr is not NULL, a new, minimum sized object is allocated and the original object is freed.

What is a "minimum sized object"? Well, any allocator stores some information about the allocations, and that takes space which is often allotted in addition to the space reserved for the user. Presumably a "minimum sized object" is just one of these headers plus zero bytes of space reserved for the user.

I would guess that this provision is present to support implementations that existed at the time of standardization, and that those implementations are useful for debugging allocation behavior.


To address Jonathan's comments

Consider the difference between

for (int i=0; i<VERY_BIG_NUMBER; ++i){
  char *p = malloc(sizeof(char[10]));
  free(p);
}

and

for (int i=0; i<VERY_BIG_NUMBER; ++i){
  char *p = malloc(sizeof(char[10]));
  realloc(p,0);
}

With a sane implementation of malloc and free the first clip does not consume memory without bound. But if the realloc implementation returns those "minimum sized objects" it might.

Certainly this example is contrived and it relies on understanding what is meant by "minimum sized object", but I think that text allows it.

In short, if you mean free you should say free.

like image 35
dmckee --- ex-moderator kitten Avatar answered Sep 28 '22 06:09

dmckee --- ex-moderator kitten