how to declar a very large 2d array

declaring large 2d arrays

int** ary = new int*[N];
for(int i = 0; i < N; ++i)
{ ary[i] = new int[N];
}

question is what do you mean by “very large” and also why do you need it…

I think you had trouble with tht ROWCOLOP in FEB COOK…There was no need of having arr[314159][314159] .
You were asking 314159 times 314159 times 4 chunk of CONTIGOUS MEMORY… i.e 370 TB…WOH MANN LOL!!!(Correct me If I am wrong)

In C or C++ local var/objects are usually allocated on the stack. If you are allocating a large array on the stack, more than the stack can handle, so you are getting a stackoverflow RTE. Don’t allocate it local on stack, use some other place instead. This can be achieved by either making the object global or allocating it on the global heap. Global variables are fine, if you don’t use the from any other compilation unit. To make sure this doesn’t happen by accident, add a static storage specifier, otherwise just use the heap.

1.This will allocate in the BSS segment, which is a part of the heap:
static int c[1000000]; int main() { cout << “I love Coding n”; return 0; }

2.This will allocate in the DATA segment, which is a part of the heap too:
int c[1000000] = {}; int main() { cout << “Coding is Fun n”; return 0; }

3.This will allocate at some unspecified location in the heap:
int main() { int* c = new int[1000000]; cout << “Coding is Really Fun n”; return 0; }

So next time you wanna use hell lot of memory use this approach…and remember AdHoc prbs always have some tricks…

Hope it helped…

HAPPY CODING :slight_smile:

1 Like