Getting the size of a malloc only with the returned pointer
The pointer is a pointer, and not an array. It can never be "recognized as an array", because it is not an array.
It is entirely up to you to remember the size of the array.
For example:
struct i_must_remember_the_size
{
size_t len;
int * arr;
};
struct i_must_remember_the_size a = { 10, NULL };
a.arr = malloc(a.len * sizeof *a.arr);
There's no standard way to do what you ask. Some compilers may provide a function for that, or some other allocators may have such a function, but, as already said there's nothing standard.
Notice that the sizeof
applied over arrays does not work because it recognizes the pointer "as from an array": it just recognizes its argument as an array (sizeof
is one of the few contexts in which an array do not decay to a pointer to its first element); once your array decays to a pointer sizeof
will only yield the size of the pointer.