For a char []
, I can easily get its length by:
char a[] = \"aaaaa\";
int length = sizeof(a)/sizeof(char); // length=6
However,
Just use std::vector
which keep the (dynamic) size for you. (Bonus, memory management for free).
Or std::array
which keep the (static) size.
Create a structure to keep the info, something like:
typedef struct {
char* ptr;
int size;
} my_array;
my_array malloc_array(int size)
{
my_array res;
res.ptr = (char*) malloc(size);
res.size = size;
return res;
}
void free_array(my_array array)
{
free(array.ptr);
}