2D vector vs 2D array

Hey all , I am having a doubt I recently solved one question of LCS (“LCS Returns | HackerRank”)
0if I create a 2d int array of size 5000 X 5000 , it showing me error , while 2D vector of same size having no issues why ?

@ssjgz please help

Playground - LeetCode

AddressSanitizer:DEADLYSIGNAL
=================================================================
==33==ERROR: AddressSanitizer: stack-overflow on address 0x7ffd3029afb8 (pc 0x000000342f95 bp 0x7ffd361f9190 sp 0x7ffd3029afc0 T0)
#2 0x7f1d7ccfb0b2 (/lib/x86_64-linux-gnu/libc.so.6+0x270b2)
==33==ABORTING

if I replace

int dpleft[len1][len2];

to

vector<vector<int>> dpleft( len1 , vector<int> (len2, 0));

It is fine why ?

1 Like

I am still not getting , please summarize it , that means when I have to create large arrays we have to use vector instead of array right ?

In short: Stack size is predetermined before compilation of code. Whereas, heap size is not predetermined and can grow as per the requirements.
If we are storing large array, stack overflow might occur as they are not enough to store that much huge data. (stack memory is very less as compared to heap)

Instead of saying like this, you can say that, when we have to create large arrays, we should allocate them the heap memory rather than stack.

1 Like