Why do I see different behavior between arrays allocated on the heap and the stack?

I test the behavior of two 2D arrays in C ++, one selected from the stack, and one selected from the heap.

I create two, 2D arrays of the same shape and fill these arrays with some data. Then I try to read the arrays in two different ways: the first - with a simple array index "Arr [ROW] [COLUMN]". Then I read the arrays using pointer dereferencing, and I get two different results for the array allocated by the heap, but identical results for the selected array stack. I am trying to understand why the results are different. I would appreciate any clarification anyone could provide. Thanks in advance.

The code that I run is below:

#include <iostream>

using namespace std;

int main(){

    int rows = 6;
    int columns = 3;

    // allocate from the stack.
    double q[rows][columns];

    // allocate from the heap.
    double ** a;
    a = new double*[rows];

    for(int i = 0; i < rows; ++i){
        a[i] = new double[columns];
    }

    // populate the arrays.
    for(int i = 0; i < 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 the heap allocated array using array indexing.
    for(int i = 0; i < rows; ++i){
        for(int j = 0; j < columns; ++j){
            cout << a[i][j] << '\t';
        }
        cout << endl;
    }

    cout << "*****************" << endl;

    // print the stack allocated array using array indexing.
    for(int i = 0; i < 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 the heap allocated array.
    for(int i = 0; i < rows; ++i){
        for(int j = 0; j < columns; ++j){
            cout << *(&a[0][0] + columns*i + j) << '\t';
        }
        cout << endl;
    }

    cout << "*****************" << endl;

    // print the stack allocated array.
    for(int i = 0; i < rows; ++i){
        for(int j = 0; j < columns; ++j){
            cout << *(&q[0][0] + columns*i + j) << '\t';
        }
        cout << endl;
    }
    cout << "*****************" << endl;

    // release the memory allocated to the heap.
    for(int i = 0; i < rows; ++i){
        delete[] a[i];
    }

    delete a;

    return 0;
}

And the results that I get are as follows:

*****************
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
*****************

, , , , .

.

+4
3

&q[0][0] rows x columns. &a[0][0] , , columns ( a[0] = new double[columns];, ?). columns*i + j Undefined Behavior.

+11

. q[1][0] q[0][columns-1]. . , undefined .

+3

. , . , . , , , , , , , .

, , [], :

double * q = new [rows * columns];

, 2- .

Also, in your original post, you are using the wrong delete type for a, which is also an array and should be deleted as one (after [] after it.)

+2
source

Source: https://habr.com/ru/post/1685633/


All Articles