How do i create a large array in c++?

Apologies if this has been answered already but after searching didn't find what i was looking for.

i want to create an integer array of size 1500000 i.e int array[1500000] but my compiler wont allow it. I remember seeing something about using "new" but can't remember where i saw it. As you can tell am just beginning and trying to learn online(difficult).

If anyone could point me in the right direction or suggest a link to something it would be much appreciated.

Recommended Answers

All 7 Replies

what compiler are you using?

You are not able to create a large array because there isn't enough memory in stack memory. So you have to allocate it and use heap memory like so :

int size = 10e10;
int array = new int[size];

//..utilize array

delete [] array; //release memory for later use

But usually, you might want to do something like this :

std::vector<int> array(10e10,0); //create an array of size 10e10, initialize to 0

thanks will give this a go.

to firstperson
tried your first suggestion and got two compile errors

invalid conversion from int* to int
type int argument given to delete, expected pointer

He forgot to make array a pointer int* array = new int[size];

worked a treat thanks guys

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.