简体   繁体   中英

Malloc, Realloc, Free

int *p=malloc(20);

Now Heap will allocate memory of 20 bytes. And returns the address of 1st byte to pointer p.(Assuming no NULL pointer is returned).

Now I do this,

int *q=realloc(p, 40);

Now their are following possibilities:

1]. q=p

2]. q!=p

3]. q=NULL

Forgot about Possibility 2 and 3.

Now I write:

free(p);

Now What will happen?

Will First 20 bytes will become free and rest will still remain allocated or all the 40 bytes will get free or something else?

The call to free will cause undefined behavior. Here is the reasoning:

The function realloc will deallocate 1 the space pointer to by pointer p.

The lifetime 2 of an object, p pointed to, ends at the deallocation.

The function free receives a pointer to deallocated space and causes undefined behavior 3 .

Additionally, the value of the pointer p after the realloc call is indeterminate and its usage may cause undefined behavior due to trap representations.

In other words, even if the pointer returned from realloc points to the start of the same space as pointer p did, the object allocated by realloc counts as a new object with new lifetime, and may not be deallocated using the pointer p.


1 (Quoted from: ISO/IEC 9899:201x 7.22.3.5 The realloc function 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.

2 (Quoted from: ISO/IEC 9899:201x 7.22.3 Memory management functions 1)
The lifetime of an allocated object extends from the allocation until the deallocation

3 (Quoted from: ISO/IEC 9899:201x 7.22.3.3 The free function 2)
Otherwise, if the argument does not match a pointer earlier returned by a memory management function, or if the space has been deallocated by a call to free or realloc, the behavior is undefined.

4 (Quoted from: ISO/IEC 9899:201x 6.2.4 Storage duration of objects 2)
The value of a pointer becomes indeterminate when the object it points to (or just past) reaches the end of its lifetime.

From malloc / realloc man page

The realloc() function tries to change the size of the allocation pointed to by ptr to size, and returns ptr. If there is not enough room to enlarge the memory allocation pointed to by ptr, realloc() creates a new allocation, copies as much of the old data pointed to by ptr as will fit to the new allocation, frees the old allocation, and returns a pointer to the allocated memory.

You should take a look about how malloc() , free() and realloc() works. The easiest way is with a simple implementation.

http://arjunsreedharan.org/post/148675821737/write-a-simple-memory-allocator

realloc(p,new_size) resize the memory block pointed to by p that was previously allocated with a call to malloc or calloc. now if memory is available adjacent to the previously allocated memory the it returns same pointer that we pass to it as here p so in this case if you will free(q) the space to which both p & q are pointing will be deleted.

but if memory is not available adjacent to the previously allocated memory then it returns different pointer for newly allocated space so in this case if you will free(q) newly allocated space will be deleted

in both cases memory deleted is 40 bits

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM