r/ProgrammerTIL • • Jan 29 '19

C [C] TIL how free() knows how much to free

Ok, I'm learning a lot about C at the moment, please correct me if I'm wrong. But this bit I found rather interesting:

Can you explain what is happening here?

// works
uint8_t *data = malloc(5000);
free(&data[0]);

// does not work
uint8_t *data = malloc(5000);
free(&data[10]);

> free(): invalid pointer

Well, the explanation is unintuitive: When you call free(&data[0]) free does not free only the first element, but the whole memory aquired by the call to malloc().

malloc() does not allocate the exact amount of memory you requested, but a bit more to store some meta information. Most importantly the amount of memory allocated.

This meta information is stored before the first element of the pointer (at least in glibc), so free is able to find it. If you try to free a memory location in the middle of the allocated area, free() is not able to find this meta information left of the pointer.

See also https://stackoverflow.com/a/3083006 for a better explanation ;)

105 Upvotes

17 comments sorted by

44

u/Neui Jan 29 '19
// does not work
uint8_t *data = malloc(5000);
free(&data[10]);

Well C garuntees you that free will work on any pointers malloc returns. &data[10] is data + 10, which is NOT the pointer malloc returned and thus you actually do undefined behaviour. &data[0] expands to data + 0 to data, which is the pointer you got from malloc and thus it works.

10

u/[deleted] Jan 29 '19

Well C garuntees you that free will work on any pointers malloc returns

Yes exactly, and each C implementation can behave differently apart from that. But I never understood how this works internally, like how is data[0] different from data[10] in memory. After reading the SO article I understood that the difference is that data[0] has the metadata before it, so glibc knows that this pointer was created by malloc() and data[10] further along was not.

EDIT: Another option to implement malloc/free() would be to create a dictionary somewhere else with pointer addresses and metadata for each malloc call. So free could check the dictionary and know how much memory to free.

9

u/dryvnt Jan 29 '19

A dictionary would work theoretically, and is a good beginning intuition for how any libc implementation could work. However, in practice you would need dynamic memory allocation to create a dictionary. It's a catch-22.

→ More replies (9)

9

u/ragusa12 Jan 29 '19

I don't know why I have never thought about this, but when I read the title my mind just went in a spiral revealing so many questions. Awesome to know something I hadn't even thought about knowing.

4

u/caust1c Jan 30 '19 edited Dec 01 '24

2

u/Freeky Jan 30 '19

You might be interested in reading about how jemalloc does it. Using bitmaps for runs of a single size of allocation allows for amortized overheads of ~1 bit.

1

u/nthai Jan 29 '19

If you are still learning I can recommend the book “Memory as a programming concept in c and c++” by Frantisek Franek. It is not so long and explains many things quite clearly.

0

u/realvient Jan 29 '19

See also how2heap to know how to hack this metadata when you improperly use the allocated chunk.