Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create a dynamic array of integers

Tags:

c++

How to create a dynamic array of integers in C++ using the new keyword?

like image 358
Sudantha Avatar asked Oct 27 '10 04:10

Sudantha


People also ask

Can we create a dynamic array in C?

We can create an array of pointers also dynamically using a double pointer. Once we have an array pointers allocated dynamically, we can dynamically allocate memory and for every row like method 2.

What is dynamic array How is it created in C?

Dynamic arrays are very useful data structures. They can be initialized with variable size at runtime. This size can be modified later in the program to expand (or) shrink the array. Unlike fixed-size arrays and Variable Length Arrays, Dynamically sized arrays are allocated in the heap.


1 Answers

int main() {   int size;    std::cin >> size;    int *array = new int[size];    delete [] array;    return 0; } 

Don't forget to delete every array you allocate with new.

like image 135
Jason Iverson Avatar answered Oct 13 '22 01:10

Jason Iverson