In this question
In the bottom-up approach when I am declaring matrix inside knapSack function it is giving segmentation fault and for some test cases it gives garbage value
and when declaring matrix globally it does not give any error or segmentation fault.
It is due to the stack size, I can’t say for sure what’s the limit on GeeksForGeeks, but with most judges it’s not a good idea to try to create an array with more than 2 \cdot 10^5 elements, even that is pretty tight upper bound, sometimes it’s a bit looser. E. g. 10^6 is usually possible, but in your case 10^3 \cdot 10^3 = 10^6 doesn’t seem to work. If you don’t want to have to play around with this, just keep in mind to declare all of your arrays globally. Another workaround would be to use a vector instead of a regular array, as the data it points to is always allocated on heap, so it excludes the worries about limits of array size.
It is giving same issues with vector also
Paste your code here I can’t access your code on GFG.
It shouldn’t be acting like that, unless you’ve made a mistake in code. I see no other reason, I’ve just simulated the whole thing on my machine and made sure once again that matrix of size 5000 * 5000 gives a seg fault, while a vector of vectors of the same size does not.