1

I am trying to implement a 2D array using dynamic memory allocation. Here is my code:

#include <iostream> 
using namespace std; 

int main() 
{ 

int r, c; 
cin >> r >> c; 
int** p = new int*[r]; 

for (int i = 0; i < r; i++) 
{ 
    p[i] = new int[c]; //this line here is the marked line
} 

for (int i = 0; i < r; i++) 
{ 
    for (int j = 0;j <c; j++) 
    { cin >> p[i][j]; 
    } 
}

for (int i = 0; i < r; i++) 
{ 
    for (int j = 0;j <c; j++) 
{ 
    cout << p[i][j]<<" "; 
    } 
} 
cout<<"\n"; 
for (int i = 0; i < r; i++) 
{ 
    delete [] p[i]; 
} 
delete [] p; 
return 0; 
}

I then compiled the same code by commenting the marked line in different compilers.

VS Code with MinGW (MinGW.org GCC-6.3.0-1) -> Compiled successfully with all the wanted output.

Jdoodle and other online compilers (tried in both c++14 and c++17 latest versions) -> The program gives segmentation fault after reading the second input for array element (reads the r, c and first 2 input for the array succesfully).

Could someone please explain, IN VS CODE, how am I getting the correct output? Which memory, heap or stack is used if marked line is commented? What are the differences when the marked line is commented and when not commented? And what's the reason of Segmentation fault? Thanks.

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
  • *Could someone please explain, IN VS CODE, how am I getting the correct output?* -- The program is broken. You are asking why you can take a broken table and put items on it and everything is ok, but have another broken table, and the table just falls apart when you place the same items on it. BTW, the method you are using to create this 2D array is one of the worst ways to do this (if you have to use pointers) -- see [this answer](https://stackoverflow.com/questions/21943621/how-to-create-a-contiguous-2d-array-in-c/21944048#21944048) – PaulMcKenzie Nov 07 '21 at 08:20
  • 1
    You have a minor typo in the marked line. Please replace ````p[i] = new int[i + 1];```` with ````p[i] = new int[c];```` because you want to allocate columns for each row. ````i+1```` is the current row+1 and not the number of columns. – A M Nov 07 '21 at 08:41
  • Hi @ArminMontigny, Thanks for pointing out. Corrected it. I was actually experimenting if i can get a jagged array. But yeah my question is when 'p[i] = new int [c]' – CodeHackeRツ Nov 07 '21 at 09:46

1 Answers1

1

In general the program has undefined behavior.

In this for loop

for (int i = 0; i < r; i++) 
{ 
    p[i] = new int[i + 1]; //this line here is the marked line
} 

in each "row" of the array you allocated i + 1 elements. But in the next for loop (and in subsequent loops)

for (int i = 0; i < r; i++) 
{ 
    for (int j = 0;j <c; j++) 
    { cin >> p[i][j]; 
    } 
}

you are trying to access c elements in each row independent on how many elements actually were allocated. So if c is greater than 1 then at least in the first iteration of the loop there is an attempt to access a memory beyond the allocated array.

Edit: If you commented this line

p[i] = new int[c]; //this line here is the marked line

as you wrote

I then compiled the same code by commenting the marked line in different compilers.

then again the program has undefined behavior. That is you are using uninitialized pointers. It means that anything can occur.

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
  • Hi. My question is when 'p[i] = new int [c]'. I corrected it. Sorry for the typo. Thanks. – CodeHackeRツ Nov 07 '21 at 09:48
  • 1
    @CodeHackeRツ See my appended answer. – Vlad from Moscow Nov 07 '21 at 10:37
  • Ok vlad. Thanks for answering. I get the reason for seg fault. Also, what about the memory? I mean if i use the commented code, would the memory from stack or heap be used for storing the elements? Or is that also not definite? – CodeHackeRツ Nov 07 '21 at 10:41
  • @CodeHackeRツ If you will uncomment the line then the program will be correct. It accesses dynamically allocated arrays. If to comment the line then the program uses indeterminate values stored in the first allocated array pointed to by the pointer p. – Vlad from Moscow Nov 07 '21 at 10:45
  • Hey Vlad. Thanks for all the explanations. Accepted. – CodeHackeRツ Nov 07 '21 at 16:50