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);

Leave a Comment