Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does free() know how much memory to deallocate? [duplicate]

Tags:

c

malloc

Possible Duplicate:
C programming : How does free know how much to free?

When programming in C, I often usemalloc() to allocate memory and free() to release it:

MyObject* objArr= (MyObject*) malloc(sizeof(MyObject)*numberOfObjects);
/** Do stuff **/
free(objArr);

How does free() know how much memory to deallocate? Does malloc() create a table somewhere to remember pointers and how much memory each pointer pointed to?

If that is the case, will free() fail if I rename the pointer? e.g.:

MyObject* objArr= (MyObject*) malloc(sizeof(MyObject)*numberOfObjects);
MyObject* newPtr= objArr;
free(newPtr); /** Does this fail? **/

What will happen if I increment the pointer and then run free()? e.g.:

MyObject* objArr= (MyObject*) malloc(sizeof(MyObject)*numberOfObjects);
newPtr++;
free(newPtr); /** What happens now? **/

Will it deallocate an additional chunk of memory just off the end of the original array?

like image 797
AndyL Avatar asked Dec 26 '09 16:12

AndyL


People also ask

How does free know how much memory to deallocate?

When we use the dynamic memory allocation techniques for memory allocations, then this is done in the actual heap section. It creates one word larger than the requested size. This extra word is used to store the size. This size is used by free() when it wants to clear the memory space.

What exactly happens as we free up dynamically allocated memory using free ()?

Yes, when you use a free(px); call, it frees the memory that was malloc'd earlier and pointed to by px. The pointer itself, however, will continue to exist and will still have the same address. It will not automatically be changed to NULL or anything else.

What happens if we forget to reclaim our memory with the free function?

In these cases, even small chunks of storage add up and create a problem. Thus our usable space decreases. This is also called “memory leak”. It may also happen that our system goes out of memory if the de-allocation of memory does not take place at the right time.

What happens when you free memory in C?

The free function deallocates the block of memory pointed at by ptr . Occasionally, free can actually return memory to the operating system and make the process smaller. Usually, all it can do is allow a later call to malloc to reuse the space.


1 Answers

The most common way is that it stores some information immediately before the address it returns to you. So if malloc returns the address 0x1004, internally, malloc will have put aside the memory started at 0x0FFC and will store various information such as the size somewhere in the memory between 0xFFC - 0x1003 but the application will be told the allocation starts at 0x1004.

The only thing that matters to free is getting the exact same address as what malloc returned.

like image 144
R Samuel Klatchko Avatar answered Sep 24 '22 20:09

R Samuel Klatchko