Array Member Syntax

This is a discussion on Array Member Syntax within the C++ Programming forums, part of the General Programming Boards category; While going over my friend's code, I noticed he had Code: class A { public: ... private: int counts[]; }; ...

  1. #1
    Amazingly beautiful user.
    Join Date
    Jul 2005
    Location
    If you knew I'd have to kill you
    Posts
    254

    Array Member Syntax

    While going over my friend's code, I noticed he had
    Code:
    class A
    {
      public:
       ...
      private:
        int counts[];
    };
    Having no idea what empty array brackets at the end of declaration like that do (it looks like it should statically allocate like counts[10] or counts[20], but the lack of number implies dynamic allocation), or even why it compiled, I messed around.

    Yet, if I attempt
    Code:
    int main()
    {
      int counts[];
    }
    I get a compile error, because a size is not specified. On the other hand, putting this is a member of a class appears to yield an array of size zero.

    What exactly is the point of this syntax, why allow the creation of arrays of size zero in a class, but not nomrally, etc. etc.?
    Programming Your Mom. http://www.dandongs.com/

  2. #2
    The superhaterodyne twomers's Avatar
    Join Date
    Dec 2005
    Location
    Ireland
    Posts
    2,262
    Was it assigned in the constructor?I believe
    Code:
    int counts[] = { 2, 3, 4, 5, ... };
    should compile. But not without without assignment.

    Edit: To explain further. You can create an array without a size parameter but it must be assigned appropriate values straight away. I believe that the size parameter is 'added' by the compiler as it goes through the code and the size of the array, as in number of elements not sizeof, is the count of the number of elements between the curly braces.
    Last edited by twomers; 10-01-2008 at 11:29 AM.

  3. #3
    The larch
    Join Date
    May 2006
    Posts
    3,573
    Actually this should not be allowed in standard C++, so your compiler may support it as an extension.

    The idea (in C99?) might be that if the last member of a struct is a zero-sized array (not legal elsewhere), you should be able to overallocate memory for the struct, and index into this extra memory using the array member. Or something like that - Google might tell you more.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  4. #4
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    You can do the same thing by using a zero sized array (which anon also made mention of) though either way issues warnings or errors on all the compilers I use.

    The idea is (and I use this a lot, so don't think its useless or stupid)...

    Example:
    Code:
    struct my_bstr
    {
    private:
      size_t length, capacity;
      char data[]; // it has to be the last member for this to work.
    
    public:
      explicit my_bstr(const char *literal) : length(0), capacity(0)
      {
        for(register const char *i = literal; *i; ++i,++length)
          ;
    
        capacity = length * 2; // this is a stupid algo, but I am just trying to demonstrate a point.
      }
    
      virtual ~my_bstr() { }
      operator const char *() const
      {
        return data;
      }
    };
    The string doesn't require a null terminator or anything like that since that is not how it is stored internally. This is more like how PASCAL's string objects are stored. Though, not exactly the same.

  5. #5
    The larch
    Join Date
    May 2006
    Posts
    3,573
    I really can't understand (without getting a massive headache) the string implementation of MingW (where sizeof(string) == 4), but a comment says this:

    Code:
       *  A string looks like this:
       *
       *  @code
       *                                        [_Rep]
       *                                        _M_length
       *   [basic_string<char_type>]            _M_capacity
       *   _M_dataplus                          _M_refcount
       *   _M_p ---------------->               unnamed array of char_type
       *  @endcode
    And later:
    Code:
       *  This approach has the enormous advantage that a string object
       *  requires only one allocation.
    Which looks like a similar technique might be in use? (Overallocation, but I can't find a zero-sized array. _M_p just points beyond the end of the class?)
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. file reading
    By gunghomiller in forum C++ Programming
    Replies: 9
    Last Post: 08-07-2007, 11:55 PM
  2. Learning OpenGL
    By HQSneaker in forum C++ Programming
    Replies: 7
    Last Post: 08-06-2004, 09:57 AM
  3. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 10:54 AM
  4. Quick question about SIGSEGV
    By Cikotic in forum C Programming
    Replies: 30
    Last Post: 07-01-2004, 08:48 PM
  5. Couple C questions :)
    By Divx in forum C Programming
    Replies: 5
    Last Post: 01-28-2003, 01:10 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21