Consider the following code:
class myarray { int i; public: myarray(int a) : i(a){ } }
How can you create an array of objects of myarray on the stack and how can you create an array of objects on the heap?
Storage of Arrays As discussed, the reference types in Java are stored in heap area. Since arrays are reference types (we can create them using the new keyword) these are also stored in heap area.
Creating an array in the heapint* A = new int[25]; allocates a new array of 25 ints and stores a pointer to the first one into variable A. double* B = new double[n]; allocates an array of 50 doubles.
The following code creates an object on the stack: Object o; When creating an object on the heap we can use: Object* o; o = new Object();
Whenever an object is created, it's always stored in the Heap space and stack memory contains the reference to it. Stack memory only contains local primitive variables and reference variables to objects in heap space.
You can create an array of objects on the stack† via:
myarray stackArray[100]; // 100 objects
And on the heap† (or "freestore"):
myarray* heapArray = new myarray[100]; delete [] heapArray; // when you're done
But it's best not manage memory yourself. Instead, use a std::vector:
#include <vector> std::vector<myarray> bestArray(100);
A vector is a dynamic array, which (by default) allocates elements from the heap.††
Because your class has no default constructor, to create it on the stack you need to let the compiler know what to pass into the constructor:
myarray stackArray[3] = { 1, 2, 3 };
Or with a vector:
// C++11: std::vector<myarray> bestArray{ 1, 2, 3 }; // C++03: std::vector<myarray> bestArray; bestArray.push_back(myarray(1)); bestArray.push_back(myarray(2)); bestArray.push_back(myarray(3));
Of course, you could always give it a default constructor:
class myarray { int i; public: myarray(int a = 0) : i(a) {} };
† For the pedants: C++ doesn't really have a "stack" or "heap"/"freestore". What we have is "automatic storage" and "dynamic storage" duration. In practice, this aligns itself with stack allocation and heap allocation.
†† If you want "dynamic" allocation from the stack, you'd need to define a max size (stack storage is known ahead of time), and then give vector a new allocator so it uses the stack instead.
Since C++11 std::array<T,size>
is available for arrays allocated on the stack. It wraps T[size]
providing the interface of std::vector
, but the most of the methods are constexpr
. The downside here is that you never know when you overflow the stack.
std::array<myarray, 3> stack_array; // Size must be declared explicitly.VLAs
For arrays allocated with heap memory use std::vector<T>
. Unless you specify a custom allocator the standard implementation will use heap memory to allocate the array members.
std::vector<myarray> heap_array (3); // Size is optional.
Note that in both cases a default constructor is required to initialize the array, so you must define
myarray::myarray() { ... }
There are also options to use C's VLAs or C++'s new
, but you should refrain from using them as much as possible, because their usage makes the code prone to segmentation faults and memory leaks.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With