Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How is dynamically allocated memory kept track in C [duplicate]

Tags:

c

memory

we allocate memory dynamically in C using malloc() and we receive a pointer to a location in the heap. now we use free() to deallocate the memory, passing the same pointer value as its argumnet.

the Question now is how does free() know how much to deallocate.. considering the fact that we can always resize the memory block allocated by malloc().

is there anything related to Hash Tables here?

like image 523
ArunMKumar Avatar asked May 03 '13 04:05

ArunMKumar


People also ask

How does dynamic memory allocation work in C?

In C, dynamic memory is allocated from the heap using some standard library functions. The two key dynamic memory functions are malloc() and free(). The malloc() function takes a single parameter, which is the size of the requested memory area in bytes. It returns a pointer to the allocated memory.

What exactly happens when you allocate memory dynamically?

Dynamic memory allocation is the process of assigning the memory space during the execution time or the run time. Reasons and Advantage of allocating memory dynamically: When we do not know how much amount of memory would be needed for the program beforehand.

Where does dynamic memory allocation take place in C?

This is known as dynamic memory allocation in C programming. To allocate memory dynamically, library functions are malloc() , calloc() , realloc() and free() are used. These functions are defined in the <stdlib. h> header file.

When a variable is dynamically allocated where it get stored?

Dynamically allocated variables live in a piece of memory known as the heap, these are requested by the running program using the keyword "new". A dynamic variable can be a single variable or an array of values, each one is kept track of using a pointer.


2 Answers

A typical implementation will store information just before the address returned by malloc. That information will include the information that realloc or free needs to know to do their work, but the details of what exactly is stored there depends on the implementation.

like image 69
Vaughn Cato Avatar answered Oct 10 '22 10:10

Vaughn Cato


The original technique was to allocate a slightly larger block and store the size at the beginning, a part the application didn't see. The extra space holds a size and possibly links to thread the free blocks together for reuse.

There are certain issues with those tricks, however, such as poor cache and memory management behavior. Using memory right in the block tends to page things in unnecessarily and also create dirty pages which complicate sharing and copy-on-write.

So a more advanced technique is to keep a separate directory. Exotic approaches have also been developed where areas of memory use the same power-of-two sizes.

In general, the answer is: a separate data structure is allocated to keep state.

like image 21
DigitalRoss Avatar answered Oct 10 '22 10:10

DigitalRoss