1

Say that you allocate some memory on the heap through malloc:

someType x  = malloc(someSize);

When you don't need x anymore, you free it:

free(x)

To perform this operation, I guess, the compiler must know the size of the memory x points to. I think free performs something like:

  1. Read the value contained in x (say it is 16).
  2. Determine in some way the memory size allocated at 16 (say it's 8).
  3. Tell OS that the 16-23 memory region is not longer needed and the OS can do whatever it wants with it.

If the above is true, why the standard doesn't prescribe a function that just performs the above till step 2 and returns the size to the programmer, without actually freeing the memory?

Some (not much convincing) reasons I read online:

  • malloc() implementations usually keep track of a region's size, but they may do this indirectly, or round it up to some value, or not keep it at all. It's that true? Can possibly malloc not keep track of the size?
  • Because C is a low-level programming language, it expects you to take care of these issues yourself, but this adds greater flexibility in exactly how you implement it. Ok, this is more philosophical. Nonetheless, C standard has evolved and has provided, over time, more headers, functions and tools.
  • You should rewrite every compiler to implement such function. Is that true? If free is already implemented, you just need a part of it; the implementation should be trivial.

So, there is a cogent, impellent, real reason why this function is not provided? Or, more likely, why the above reasons are actually important and what I am missing?

I think that this question has been already asked (and very likely a dupe), but I was able only to find similar subjects, for instance (btw the below questions are where I found some of the objections I reported above):

Here is discussed whether such function exists, but not why it doesn't.

Here is discussed about (stack-allocated)arrays.

15
  • 1
    It's not the compiler which knows the size of the block, but the OS and the programmer who allocated it. Sep 11, 2019 at 8:52
  • 2
    @WeatherVane When you call free you don't say: free this size; you just provide the pointer. So the compiler must know how much memory is allocated. Am I wrong?
    – nicola
    Sep 11, 2019 at 8:53
  • 2
    No, the compiler doesn't say "free this size", it says "free the block starting at this address". It's the allocator who takes care of how much to free, and the allocator may reside in the OS, with the compiler not knowing about it.
    – Ruslan
    Sep 11, 2019 at 8:54
  • 2
    @nicola The compiler doesn't know anything. It's the c standard library which implements the malloc and free functions and that's completely independent by the compiler itself.
    – Vlad Rusu
    Sep 11, 2019 at 8:55
  • 1
    The allocation size stored in the control block (if there is indeed such a thing) is not necessarily the size passed to malloc. Exposing it won't do you much good. Sep 11, 2019 at 9:12

2 Answers 2

6

Because it is completely unnecessary. The user who called malloc() already knows the size s/he asked for. If they want to store it, they can use a variable to do that. They could even store it inside the malloc'd memory itself!

Secondly, when you call malloc(size), you almost never get a memory block of exactly size bytes. The only thing the standard guarantees is that you get at least size bytes. This difference could be due to reasons like alignment, using larger block sizes to prevent fragmentation, etc..

Now, let's suppose that the standard library were to provide a function of the kind that you asked. Should it give you the allocated size or the requested size? The allocated size is heavily implementation dependent and practically meaningless since you are only allowed to access as many bytes as you initially asked for. If you go beyond that, it is undefined behaviour. If you say requested size, the implementation only needs the allocated size to handle the free, so why should it waste memory storing the requested size as well?

1
  • Comments are not for extended discussion; this conversation has been moved to chat.
    – Samuel Liew
    Sep 12, 2019 at 6:25
5

Such a function does not exist since nobody has managed to convince the C standards committee of its necessity.

Providing it might also require additional interfacing with the operating system - where such memory tasks are often delegated, so that could require changes to be made in order to implement the new functionality. That could damage the reputation of C as being supported by many platforms.

Lastly, what would you use it for? I can't conject a use case. If you want to keep track of allocated sizes yourself, then create stub functions for malloc, realloc, &c.

1
  • I like a lot the first sentence and the fact that it requires some work. Of course it's not necessary, but it might be handy. Many functions provided by the standard aren't actually necessary.
    – nicola
    Sep 11, 2019 at 9:02

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