C function function free ()

Possible duplicate:
C-programming: how to find out how much for free?

Hi,

when i have the following code:

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

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

Thank you!

-

ok I found other questions asking the same thing, please close - sorry

+3
source share
2 answers

This information is usually contained in some implementation-driven memory area malloc. Information often precedes the actual memory passed to you malloc, but this is a detail of the implementation, and you cannot rely on anything here.

+5
source

, .

glibc, , , .

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);
}
+2

Source: https://habr.com/ru/post/1780211/


All Articles