How long can an array be, and how can I extend that length?

Recommended Answers

All 9 Replies

An array's size is only limited to the memory of the machine running the code. If you want to make an array larger you will need to read up on dynamic arrays. If you need storage that can get very large or you need it to grow I would suggest using something from the STL. Perhaps a vector would work for you.

I know about dynamic arrays, but my compiler gives me an error whenever I use an array index above 10000. I need to know how to determine the maximum length of a dynamic array.

It is all dependent on the amount of ram int the system. Are you creating the array using the new operator or are you defining the size at compile time?

New operator, and I guess I will refine my question to how can I know if I have created too many elements? ie:

int *test;
int numtest;
test=new int[x];
if (test has overstepped my RAM's capability)
{
//do something
}

Well if you have a array of 100000 doubles it would only need about 8mb of ram so I'm not sure why you wouldn't be able to create an array of 10000 elements unless they use a lot of memory. What is the error that you are getting and what compiler/IDE are you using?

An array's size is only limited to the memory of the machine running the code.

It would be limited to the sizeof(std::size_t) See definition of the new operator

Thanks, that is what I was looking for.

Showing your code that is having this problem would be helpful.

Well if you have a array of 100000 doubles it would only need about 8mb of ram so I'm not sure why you wouldn't be able to create an array of 10000 elements unless they use a lot of memory. What is the error that you are getting and what compiler/IDE are you using?

Actually, an array 100000 doubles would take less than 1MB (800KB @ 8bytes / double).

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.