3

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

Hi,

when i have following code:

void *ptr = malloc(100); // alloc 100 bytes
// do sth
free(ptr);

how does the free() function know how much space has to be freed?

thank you!

--

ok i have found other questions asking the same, please close - sorry

Community
  • 1
  • 1
Weidling C
  • 31
  • 1
  • I think this kind of questions is asked so *many* times on SO already. So should have faq perhpas? – Nyan Dec 14 '10 at 14:36

2 Answers2

5

This information is usually contained in some memory area managed by the malloc implementation. The information often preceeds the actual memory handed out to you by malloc, but that's an implementation detail, and you cannot rely on anything here.

Dirk
  • 30,623
  • 8
  • 82
  • 102
  • 1
    There really isn't an *implementation* of malloc. Your operating system provides the implementation. – Luca Matteis Dec 14 '10 at 13:39
  • 1
    @Luca Matteis: Nope, it usually does not. It's part of the run-time library (libc on unix, crt on Windows, etc.) See for example Doug Lea's `malloc` implementation (http://g.oswego.edu/dl/html/malloc.html) and, of course, http://en.wikipedia.org/wiki/Malloc (which even mentions Doug Lea's implementation, so I am hopefully not that far off the rails... :-) – Dirk Dec 14 '10 at 13:41
  • 1
    @Luca Matteis: This is not true. `malloc` is definitely a CRT function. OS usually doesn't care about such a minor things as heap management. It allocates memory for the application in terms of memory **pages** (aka virtual memory). And this memory in turn is **partitioned** inside the application. – valdo Dec 14 '10 at 13:43
  • @Dirk, okay. I'll leave it to the community decide what is part of an OS and what is not. Sure there are implementations of malloc, but they're all vigorously tied with how the OS works, since that's where all the logic for calculating what space should be allocated happens. And since the spirit of the question was to realize and understand how `free()` would know how much space to free, it's inevitable to bring into the subject the OS and not just `malloc`'s implementation – Luca Matteis Dec 14 '10 at 13:46
  • @Luca Matteis: Of course, the `malloc` implementation has to know a lot of details about the system it is running on. Besides other things, it has to cater for alignment of data types etc. (`malloc`ed memory must be aligned "just right", so that you can stuff any primitive type at the address returned). – Dirk Dec 14 '10 at 13:52
  • @Luca, @Dirk: Actually, all `malloc()` *needs* to know is how to request more memory from the operating system. That's one system call. Of course, knowing additional details will help with performance. – JeremyP Dec 14 '10 at 14:13
  • @JemeryP: Of course it has to. It does so via `sbrk`, `mmap`, `int 21h`, `VirtualAllocEx` or what ever system call for allocating raw core the system's OS provides/requires. Note, though, that dealing with alignment is not necessarily "only" a performance issue, but on many systems required for proper functionality. – Dirk Dec 14 '10 at 14:38
2

It's implementation dependent but usually the underlying system mas a map of addresses to blocks and it knows the size from that memory map.

Here is the striped down code from glibc which shows it doing basically what what I just said.

void fREe(Void_t* mem)
{
  arena *ar_ptr;
  mchunkptr p;
  if (__free_hook != NULL) {
    (*__free_hook)(mem, NULL);
  }

  if (mem == 0)                              /* free(0) has no effect */
    return;

  p = mem2chunk(mem);

  if (chunk_is_mmapped(p))                       /* release mmapped memory. */
  {
    munmap_chunk(p);
    return;
  }

  ar_ptr = arena_for_ptr(p);
  chunk_free(ar_ptr, p);
  (void)mutex_unlock(&ar_ptr->mutex);
}
Andrew White
  • 52,720
  • 19
  • 113
  • 137