CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 8 of 8
  1. #1
    Join Date
    Oct 2007
    Location
    Scotland
    Posts
    137

    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?

  2. #2
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    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.

  3. #3
    Join Date
    Oct 2007
    Location
    Scotland
    Posts
    137

    Re: sizeof(static array[])

    whats a better way i could do this then?

  4. #4
    Join Date
    Oct 2008
    Location
    Tel Aviv, Berlin, L.A.
    Posts
    23

    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

  5. #5
    Join Date
    Oct 2007
    Location
    Scotland
    Posts
    137

    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.

  6. #6
    Join Date
    Apr 1999
    Posts
    27,449

    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

  7. #7
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    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.

  8. #8
    Join Date
    Oct 2007
    Location
    Scotland
    Posts
    137

    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
  •  





Click Here to Expand Forum to Full Width

Featured