Why Garbage value in 2D array

This is the geeksforgeeks question why 2D array initialized with 0 is having garbage values in it.
question link: https://practice.geeksforgeeks.org/problems/longest-palindrome-in-a-string2235/1#comment-4857578508

code link: https://ide.codingblocks.com/s/304977

@gaurav19063
The problem is with your array declaration. You are trying a mix of static and dynamic allocation and achieving neither.
Either declare it statically by providing a fixed constant integer values
bool dp[100][100] = {0}

or declare it dynamically using pointer , or simply use a vector.
vector<vector> dp(l, vector(l,0));

In the method that you have used, you pass l as a value for static allocation. This creates problem since value of l is not known at compile time but the allocation is done at compile time only. So compiler can pick any random value for l and allocate according to that.

1 Like

I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.

On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.