How memory is allocated to 2D array arr?

I kinda understood the code,but can you plz explain how memory allocation is done here to 2D array arr.
I have implemented this by preallocating the memory by defining arr[1000][1000],but i think this is good method.
Also plz explain how the array is passed to function as pointer of pointer…

A 2d array has been dynamically allocated in the code. You can check how it is done from here:

Arrays can be passed to a function using pointer.It is just another way.As you know…pointer variables store address…So here the pointer will store the base address of the array.

1 Like

thank you so much ,it cleared my doubt,one more thing to ask when an array is allocated memory dynamically ,do we have to initialize it as it will contain garbage,
like in the case of computing sum of submatrix of 2d array

Yes you should initialize it.

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.