abhishek kaushik
abhishek kaushik

Reputation: 13

Create array upto 10^12

I tried to create an array with size upto 10^12 elements in c++. But I can only make array upto 1000001 size. i.e

long long int dp[1000001]

But I want to store data upto 10^12 values in the array. Any Idea how can I implement this in C++ ?

Upvotes: 0

Views: 436

Answers (1)

eerorika
eerorika

Reputation: 238361

First, you must realize that the size of that array is nearly 8 TB. Does your computer have that much memory? Probably not. In such case, you cannot store that much data in memory, and practically cannot have such a large array.

Any Idea how can I implement this

Instead of an array in memory, you could store the data in the file system... Assuming you have 8 TB free storage. You can use a paging mechanism to read and write small pieces of the file at a time.

The simplest way to implement that in C++ is to use operating system functionality to map the file into the memory. That way the operating system takes care of the paging. There is no standard way to map files into memory in C++, so first step is to figure out what operating system you're using. POSIX standard specifies mmap function for this purpose.


Before doing that however, I recommend considering whether you actually need to store that much data. Perhaps you need a smarter algorithm instead.

Upvotes: 3

Related Questions