Re: sizeof(static array[])
It's possible some compilers might be able to figure that out. However, in general you should never use sizeof() as a means of determining array size.
Re: sizeof(static array[])
whats a better way i could do this then?
Re: sizeof(static array[])
I agree with Lindley. Sizes, even size of an int, in C++ is implementation-defined. Consider it a "dependency". There's lot of differences in mem requirements and access -even among different MACHINES-, so it's straight logical (as for dependencies). When "sizes" are understood in terms of multiples of a char (== 1), then it's clear that no one (no compiler) guarantees a correct return value for an array (different possible fundamental types on different machines that might be stored to an array).
However, what exactly do u need it for?
Re: sizeof(static array[])
Quote:
However, what exactly do u need it for?
to find the number of structures/elements in the array. I was going to go with sizeof(array)/sizeof(array[0]) but I think this could be inaccurate anyway, if you pack a struct a certain way an the compiler then aligns the array then this would give a wrong array lenght.
Re: sizeof(static array[])
Quote:
Originally Posted by staticVoid
to find the number of structures/elements in the array. I was going to go with sizeof(array)/sizeof(array[0]) but I think this could be inaccurate anyway, if you pack a struct a certain way an the compiler then aligns the array then this would give a wrong array lenght.
Then how would pointer arithmetic work on such an odd setup? It is guaranteed that if you have the address of the first element in the array, adding the same number of bytes gets you to the next element.
There are no issues with sizeof(array) / sizeof(array[0]).
Regards,
Paul McKenzie
Re: sizeof(static array[])
Since by definition the number of things in the array must be a compile-time constant, why even bother? Just put another variable defined right next to it with the number of items.
Re: sizeof(static array[])
solved: it was a linker error, in the .cpp file I forgot to put class:: before the array identifier.