What happens when blocks are freed from the free () heap?

So, I allocated 256 blocks on the heap:

char* ptr1 = malloc(128); char* ptr2 = malloc(128); 

Now, after I free ptr2, which I believe is currently on top of the heap, the program break (the current location of the heap) is not reduced. However, if I do another malloc, the address returned by malloc is the same as the one that is freed.

So, I have the following questions:

When I free a block, why doesn't the program gap decrease? When I call for free, what exactly happens? How does it track freed memory, so the next time I declare malloc, the address will be the same?

+4
source share
5 answers

This is unspecified behavior. You cannot rely on any one answer unless you need only one specific combination of the platform / os / compiler / libc. You did not specify an OS, and the C standard does not describe or require any specific implementation. With C99 (I don't have the final version of C11 yet):

7.20.3

The storage order and adjacency allocated by successive calls to calloc, malloc, and realloc are undefined. The pointer is returned if the selection succeeds appropriately so that it can be assigned to a pointer to any type of object, and then such an object or an array of such objects is used to access the allocated space (until the space is explicitly freed). The lifetime of a selected object extends from distribution to release. Each such distribution should give a pointer to an object that is not associated with any other object. The pointer returns the start point (lowest byte address) of the allocated space. If space cannot be allocated, a null pointer is returned. If the size of the request space is zero, the behavior is determined by the implementation: either the null pointer is returned, or the behavior is similar to a size other than zero, except that the returned pointer should not be used to access the object.

+4
source

This manual GNU libc can help.

Here gist

Sometimes a free one can actually return memory to the operating system and make the process smaller. Usually all he can do is allow a later call malloc to reuse the space. In the meantime, space remains in your program as part of the free list used inside malloc.

+2
source

When I free a block, why does the program gap not decrease?

I believe that it is not decreasing, because this memory has already been transferred to the program.

When I call free() , what exactly happens?

This section of memory is marked as allocatable, and its previous contents may be overwritten.

Consider this example ...

 [allocatedStatus][sideOfAllocation][allocatedMemory] ^-- Returned pointer 

Given this, free() can then mark [allocatedStatus] to false, so future allocations on the heap may use this memory.

How does it track the memory of free() d so that next time it declares malloc() Address is the same?

I do not think so. He simply looked at some free memory and found that the previous block was marked as free.

0
source

I believe that it completely depends on the operating system, when you call free (), it can return this memory right away or not care and just mark this memory segment as a possible acquisition at a later date (probably the same). I know the memory (if significant) appears in the task manager immediately after free () on the windows.

Keep in mind that the memory we are talking about here is virtual . Thus, the operating system can tell you everything it wants, and most likely is not an accurate representation of the physical state of the machine.

Think about how you will manage memory allocation, if you were writing an OS, you probably wouldn't want to do anything hasty that could waste resources. We are talking about 128 bytes here, do you want to spend the processing time of important processing, processing it alone? This can be the cause of this behavior or not, at least believable. Do it in a loop and then free () in another loop or just allocate large chunks of memory, see what happens, experiment.

0
source

Here's a rough idea of ​​how memory allocators work:

You have a allocator that has a bunch of “boxes” (“free lists”) that are just linked by lists of free memory blocks. Each bit has a different block size associated with it (Ie: you can have a list for 8-byte blocks, 16-byte blocks, 32 byte blocks, etc.) Even arbitrary sizes such as 7 or 10 byte blocks) . When your program requests memory (usually via malloc ()), the allocator is sent to the smallest bit that will match your data and checks to see if there are any free memory blocks in it. If not, it will request some memory from the OS (usually called a page) and cut the block, which it will return to a bunch of smaller blocks to fill the bit. Then it returns one of these free blocks to your program.

When you call for free, the distributor takes this memory address and puts it back in the basket (it's a free list), and everyone is happy. :)

Memory is still in use, so you do not need to store paging memory, but it is free for your program.

0
source

All Articles