Array size at run time without dynamic allocation is allowed?

57,315

Solution 1

This is valid in C99.

C99 standard supports variable sized arrays on the stack. Probably your compiler has chosen to support this construct too.

Note that this is different from malloc and new. gcc allocates the array on the stack, just like it does with int array[100] by just adjusting the stack pointer. No heap allocation is done. It's pretty much like _alloca.

Solution 2

This is known as VLAs (variable length arrays). It is standard in c99, but gcc allows it in c++ code as an extension. If you want it to reject the code, try experimenting with -std=standard, -ansi and -pedantic options.

Solution 3

It is valid only in C99. Next time you may try checking your code in a reliable compiler.

Solution 4

It is valid C99, it is not valid C++. This is one of not a few differences between the two languages.

Solution 5

This Code runs in GNU GCC Compiler.

#include<bits/stdc++.h>

int main(int argc, char **argv)

{
    size_t size;

   std:: cin >> size;

    int array[size];

    for(size_t i = 0; i < size; i++)

{

array[i] = i;

        std:: cout << i;

 }

    return 0;
}
Share:
57,315

Related videos on Youtube

Santiago Porras
Author by

Santiago Porras

Seeking knowledge.

Updated on July 05, 2022

Comments

  • Santiago Porras
    Santiago Porras almost 2 years

    I've been using C++ for a few years, and today I saw some code, but how can this be perfectly legal?

    int main(int argc, char **argv)
    {
        size_t size;
        cin >> size;
        int array[size];
        for(size_t i = 0; i < size; i++)
        {
            array[i] = i;
            cout << i << endl;
        }
    
        return 0;
    }
    

    Compiled under GCC.

    How can the size be determined at run-time without new or malloc?

    Just to double check, I've googled some and all similar codes to mine are claimed to give storage size error.

    Even Deitel's C++ How To Program p. 261 states under Common Programming Error 4.5:

    Only constants can be used to declare the size of automatic and static arrays.

    Enlight me.

    • Admin
      Admin about 15 years
      note that DMA means "direct memory access" - I think you are asking about dynamic allocation
    • Lightness Races in Orbit
      Lightness Races in Orbit about 9 years
      C or C++? Pick one.
    • Nic
      Nic about 8 years
      Again, pick one: C or C++. This question is explicitly and only about C; there should not be a C++ tag.
    • Vadzim
      Vadzim over 5 years
  • mmx
    mmx about 15 years
    I guess it's going to be supported in C++0x
  • Admin
    Admin about 15 years
    Not according to section 8.3.4 of the draft standard.
  • Johannes Schaub - litb
    Johannes Schaub - litb about 15 years
    it won't ever be included in c++1x :D but let's hope dynarray<T> gets in. i would love it. so you could do dynarray<int> a(some_size); and have it allocate efficiently, possibly with compiler hax like _alloca and so on.
  • Jeff Lamb
    Jeff Lamb over 13 years
    I came across this same scenario in a file in our code base that was written months ago. I was baffled, as was the rest of the team, as to why it compiled. In our situation, the size of the array was calculated before declaring the array as well (which shouldn't be allowed, either?) Anyway, a challenge went out. Anyone who could answer why this is legal gets a pop tart. If you're ever in Seattle, let me know. I have a pop tart for you.
  • balki
    balki almost 13 years
    Can you provide some info/link on how stack internally works in this case? Does this introduce some overhead in run time?
  • mmx
    mmx almost 13 years
    @balki The overhead is minor, as it's basically increment/decrementing the stack pointer. The stack behavior can be essentially identical to the normal case if you save the original stack pointer at the beginning of the function.
  • jerry
    jerry about 11 years
    Most compilers don't assign anything to uninitialized local variables, they will usually appear to hold whatever was in the memory that they occupy until they are assigned by the program. It seems the Dev-C++ you referenced is an IDE on top of MinGW, which includes a port of GCC as the compiler. As noted in other answers, VLAs are not standard C++, but some compilers (including g++) support them anyway.
  • Red XIII
    Red XIII almost 11 years
    For anyone from the future: it will be included in C++14 (with smaller set of features such as typedef, sizeof(), etc.).
  • Elkvis
    Elkvis over 9 years
    Dev-C++ is not a compiler. Neither is Visual Studio. Dev-C++ uses GCC/G++ as its compiler, while Visual Studio uses cl (Microsoft's compiler back-end). By themselves, Dev-C++ and Visual Studio are Integrated Development Environments (IDEs). This is an important distinction to make. Dev-C++ does not "assign" anything. The compiler does that.
  • justpraveen
    justpraveen over 9 years
    is this allowed in c++ ? Visual studio does not allow this?
  • Jason
    Jason over 8 years
    VLAs are still not part of the standard. There's a dynarray TS, but as of yet it isn't ratified.
  • magras
    magras over 8 years
    @RedXIII: Bad guess. Neither VLA nor dynarray are parts of C++14.
  • Jonathan Wakely
    Jonathan Wakely over 5 years
    The -std and -ansi options have no bearing on this extension at all.
  • St.Antario
    St.Antario over 4 years
    @JohannesSchaub-litb Are there plans to introduce it in further realeases of the Standard?