|
-
October 2nd, 2008, 04:53 PM
#1
sizeof(static array[])
hi, I have a static array, as a static class member whichz definition looks a bit like:
Code:
//.h
class A
{
public:
static struct _Foo { int x, y, z; } Foo[];
};
.cpp
A::_Foo A::Foo[] =
{
{ 1, 2, 3 },
{ 4, 5, 6 },
{ 7, 8, 9 }
};
when I ask the compiler for the sizeof(A::Foo) it says its an invalid use of the sizeof directive, this seems a bit strange to me, when you initialize Foo, there will only be a fixed amount of _Foo structs in the array, so why can't the compiler give me this value?
-
October 2nd, 2008, 05:00 PM
#2
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.
-
October 2nd, 2008, 05:22 PM
#3
Re: sizeof(static array[])
whats a better way i could do this then?
-
October 2nd, 2008, 05:42 PM
#4
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?
ariell
programming is understanding
-
October 2nd, 2008, 06:16 PM
#5
Re: sizeof(static array[])
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.
-
October 2nd, 2008, 06:40 PM
#6
Re: sizeof(static array[])
 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
-
October 2nd, 2008, 07:32 PM
#7
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.
-
October 3rd, 2008, 03:15 PM
#8
Re: sizeof(static array[])
solved: it was a linker error, in the .cpp file I forgot to put class:: before the array identifier.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|