0

I was wondering if the size of a dynamically allocated array is stored somewhere in memory because calling free() function on a pointer initialized by malloc() or its family frees the memory accurately.

So if free() knows exactly how much memory to de-allocate,doesn't that mean that the size value is stored somewhere in memory?

Can we retrieve this value?

Sourav Ghosh
  • 133,132
  • 16
  • 183
  • 261
  • 5
    It should depend on the implementation of the library. – MikeCAT Jun 02 '16 at 12:58
  • 4
    Related: http://stackoverflow.com/questions/8335402/how-to-find-how-much-memory-is-actually-used-up-by-a-malloc-call – Grzegorz Szpetkowski Jun 02 '16 at 13:01
  • @layzak simply see source code of (g)libc; aim for the definition of free/malloc - https://github.com/lattera/glibc/tree/master/malloc - actually, yeah, it stores both begin and end of used mem block. –  Jun 02 '16 at 13:01
  • 1
    Highly related/maybe dupe: http://stackoverflow.com/questions/1518711/how-does-free-know-how-much-to-free, http://stackoverflow.com/questions/197675/how-does-delete-know-the-size-of-the-operand-array – NathanOliver Jun 02 '16 at 13:02
  • 1
    It is highly implementation dependant. On unixes, malloc is a higher lever abstraction above sbreak or mmap that has its own book keeping structures. But unless you want to develop an allocation library, you as a C or C++ programmer should considere `malloc` - `free` or `new` - `delete` as an opaque dynamic allocation system. – Serge Ballesta Jun 02 '16 at 13:06
  • This is an awesome explanation about how malloc work (on osx) http://www.cocoawithlove.com/2010/05/look-at-how-malloc-works-on-mac.html – albttx Jun 02 '16 at 13:10
  • @Olaf I though we closed this question? – 2501 Jun 02 '16 at 13:32
  • @2501: At least I CVed as dup (didn't see the actual close). Who re-opened without commenting? – too honest for this site Jun 02 '16 at 13:37
  • @Olaf I closed it with gold-hammer, and it was closed for a few minutes, but it was magically reopened without any traces. – 2501 Jun 02 '16 at 13:37
  • @2501: A mod gone wild? it clearly is a dup. Not sure if that was it, but: http://stackoverflow.com/questions/5451104/how-to-get-memory-block-length-after-malloc – too honest for this site Jun 02 '16 at 13:39
  • @2501: flagged for mod. At least leaving a comment would be the polite way. – too honest for this site Jun 02 '16 at 13:43
  • I reopened it. The duplicate was not close enough. In my humble opinion, naturally. – Bathsheba Jun 02 '16 at 14:02

4 Answers4

1

malloc() is a library wrapper function over many levels of low-layer memory allocation call. It hides details like the size of actual memory size allocated by the memory allocator of the OS and directly returns a valid pointer (in case of success) to the application making the call. The pointer can be used safely (within the bounds) and passed to free() after the usage is done.

The pointer returned as the return value of malloc() need not be (and most of the time, it's not) the actual address returned by the memory allocator. OS/ memory allocator does some book-keeping under the hood to keep the track of size of allocated memory and then after some adjustments, it passes a pointer to the malloc() which is then returned to the application calling malloc().

There is no standard way of getting the actual info (it was meant to be hidden from the user, because of proper reasons, IMHO), but there are platform-dependent ways of getting the info.

Sourav Ghosh
  • 133,132
  • 16
  • 183
  • 261
0

It may well be; that would indeed be a sensible implementation of the C standard library.

But it's not guaranteed by the C standard.

There's nothing stopping me from writing a C-standard compliant library that always allocates a fixed amount of memory for every malloc call! (And return appropriately if you're asking for an amount in excess of that fixed value). Then I would not need to store the amount allocated anywhere.

Bathsheba
  • 231,907
  • 34
  • 361
  • 483
0

If you are using glibc there is the non-standard malloc_usable_size method that you can use. It may return a larger number than the number of bytes that you allocated, though.

Martijn Otto
  • 878
  • 4
  • 21
  • It would make sense that it would return more bytes than you allocated because the size values stored in the header and footer of malloc allocated memory blocks include the size of the header and footer. – alltrue Jun 02 '16 at 14:01
  • The header and footer bytes are not included in the return-value from malloc_usable_size. malloc just allocates more memory than asked for in many cases. When and how much extra memory seems to depend on the specific architecture and the current position of the moon, though. – Martijn Otto Jun 03 '16 at 08:49
0

Is the size of a dynamically allocated array stored somewhere in RAM?

Maybe. Maybe not.

C does not specify how the memory management is implemented. If portable code needs the size allocate for a pointer, it must handle that itself.


Select platforms do provide helper functions as some systems do include a size stored in memory. Others encode the size in the pointer, fixed sizes per block of memory, etc.

chux - Reinstate Monica
  • 143,097
  • 13
  • 135
  • 256