shreyak jain
shreyak jain

Reputation: 61

Size of array exceeds maximum object size in CPP

Recently, i'd faced this error while solving a variation problem of knapsack. Can anyone please suggest some way to get rid of it.

const int n=105, w= 1000000005;
int val[n], ans[n][w];
int weight[n];

Error: size '4000000020' of array 'ans' exceeds maximum object size '2147483647'.

Upvotes: 0

Views: 1925

Answers (2)

eerorika
eerorika

Reputation: 238351

Can anyone please suggest some way to get rid of it.

  • Option 1: Make the array smaller so that it doesn't exceed the limit.
  • Option 2: Target a system with a higher limit.
  • Option 3: Allocate the array dynamically. A simple way to do that is to use std::vector.

Upvotes: 0

D-RAJ
D-RAJ

Reputation: 3372

What your doing there is creating Tera Bytes worth of memory allocations. Usually you do those type of allocations in the heap (using vectors like std::vector<int> val(n); and std::vector<std::vector<int>> ans(n, std::vector<int>(w));). But still, are you sure you need that much of memory? After all your talking about as much as 105,000,000,525 integers.

Upvotes: 3

Related Questions