0

I'm trying to understand how malloc-realloc and free works deeply in C. I've found this page and I was able to understand how a chunk is allocated, but I'm not entirely sure how the free function works, because in my test program it leaves some data in memory after free is called.

This is how the memory look before...

33 0 0 0 0 0 0 0 *q=0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 49 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0

...and after the free function:

33 0 0 0 0 0 0 0 *q=112 132 178 223 255 127 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 49 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0

I have understood that 33 is the chunk size stored in the chunk header, but I can't understand what the numbers after "q" means when the memory is freed.

Q is the address returned by the malloc.

Thanks!

11
  • 5
    When the memory is freed, the program is free to allocate that memory to something else. It has no obligation to waste CPU cycles clearing out the data. May 9, 2019 at 21:28
  • 2
    and after the free function – After free(q); you have no business looking at that memory through q. After free(q); the pointer q holds an invalid pointer value and dereferencing an invalid pointer value has undefined behavior.
    – Swordfish
    May 9, 2019 at 21:45
  • @ChristianGibbons: The question does not ask why free does not clear out data. It asks why free writes new data into the space. May 9, 2019 at 23:07
  • 1
    @Swordfish: The OP does have business looking at the memory. They are investigating the implementation of free. While the C standard does not define the behavior, it does not prohibit people from investigating the C implementation in any way they desire. The lack of definition by the C standard does not nullify other causes of behavior, such as the code that implements malloc and free, and it reasonable to investigate it in the same way it is reasonable for a person to learn how a machine works by disassembling and poking at it, even if that voids the manufacturer’s warranty. May 9, 2019 at 23:09
  • After you release the memory, you have given up all rights to it, and the implementation may do as it pleases with the space. It may legitimately make it so that an attempt to read or write to any of the previously allocated data generates a segmentation fault. (It usually doesn't, but it can.) It can even make it such that an attempt to copy the pointer triggers a fault. It can also use the freed space to hold pointers to other bits of memory, for example. To look at the freed data is 'undefined behaviour'. What you find is entirely up to the implementation of malloc() et al. May 9, 2019 at 23:09

1 Answer 1

2

The memory management software you are using uses the memory released by free for its own purposes. It needs data to organize the blocks of memory that are available for allocation, and using the memory released by free is a convenient way to do that.

How the memory is used, and whether it is used, is implementation-specific. One implementation may use the freed memory in one way, while another implementation uses it in another way, and a third implementation might not use the freed memory at all, at least for certain sizes of blocks. For example, blocks of a certain fixed size might be tracked by a bitmap maintained elsewhere, possibly using different bitmaps for different fixed sizes.

2
  • Alright, I was trying to understand how the memory manager organize the free blocks. I think that those numbers left when the memory is freed means something, but I can't find out what they rappresent.
    – Ghimbo21
    May 11, 2019 at 0:00
  • @Ghimbo21: You should not expect to figure out what the numbers mean by examining them. It might be possible, but it might be very difficult, depending on the techniques used. The best way to understand what the numbers mean would be to get the source code or documentation for the malloc family of routines used in your C implementation and read it. Failing that, you could try reverse engineering the code from the object modules in the library. Either of those methods requires considerable knowledge and experience. May 11, 2019 at 0:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.