The underlying implementation of malloc
or new
must inevitably keep track of the size of the memory allocated for your variable. Unfortunately, there is no standard way to get the size of allocated block. The reason is due to the fact that not all memory block are dynamically allocated, so having the function that only works for only dynamic allocation is not so useful.
void fillwithzero(int* array) {
unsigned int size = getsize(array); // return 0 for statically allocated array ??
for(int i = 0; i < size; i++) {
array[i] = 0;
}
}
Let us say we have getsize
function that is capable of magically get the size of the dynamically allocated array. However, if you send pointer of a static array, or some array allocated by other means (e.g. external function) to fillwithzero
, this function will not be working. That is why most C function that accept array required caller to send the size or the maximum size of the array along with the array itself.