0
I’m testing with dynamic memory allocation, but when I try to get the array size I always get the same result.
int main()
{
int *teste=malloc(sizeof(int) * 10);
int len=sizeof(teste) / sizeof(int);
print("%i\n", len);
return 0;
}
Compiling with gcc:
bash-4.2$ ./teste
2
It doesn’t matter if I put sizeof(int) * 100
or 10
, always returns 2 :(
What am I doing wrong?
I believe it’s because you’re calculating the size of a pointer, not a array. Why pointers have fixed size independent of the pointed type?
– Woss
I think that may be the case, but then another question arises.. how to get the size of a dynamic array, need to do a structure or other location to save the size?
– fdavid