Arrays and pointers to allocated memory

This is a discussion on Arrays and pointers to allocated memory within the C Programming forums, part of the General Programming Boards category; When memory is allocated during runtime it is easier to get the size of it by a function, rather than ...

What on earth are you talking about?
Why is it so hard to pass around the size value along with the pointer, which is so much more flexible since it allows you to use real arrays as well as dynamic memory (just look at fgets() for a prime example).

Code:

void foo ( char *ptr ) {
// There is NOTHING you can do here to tell whether ptr is
// a global variable, a local variable, or a malloc'ed variable
// or tell exactly how many bytes are being pointed at.
}
char global[10];
int main ( ) {
char local[20];
char *dynamic = malloc(30);
foo( global );
foo( local );
foo( dynamic );
foo( &dynamic[20] ); // is in dynamic memory, just not the start of it
}

Assuming that your magic function could take care of the last two cases, the first two would be much harder to solve. Such a function would have to return an error status of some sort, then what are you supposed to do - bail out?

If it really bugs you that much, create a struct which contains the pointer and the size, and pass that around instead. Or move to C++ and use the additional creature comforts offered by std::vector

I know what you say. But this is not like for example dynamic arrays. This is something that should being puted into language when it was created. I feel it should be standard in C an C++ but I don't insist on it.