1

For example if an integer array is declared:

int ar[12];

And here a vector of integers:

vector<int> ar;     //OR
vector<int> ar(12);

In either case, is memory allocated to the array at compile time or runtime? I know that vector class in C++ STL uses dynamic memory allocation but what about the ordinary array? Also:

int n;
cin >> n;
char ar[n];

If memory allocation is at compile time then how does this work? I can't find anything scavenging the net.

  • 7
    `cin >> n; char ar[n];` doesn't actually work. Add `-pedantic-errors` to your compiler flags and it will now give you an error. For whatever reason, GCC has this non-standard extension on by default. – NathanOliver Apr 20 '21 at 12:14
  • 1
    for the last part of the question see here: [Why aren't variable-length arrays part of the C++ standard?](https://stackoverflow.com/questions/1887097/why-arent-variable-length-arrays-part-of-the-c-standard) – 463035818_is_not_an_ai Apr 20 '21 at 12:14
  • 1
    `char ar[n];` is a [variable-length array](https://en.wikipedia.org/wiki/Variable-length_array) and those aren't actually part of C++. – Some programmer dude Apr 20 '21 at 12:15
  • Good read: https://stackoverflow.com/questions/21350478/what-does-memory-allocated-at-compile-time-really-mean – NathanOliver Apr 20 '21 at 12:17
  • just recently I learned that for C++14 something similar to variable lenght arrays was considered, but it didn't make it in. The fact that some "tutorial" sites persistently present them as C++ code also adds to the confusion. – 463035818_is_not_an_ai Apr 20 '21 at 12:17
  • There certainly is a dupe somewhere. Just check all question found with query "heap stack when C++" – Jeffrey Apr 20 '21 at 12:19
  • It should also be noted that `vector` doesn't actually allocate anything. It s the allocator that does that, and those can be configured to acquire memory from wherever. – NathanOliver Apr 20 '21 at 12:25
  • As for `int arr[12]`, the allocation depends on context. If it is a definition at file scope (outside any function block) then the array is allocated statically. If it is a definition within a function, then the array has automatic storage duration. If it is a declaration of a member within a class definition (i.e. an array member of a class) then its allocation is related to how each instance of the containing class is instantiated. – Peter Apr 20 '21 at 12:42
  • @NathanOliver but it does work, maybe without the flag, and as already pointed out, sites like GFG use this a lot. I am not a fan of normal arrays over vectors though – Saarthak Sabharwal Apr 20 '21 at 13:29
  • @SaarthakSabharwal -- it works for some compilers. It is not standard C++. – Pete Becker Apr 20 '21 at 13:47

1 Answers1

1

"Normal" arrays will have a size known at compile-time, which means the compiler can (and will) make sure that there's space for them. That space might not be allocated inside the executable program but allocated at run-time (like e.g. a local variable inside a function).

The size of a vector is unknown at compile-time, and its the vectors constructor that will allocate memory (if asked to, as in the case with vector<int> ar(12);). The memory for vectors will always be allocated dynamically of the heap.

Then there's also std::array which is a C++ standard container around a compile-time array. When it comes to size and allocations it acts like a "normal" array, but since it's also a standard container object it can be used with functions and algorithms designed for those.

And to confuse matter even more, something being "static" has a special meaning in C++, so saying than an array is "statically" allocated could mean different things depending one ones viewpoint. However, "statically allocated" seems to be commonly used for things like arrays, whose memory is allocated and handled by the compiler and its generated code.

Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
  • 1
    Is the `vector` case still true for `c++20` in combination with `constexpr`? – t.niese Apr 20 '21 at 12:23
  • Another thing that I'm wondering. There are some answers here on StackOverflow and sites on the internet that - as you wrote it in your question - consider those local variables as "statically" allocated. But there are other answers and sites that differentiate between statically (known a compile-time), automatically (local variable), and dynamically allocated. [Difference between static memory allocation and dynamic memory allocation](https://stackoverflow.com/questions/8385322/difference-between-static-memory-allocation-and-dynamic-memory-allocation) (tagged with `c`). – t.niese Apr 20 '21 at 12:30
  • @t.niese I'm not sure, but if the `contexppr` constructor still uses the allocator then [the allocated memory must be deallocated in the same expression](https://en.cppreference.com/w/cpp/memory/allocator/allocate), which makes it kind of useless. – Some programmer dude Apr 20 '21 at 12:32
  • Still does not answer my question completely, in the "normal" case, maybe in C++ solely, are variable-length arrays automatically differentiated from the usual static arrays by the compiler? So `int ar[3];` will be allocated space beforehand and `int ar[n];` will get memory during runtime...? I guess marking a bold line between how objects are allocated memory makes it more confusing – Saarthak Sabharwal Apr 20 '21 at 13:40
  • @SaarthakSabharwal But variable-length arrays aren't part of C++, really. It's a non-standars and ***non-portable*** extension to the language, implemented by a single compiler. You shouldn't use them. If you want to know how variable-length arrays work in e.g. C then please post a specific question about that (but note that it will very likely be closed as a duplicate). – Some programmer dude Apr 20 '21 at 13:48