DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 7 of 7

Thread: Variable sized arrays

  1. #1
    Join Date
    Oct 2007
    Posts
    369

    Variable sized arrays

    When I use g++ -pedantic, it complains about:

    Code:
    int SomeClass::f(int i,...)
    {
       char buf[i];
    ...
    }
    It gives me the message that ISO C++ forbids variable-size arrays.

    Is g++ correct in calling this a variable-sized array?

    If it is correct, can someone tell me the rationale on why the above code is bad?

    (I can change it by "char *buf = new char[i]; ", but then I don't get the benefit of an auto variable.

  2. #2
    Join Date
    Nov 2003
    Posts
    4,118
    Variable sized arrays are available only in C99. See http://www.devx.com/cplus/10MinuteSolution/27219

    In C++ and in pre C99 C all arrays must have constant sizes which are must be known at compile time (yes, there are constants which aren't known at compile time but that's a digression).
    In your code, you're using a variable instead of a constant as the array's size so the compiler assumes that you meant to use VSA. If you want to use this feature you need to enable C99 extensions explicitly, but note that this will make your code non-portable (at least with respect to C++).
    Danny Kalev

  3. #3
    Join Date
    Oct 2007
    Posts
    369
    Thanks for the info.
    When I think variable-sized array, I think of BASIC or what we can do in collection classes. Since the size of the array was fixed at instantiation, I thought of it as a fixed-size array.
    Of course, this means that the compiler can't know what size it is at compile time.
    Is the issue, then, that the compiler needs/wants to know the size of any object that will be instantiated on the stack (versus the heap)?

  4. #4
    Join Date
    Nov 2003
    Posts
    4,118
    Exactly. The compiler must know the exact size of an array to allocate sufficient stack or static storage for it (that is, it needs to know the offset for other local variables on the same stack frame). In C99, the meaning of an array was stretched, and now arrays can have a size that isn't known at compile time but this incurs serious performance penalties and risks. Anyway, C++ isn't going to adopt this feature. I spoke with Bjarne Stroustrup about this and other C99 features. He didn't exactly express his enthusiasm about VSAs...
    Danny Kalev

  5. #5
    Join Date
    Dec 2007
    Posts
    401
    > Anyway, C++ isn't going to adopt this feature
    i guess, because std::vector<> does provide equivalent functionality.
    albeit, less efficiently (when you have sufficient stack space).

  6. #6
    Join Date
    Nov 2003
    Posts
    4,118
    std::vector is no quite the same thing because its allocation scheme is dynamic, it's resizable, it occupies much more memory than N*sizeo(T) objects, a vector's address is not the address of the first array element etc.
    The closest thing to VSA is std::array<T>, or some non-standard stack-allocated dynamic arrays. To be honest, C++ doesn't have a viable equivalent to VSAs.
    Danny Kalev

  7. #7
    Join Date
    Jan 2008
    Posts
    4
    In C++ the array size must be known before compilation. If you wants that the user give the size of array then take the input at run time and passes it to the array.

Similar Threads

  1. GetString Help
    By Chris Yard in forum VB Classic
    Replies: 5
    Last Post: 02-08-2007, 06:44 AM
  2. How to retain session variable
    By NikhilSriv in forum Java
    Replies: 1
    Last Post: 06-14-2006, 05:24 AM
  3. Replies: 15
    Last Post: 05-09-2001, 04:40 AM
  4. Variable arrays and Database
    By Colleen Coleman in forum VB Classic
    Replies: 1
    Last Post: 11-29-2000, 02:25 PM
  5. VB Arrays and memory allocation
    By Brian Leung in forum VB Classic
    Replies: 12
    Last Post: 06-20-2000, 03:06 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center