c access array in heap code example
Example 1: heap allocated array in c ++
// Heap allocated array in c++
//using std::vector
#include <vector>
std::vector<myarray> bestArray(100); //A vector is a dynamic array, which (by default) allocates elements from the heap
//or managing memory yourself
myarray* heapArray = new myarray[100];
delete [] heapArray; // when you're done
//How to use vector to put elements inside the array.
// 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));
Example 2: heap allocated array in c ++
//Heap array
std::array<myarray, 3> stack_array; // Size must be declared explicitly.VLAs
//Stack array
std::vector<myarray> heap_array (3); // Size is optional.