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.

10
  • 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. Commented Apr 20, 2021 at 12:14
  • 1
    for the last part of the question see here: Why aren't variable-length arrays part of the C++ standard? Commented Apr 20, 2021 at 12:14
  • 1
    char ar[n]; is a variable-length array and those aren't actually part of C++. Commented Apr 20, 2021 at 12:15
  • Good read: stackoverflow.com/questions/21350478/… Commented Apr 20, 2021 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. Commented Apr 20, 2021 at 12:17

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

5 Comments

Is the vector case still true for c++20 in combination with constexpr?
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 (tagged with c).
@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, which makes it kind of useless.
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
@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).

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.