Novice c programmer here. This may be nonsense.
I've learned about some basics of dynamic memory allocation using malloc
and free
. When I allocate memory, for concreteness say for an array of 10 ints
via malloc
, malloc
returns to me a pointer p to the beginning of the memory block allocated. Typically I'd like to pass that pointer, as well as some relevant metadata about the memory allocation (namely, that it was for a length 10 array of int
s) to the rest of my code to support subsequent informed and safe read/write access. So that is what I do, ferrying this metadata around my code.
It's interesting to me that when the time comes to actually free the memory, free
is perfectly capable of doing the job given only the pointer p; it does not need to be told e.g. the length of the array. I don't have knowledge of any of the details here, but from what I've read, the implementations of malloc
/free
involve storage of and access to metadata that is written adjacent to each allocation, and that metadata is accessed by free
at de-allocation time (and is evidently sufficient to for it know precisely which memory locations should be freed). I imagine this must mean in particular that in my example, the length of the allocated array of ints can be gleaned from the metadata that free
is looking up.
This leads me to a few questions:
Are the high level details of my understanding of the existence of this metadata correct?
Can this allocation metadata be accessed by a c programmer using the standard library?
If not, can it be readily accessed from c by means other than the standard library?
If the answer to 2 or 3 is negative for reasons related to standards and rules, are there specific contexts or examples (e.g. that only work on a specific OS, or with a specific non-standard implementation of c, etc.) where this is possible and/or useful?
If this is a bad idea, regardless of whether it is possible, I'd be curious to hear about why. My only guess so far is that the details of how the metadata are stored might vary by system, so that code using it naively might end up being intolerant to system changes. Still, given that free can successfully make sense of it on different systems, I'd think there'd be a way to encapsulate the system dependencies to still get useful introspection into the metadata.
For 2 or 3, I'm imagining something like:
// Allocate some memory
int size = 10;
int *arr;
arr = (int *)malloc(sizeof(int) * size)
// Do other stuff.
// Choose not to keep track of storage details necessary for informed read/write access to arr, e.g. size;
// Acquire access to whatever `free` would access if asked to free `arr`
_type_ metadata = get_pointer_metadata(arr);
// Use metadata to perform read/writes
But don't know whether there is anything like get_pointer_metadata
in the c standard library, or third party packages?
I've tried searching google for such a function, but my understanding of the details of the situation and language is too poor to make meaningful searches.