536

In C programming, you can pass any kind of pointer you like as an argument to free, how does it know the size of the allocated memory to free? Whenever I pass a pointer to some function, I have to also pass the size (ie an array of 10 elements needs to receive 10 as a parameter to know the size of the array), but I do not have to pass the size to the free function. Why not, and can I use this same technique in my own functions to save me from needing to cart around the extra variable of the array's length?

3

11 Answers 11

487

When you call malloc(), you specify the amount of memory to allocate. The amount of memory actually used is slightly more than this, and includes extra information that records (at least) how big the block is. You can't (reliably) access that other information - and nor should you :-).

When you call free(), it simply looks at the extra information to find out how big the block is.

21
  • 59
    FYI, for example BSD has malloc_size() to reliably access the block size from an malloc()ed pointer. But there's no reliable, portable way.
    – laalto
    Commented Oct 5, 2009 at 7:53
  • 74
    I think it's important to say that this extra information block is situated before the returned pointer. Commented Oct 5, 2009 at 8:28
  • 54
    @gs Well that's implementation-dependent. But, yes, that's where it usually is.
    – Falaina
    Commented Oct 5, 2009 at 11:20
  • 54
    Can you imagine the horror if free() required the programmer to accurately report how big the malloc() block was? The memory leaks are bad enough as it is. Commented Apr 7, 2010 at 13:45
  • 67
    Why is that information available to malloc() and free(), but you have to store the size of an array? Why wouldn't they make it possible to do something like blockSize(ptr) if they're storing the information anyway?
    – corsiKa
    Commented Jun 14, 2014 at 1:12
209

Most implementations of C memory allocation functions will store accounting information for each block, either in-line or separately.

One typical way (in-line) is to actually allocate both a header and the memory you asked for, padded out to some minimum size. So for example, if you asked for 20 bytes, the system may allocate a 48-byte block:

  • 16-byte header containing size, special marker, checksum, pointers to next/previous block and so on.
  • 32 bytes data area (your 20 bytes padded out to a multiple of 16).

The address then given to you is the address of the data area. Then, when you free the block, free will simply take the address you give it and, assuming you haven't stuffed up that address or the memory around it, check the accounting information immediately before it. Graphically, that would be along the lines of:

 ____ The allocated block ____
/                             \
+--------+--------------------+
| Header | Your data area ... |
+--------+--------------------+
          ^
          |
          +-- The address you are given

Keep in mind the size of the header and the padding are totally implementation defined (actually, the entire thing is implementation-defined (a) but the in-line accounting option is a common one).

The checksums and special markers that exist in the accounting information are often the cause of errors like "Memory arena corrupted" or "Double free" if you overwrite them or free them twice.

The padding (to make allocation more efficient) is why you can sometimes write a little bit beyond the end of your requested space without causing problems (still, don't do that, it's undefined behaviour and, just because it works sometimes, doesn't mean it's okay to do it).


(a) I've written implementations of malloc in embedded systems where you got 128 bytes no matter what you asked for (that was the size of the largest structure in the system), assuming you asked for 128 bytes or less (requests for more would be met with a NULL return value). A very simple bit-mask (i.e., not in-line) was used to decide whether a 128-byte chunk was allocated or not.

Others I've developed had different pools for 16-byte chunks, 64-bytes chunks, 256-byte chunks and 1K chunks, again using a bit-mask to decide what blocks were used or available.

Both these options managed to reduce the overhead of the accounting information and to increase the speed of malloc and free (no need to coalesce adjacent blocks when freeing), particularly important in the environment we were working in.

7
  • 2
    @user10678, the only real requirement of malloc is that it give you, for the successful case, a block of memory at least as large as what you asked for. Individual blocks are contiguous in terms of how you access elements within them, but there's no requirement that the arenas the blocks come from are contiguous.
    – paxdiablo
    Commented Nov 5, 2017 at 8:29
  • 1
    Related question: Why is there no variation of malloc/free, where you specify the size when freeing and so it doesn't have to store the size? Commented Dec 4, 2019 at 14:57
  • 3
    @user253751, because then theer's one more thing you need to keep track of, over and above the pointer itself. It's both unnecessary and dangerous: void *x = malloc(200); free(x, 500); is not going to end well :-) In any case, for efficiency, the actual size of the buffer may be larger (you just can't rely on this).
    – paxdiablo
    Commented Dec 5, 2019 at 2:30
  • 1
    @paxdiablo It also avoids wasting memory to hold the size. Commented Dec 5, 2019 at 10:15
  • 1
    Much better to let the system remember the size than relying on the developer. It would be impossible to check that the right size is passed to free().
    – gnasher729
    Commented Jul 8, 2023 at 6:59
53

From the comp.lang.c FAQ list: How does free know how many bytes to free?

The malloc/free implementation remembers the size of each block as it is allocated, so it is not necessary to remind it of the size when freeing. (Typically, the size is stored adjacent to the allocated block, which is why things usually break badly if the bounds of the allocated block are even slightly overstepped)

2
  • 9
    This is a non answer. The question is exactly this: why can free reliably look up the size of the block, but yet there is no function available to the programmer that does that?
    – Bananach
    Commented Jan 4, 2019 at 11:54
  • 1
    This is indeed an implementation detail for the malloc api and there is no api to get this info back in a standard way (to my knowledge). The "system" records it and uses that on free. Maybe the answer is not satisfying you but I do not think you'll get one with more generically applicable info :-)
    – jdehaan
    Commented Jan 20, 2019 at 18:32
9

This answer is relocated from How does free() know how much memory to deallocate? where I was abrubtly prevented from answering by an apparent duplicate question. This answer then should be relevant to this duplicate:


For the case of malloc, the heap allocator stores a mapping of the original returned pointer, to relevant details needed for freeing the memory later. This typically involves storing the size of the memory region in whatever form relevant to the allocator in use, for example raw size, or a node in a binary tree used to track allocations, or a count of memory "units" in use.

free will not fail if you "rename" the pointer, or duplicate it in any way. It is not however reference counted, and only the first free will be correct. Additional frees are "double free" errors.

Attempting to free any pointer with a value different to those returned by previous mallocs, and as yet unfreed is an error. It is not possible to partially free memory regions returned from malloc.

1
5

malloc() and free() are system/compiler dependent so it's hard to give a specific answer.

More information on this other question.

1
  • 2
    They're really library-dependent (typically the C library, which is usually very closely linked to the OS). To the compiler, they're just functions. Commented Jun 21, 2010 at 8:19
5

On a related note GLib library has memory allocation functions which do not save implicit size - and then you just pass the size parameter to free. This can eliminate part of the overhead.

4

The heap manager stored the amount of memory belonging to the allocated block somewhere when you called malloc.

I never implemented one myself, but I guess the memory right in front of the allocated block might contain the meta information.

1
  • 5
    That's one possible implementation, but one could devise a system where all memory is tracked in a single table in a totally different page, not necessarily anywhere close to the memory pool being allocated from.
    – ephemient
    Commented Oct 6, 2009 at 2:01
3

The original technique was to allocate a slightly larger block and store the size at the beginning, then give the application the rest of the blog. The extra space holds a size and possibly links to thread the free blocks together for reuse.

There are certain issues with those tricks, however, such as poor cache and memory management behavior. Using memory right in the block tends to page things in unnecessarily and it also creates dirty pages which complicate sharing and copy-on-write.

So a more advanced technique is to keep a separate directory. Exotic approaches have also been developed where areas of memory use the same power-of-two sizes.

In general, the answer is: a separate data structure is allocated to keep state.

1
  • Typically on MacOS you have large blocks for 16 byte mallocs, for 32 byte mallocs, for 48 byte mallocs etc. For malloc/free you just set or clear a single bit for each block. Very cache friendly because the bits for say 128 malloc blocks are all in a single cache line.
    – gnasher729
    Commented Jul 8, 2023 at 7:03
2

To answer the second half of your question: yes, you can, and a fairly common pattern in C is the following:

typedef struct {
    size_t numElements
    int elements[1]; /* but enough space malloced for numElements at runtime */
} IntArray_t;

#define SIZE 10
IntArray_t* myArray = malloc(sizeof(intArray_t) + SIZE * sizeof(int));
myArray->numElements = SIZE;
1
  • That's a completely different technique to the one BSD malloc uses for small objects ( though it's a perfectly good technique for creating Pascal style arrays ) Commented Oct 5, 2009 at 16:34
1

to answer the second question, yes you could (kind of) use the same technique as malloc() by simply assigning the first cell inside every array to the size of the array. that lets you send the array without sending an additional size argument.

0

When we call malloc it's simply consume more byte from it's requirement. This more byte consumption contain information like check sum,size and other additional information. When we call free at that time it directly go to that additional information where it's find the address and also find how much block will be free.

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