C
CJ
A conforming implementation is free to allocate a block larger than
requested; most real implementations do so. Some implementations round
upward to the next multiple of a fixed block size. Some implementations
round upward to the next power of 2.
Are you suggesting that the programmer should concern himself with the
internal implementation details of malloc()?
The only conceivable uses I can think of are:
1) the programmer has allocated some memory and needs a bit more, so
does something like
if(actually_allocated(p) >= WHAT_I_NEED_NOW)
/* do nothing! */
else
/* use realloc on p */
but in this case, if enough memory has already secretly been assigned by
malloc then the realloc() call is likely to be of negligible cost
anyway.
2) misguided attempts at bounds checking:
void some_library_function(char * mallocd_pointer, size_t index)
{
if(index >= actually_allocated(mallocd_pointer))
/* go boom */
...
}
This has two problems: a) the function can only accept a pointer
returned by malloc() ; b) if the programmer got lucky and supplied an
index that was beyond the memory he knew he had available, but which
just happened to lie inside the additional memory his particular
malloc() implementation had assigned, this would disguise the bug in his
code and likely make it extremely difficult to find and fix.