Answers for "declare a dynamic array in c++"

C++
0

c++ dynamic array

//A dynamic array is a pointer that contains multiple objects or pointers.

int size = 5; //Size of the array

/*
"new" keyword tells your computer to allocate memory
and return a pointer to that memory.
*/

int* array = new int[size];

int* array = new int[size] { 5, 6, 1, 2, 8 }; //Optional initializer

/*
Always use delete or delete[] to free a dynamic pointer.
Do not use to free a pointer or an array that was not created with 
"new" or "malloc()"
NOTE: delete frees a pointer whereas delete[] removes an array.
*/
delete[] array;
Posted by: Guest on March-28-2022

Browse Popular Code Answers by Language