0
int main()
{
    int num=0;
    std::cin>>num;
    int array[num];
    array[0]=40;
    std::cout<<array[0];
}

The above code is written in C++(The above code was written in C99)., and the code declares a variable and then declares an array with the size of the variable as input from the user. I know that such a declaration is a local variable, so an array called array is created in the stack area. However, since the stack area is a static area, I know that "the size of the stack frame should not change during runtime, and the size is already determined before the program starts", but in the case of my code, there is a contradiction that the size of the array is eventually determined during runtime by receiving input from the user after the program starts, so I wonder why an error does not occur. I know that C99 allows this type of thing, but in the end, what it means to say "allocated on the stack area" is that the size must already be specified before the program starts. Isn't that correct? But I'm also wondering why Java allows this behavior.

I used the C++ development version of Visual Studio 2022,

int main()
{
    int a = 0
    int b = 0;
    std::cin >> b;  
    int array[b];
}

When typing this code

"Expression must have a constant value. The value of variable "b" cannot be used as a constant." 

You will get an error. However, C99 says that code like the above does not raise an error.

Yksisarvinen
  • 18,008
  • 2
  • 24
  • 52
김태영
  • 11
  • 2
  • 2
    This is not standard C++ : `int array[num];` since num is not a constant (gcc supports it as extension but msvc does not). Use [`std::vector`](https://en.cppreference.com/w/cpp/container/vector) – Pepijn Kramer May 30 '23 at 13:59
  • C and C++ are two different languages and you should not expect code that works in one to work on the other. Back at the beginning of C++ there was a lot of compatibility, but there was also major differences, and that has only grown since then. – NathanOliver May 30 '23 at 13:59
  • 2
    C99 supports variable-length arrays; C++ doesn't. GCC supports VLAs in C++ as a non-standard compiler extension. MSVC (the compiler bundled with Visual Studio) does *not* support VLAs (not in C++ as an extension, and not in C, because it doesn't support C99). – Cody Gray - on strike May 30 '23 at 14:01
  • 7
    Because unfortunately gcc and clang accept Variable Length Arrays as compiler extension by default, without having to specify a compilation flag or a warning. This has led to generations of new C++ developers using that feature incorrectly and teaching others the same :/ – Yksisarvinen May 30 '23 at 14:01
  • Get up to date with current C++ using : A [recent C++ book](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) or have a go at https://www.learncpp.com/ (that's pretty decent, and pretty up-to-date). For C++ reference material use : [cppreference](https://en.cppreference.com/w/). And after you learned the C++ basics from those sources, look at the [C++ coreguidelines](https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines) regularely to keep up-to-date with the latest guidelines. – Pepijn Kramer May 30 '23 at 14:02
  • Your assumption about the stack frame size not changing during runtime is incorrect. The POSIX function [alloca](https://www.man7.org/linux/man-pages/man3/alloca.3.html) does change the stack frame size at runtime. Presumably C99 compilers do something similar with variable length arrays. See duplicate above for more details. – john May 30 '23 at 14:05
  • 1
    All it takes is for clang and g++ to not make VLA's the default, and just let developers know that "starting with version x, a flag will be introduced to allow variable length arrays in C++ code". This way, there is less chance for newbies to make this mistake, plus it practically forces those (poor C++) websites showing such code to make changes. – PaulMcKenzie May 30 '23 at 14:06
  • 1
    A related post about why VLAs are a serious problem in C++: https://stackoverflow.com/a/21519062/179910 – Jerry Coffin May 30 '23 at 14:11

0 Answers0