0

I was learning about dynamic memory allocation functions and in that whenever we use malloc function you would have specified some bytes to allocate in the memory.

So when we assign the address of this allocated memory block the pointer will only have the information about the address of the first bite

When we call the free function on this pointer again free() how does it know how many bytes to deallocate since it takes only the pointer as the argument which only contains information about this first bite of the allocated memory.

Does the operating system keep some kind of a database table to hold the information about the address of the first byte allocated and the number of bytes are allocated?

Or does something else happen entirely?

If something like this database exists then who is maintaining this database and the how I can access this externally or explicitly in my program?

Use cases for accessing this info :- For anyone who is saying we don't need this info....

Let's say I am within a function and I haven't passed a parameter about how many integers my array has... In this case, to minimise the number of arguments, we can do this hack (although it's not desirable,)

Natesh bhat
  • 12,274
  • 10
  • 84
  • 125
  • 1
    The allocator stores the size near the allocated block. If you give it a pointer, it'll know how to find the size. – Petr Skocik Oct 16 '17 at 14:00
  • 1
    Yes, very roughly spoken: _the operating system (or rather the underlying library) keeps some kind of a database table to hold the information about the address of the first byte allocated and the number of bytes are allocated_. But the exact way how it is done is totally implementation dependent and therefore you cannot access this "database", and BTW you don't need to. – Jabberwocky Oct 16 '17 at 14:00
  • Strictly speaking - it is completely implementation defined. Any way you can think it might work - it can be implemented. It can stick a magnet note on your fridge with the size recorded. – Eugene Sh. Oct 16 '17 at 14:01
  • This is maybe not technically correct, but the function `free` does not know, but the OS knows. – klutt Oct 16 '17 at 14:02
  • 1
    @klutt In case there *is* an OS. – Eugene Sh. Oct 16 '17 at 14:02
  • @EugeneSh. True. – klutt Oct 16 '17 at 14:03
  • The curious thing about the implementation of the C library is the lack of a `size_t allocation_size(void *)` function. As the underlying library/OS knows the size, why is there not a standard function to retrieve it? Instead, user code for decades has been obliged to carry this info in some auxiliary fashion when - somehow - the systems knows the answer. Perhaps this lack of `allocation_size()` function supports archaic allocation routines that themselves do not know the answer and only ever consume? – chux - Reinstate Monica Oct 16 '17 at 16:08
  • @chux, You are incorrectly assuming that the allocated size is the same as the requested size. – ikegami Oct 16 '17 at 16:44
  • @ikegami OK - so change the function `allocation_size()` --> `allocation_available_size()`. IAC, access to the underlying available memory size is hidden. IMO it should be available with no negative impact on various allocation schemes. – chux - Reinstate Monica Oct 16 '17 at 16:55
  • @chux, That doesn't help. You still gotta carry around the requested allocated size along. – ikegami Oct 16 '17 at 17:50
  • @ikegami "You still gotta carry around the requested allocated size along ...useless function" disagree with that. Still thanks for your insights. As SO is not a good place to dig deeper into this, do you suggest any SE site that this may be on on topic? – chux - Reinstate Monica Oct 16 '17 at 17:55
  • @chuz, You're disagreeing with your own claim. You first claimed that we need to carry around requested allocated size unless it's provided `allocation_size()`, and you've just claimed you disagree with this. Which one is it? – ikegami Oct 16 '17 at 17:56
  • @klutt rather than the OS knowing, it's the internal functionality of the C standard library that records the exact size of allocated memory on top of the OS. The operating system itself only manages how many memory pages of a fixed size (4k in Linux I believe) in the processes' virtual memory address space are used. Look up the sbrk system call for more details. – Paul Humphreys Oct 17 '17 at 02:13

0 Answers0