I want a 2d array to take inputs upto 10^9. Rows and Columns can go upto 10^9. How shall I assign it since dynamic allocation is giving me a run-time error.
I assigned the memory dynamically but I am getting a run-time error when input is 10^5 or more. I know array will use 10^9 x 10^9 x 4 bytes of memory which my heap can't handle it, so How do i assign it.
long long int n,m,k,ans=0;
cin>>n>>m>>k;
long long int**a=new long long int*[n+2];
for(long long i=0;i<=n+2;i++){
a[i]=new long long int[m+2];
}
for(long long int i=0;i<=n+1;i++){
for(long long int j=0;j<=m+1;j++)
a[i][j]=0;
}
I expect the code to take input when rows and columns are 10^5. However I am getting following:
terminate called after throwing an instance of std::bad_alloc
10^9 even with 1 byte elements is a gigabyte, a 2D array is 10^9^2 or an exabyte. With 64bit integers that is 8 exabytes. So you need to rethink this. I assume you won't actually have data in the vast majority of that?
You could for example use an std::map
or std::unordered_map
.
unsigned char bytes[] = { 10, 55, 255 };
struct pair_hash
{
std::size_t operator() (const std::pair<int, int> &p)const
{
return std::hash<int>()(p.first) ^ std::hash<int>()(p.first); // Possibly want a better hash
}
};
std::unordered_map<std::pair<int, int>, long long, pair_hash> map;
You will need to define a hash for it. In this case you could also combine the two integers into the high and low bits, as a long long is generally large enough to hold two ints.
std::unordered_map<long long, long long> map;
There are other ways to do "sparse arrays" and can depend a lot on your data, for example do you expect "clusters" or everything evenly distributed? What is the read/write pattern?
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.