c++ - Why am I seeing different behavior between arrays allocated on the heap and the stack? -


i inspecting behavior of 2 2d arrays in c++, 1 allocated stack, , 1 allocated heap.

i create two, 2d arrays of same shape, , populate arrays data. attempt read arrays in 2 different methods, first being simple array index format "arr[row][column]". read arrays using pointer dereference, , 2 different results heap allocated array, identical results stack allocated array. trying understand why results differ. appreciate clarification can provide. in advance.

the code running below:

#include <iostream>  using namespace std;  int main(){      int rows = 6;     int columns = 3;      // allocate stack.     double q[rows][columns];      // allocate heap.     double ** a;     = new double*[rows];      for(int = 0; < rows; ++i){         a[i] = new double[columns];     }      // populate arrays.     for(int = 0; < rows; ++i){         for(int j = 0; j < columns; ++j){             a[i][j] = columns*i+j;             q[i][j] = columns*i+j;         }     }      cout << "*****************" << endl;     cout << "array indexing method." << endl;     cout << "*****************" << endl;      // print heap allocated array using array indexing.     for(int = 0; < rows; ++i){         for(int j = 0; j < columns; ++j){             cout << a[i][j] << '\t';         }         cout << endl;     }      cout << "*****************" << endl;      // print stack allocated array using array indexing.     for(int = 0; < rows; ++i){         for(int j = 0; j < columns; ++j){             cout << q[i][j] << '\t';         }         cout << endl;     }      cout << "*****************" << endl;     cout << "pointer dereferencing method." << endl;     cout << "*****************" << endl;      // print heap allocated array.     for(int = 0; < rows; ++i){         for(int j = 0; j < columns; ++j){             cout << *(&a[0][0] + columns*i + j) << '\t';         }         cout << endl;     }      cout << "*****************" << endl;      // print stack allocated array.     for(int = 0; < rows; ++i){         for(int j = 0; j < columns; ++j){             cout << *(&q[0][0] + columns*i + j) << '\t';         }         cout << endl;     }     cout << "*****************" << endl;      // release memory allocated heap.     for(int = 0; < rows; ++i){         delete[] a[i];     }      delete a;      return 0; } 

and results obtain are:

***************** array indexing method. ***************** 0       1       2 3       4       5 6       7       8 9       10      11 12      13      14 15      16      17 ***************** 0       1       2 3       4       5 6       7       8 9       10      11 12      13      14 15      16      17 ***************** pointer dereferencing method. ***************** 0       1       2 0       3       4 5       0       6 7       8       0 9       10      11 0       12      13 ***************** 0       1       2 3       4       5 6       7       8 9       10      11 12      13      14 15      16      17 ***************** 

and can see in third block of output, heap allocated array isn't being read properly, stack allocated array is.

thanks again.

&q[0][0] gives pointer first double in block containing rowsxcolumns doubles. while &a[0][0] gives pointer first double in block containing columns doubles (you've allocated using a[0] = new double[columns];, remember?). accessing columns*i + j out of bounds , triggers undefined behavior.


Comments

Popular posts from this blog

ios - MKAnnotationView layer is not of expected type: MKLayer -

ZeroMQ on Windows, with Qt Creator -

unity3d - Unity SceneManager.LoadScene quits application -