user506938
user506938

Reputation: 39

How to create an array with size more than C++ limits

I have a little problem here, i write c++ code to create an array but when i want to set array size to 100,000,000 or more i got an error.

this is my code:

int i=0;
double *a = new double[n*n];

this part is so important for my project.

Upvotes: 0

Views: 154

Answers (3)

Nicol Bolas
Nicol Bolas

Reputation: 474436

In general, the only reason that would fail would be due to lack of memory/memory fragmentation/available address space. That is, trying to allocate 800MB of memory. Granted, I have no idea why your system's virtual memory can't handle that, but maybe you allocated a bunch of other stuff. It doesn't matter.

Your alternatives are to tricks like memory-mapped files, sparse arrays, and so forth instead of an explicit C-style array.

Upvotes: 1

perreal
perreal

Reputation: 98118

If you do not have sufficient memory, you may need to use a file to store your data and process it in smaller chunks.

Don't know if IMSL provides what you are looking for, however, if you want to work on smaller chunks you might devise an algorithm that can call IMSL functions with these small chunks and later merge the results. For example, you can do matrix multiplication by combining multiplication of sub-matrices.

Upvotes: 0

fredoverflow
fredoverflow

Reputation: 263360

When you think you need an array of 100,000,000 elements, what you actually need is a different data structure that you probably have never heard of before. Maybe a hash map, or maybe a sparse matrix.

If you tell us more about the actual problem you are trying to solve, we can provide better help.

Upvotes: 1

Related Questions