I've allocated an "array" of mystruct of size n like this:
if (NULL == (p = calloc(sizeof(struct mystruct) * n,1))) {
/* handle error */
}
Later on, I only have access to p
, and no longer have n
. Is there a way to determine the length of the array given just the pointer p
?
I figure it must be possible, since free(p)
does just that. I know malloc()
keeps track of how much memory it has allocated, and that's why it knows the length; perhaps there is a way to query for this information? Something like...
int length = askMallocLibraryHowMuchMemoryWasAlloced(p) / sizeof(mystruct)
I know I should just rework the code so that I know n
, but I'd rather not if possible. Any ideas?