initialize dynamic array c++ code example
Example 1: declare dynamic array c++
int main()
{
int size;
std::cin >> size;
int *array = new int[size];
delete [] array;
return 0;
}
Example 2: resizing dynamic array c++
void resize() {
size_t newSize = size * 2;
int* newArr = new int[newSize];
memcpy( newArr, arr, size * sizeof(int) );
size = newSize;
delete [] arr;
arr = newArr;
}
Example 3: initialize an array in c++
int nums[100] = {0};
int nums[5] = {1,2,3,4,5};
Example 4: c++ delete dynamically allocated array
int length = 69;
int * numbers = new int[length];
delete[] numbers;