Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does std::array<> guarantee allocation on the stack only?

Is std::array<int,10> (without myself using new) guaranteed to be allocated in the stack rather then the heap by the C++-Standard?

To be clear, I do not mean new std::array<int, 10>. I mainly wonder, if the standard library is allowed to use new inside its implementation.

like image 509
towi Avatar asked Sep 17 '16 15:09

towi


People also ask

Is std :: array allocated on the stack?

If you create an object with a C-style array or std::array member in a local variable, it's on the stack, with the array inside of it. If you create the object with new, it's on the heap, with the array also inside of it.

Can arrays be stored on the stack?

Yes, the whole array is pushed on stack.

Is std::vector on heap or stack?

std::vector typically allocates memory on the heap (unless you override this behavior with your own allocator). The std::vector class abstracts memory management, as it grows and shrinks automatically if elements are added or removed.

Are arrays allocated on the heap?

Dynamically allocated arrays are allocated on the heap at run time. The heap space can be assigned to global or local pointer variables that store the address of the allocated heap space (point to the first bucket).


2 Answers

TL;DR: yes, it is on the stack.


The longer story:

C++ has no concept of stack or heap. Those are implementation details, and there is at least one platform that does not use a traditional stack (but rather linked list of heap allocations for it).

It has automatic storage and the free store. new accesses the free store, and variables "on the stack" go into automatic storage.

In practice, in order to allocate things on the free store, you have to risk an out of memory exception. So the general rule is things that guarantee they do not throw must be using automatic storage. array makes this guarantee (except whatever is in it can throw, naturally). It is also an aggregate of plain old data, effectively forced to look like:

template<class T,std::size_t N> struct array {   T __no_fixed_name__[N];   // non-constructor/destructor methods omitted as they are noise at this point }; 

In theory it could be implemented by the compiler via magic that is not actual C++, but there is no need for that, so nobody bothers.

So in conclusion: yes, std::array is on the stack.

like image 200
Yakk - Adam Nevraumont Avatar answered Sep 21 '22 06:09

Yakk - Adam Nevraumont


I could not find more explicit answer in the standard, but [array.overview]/2:

An array is an aggregate ([dcl.init.aggr]) that can be list-initialized with up to N elements whose types are convertible to T.

And [dcl.init.aggr]/1:

An aggregate is an array or a class (Clause [class]) with

  • no user-provided, explicit, or inherited constructors ([class.ctor]),

...

That about covers it. No way an aggregate could allocate memory dynamically (or perhaps, do anything at all at its own during the construction). There's only an implicitly-declared trivial constructor.

Of course, if you new std::array<...>, you get an array on "the heap".


Some may be more satisfied by what we can get on cppreference:

std::array is a container that encapsulates fixed size arrays.

This container is an aggregate type with the same semantics as a struct holding a C-style array T[N] as its only non-static data member.


Thirdly, std::array was introduced in C++11. Why? For example, to complement std::vector in some ways, like usage in constexpr functions, where dynamic allocation is not allowed.

like image 31
LogicStuff Avatar answered Sep 23 '22 06:09

LogicStuff