简体   繁体   中英

C++ function to return a pointer to a multidimensional array

I am trying to create a function in a c++ file to create an identity matrix, and return a pointer to the first element in said matrix

I have the following code, with a void print_matrix function to display the matrices

#include <iostream>

void print_matrix(int *p, int n) {
    for (int row = 0; row < n; row++) {
        for (int col = 0; col < n; col++) {
            std::cout << *p << "\t";
            p++;
        }
        std::cout << std::endl;
    }
    std::cout << std::endl;
}

int *get_identity_matrix(int n) {
    int m[n][n];
    for (int row = 0; row < n; row++) {
        for (int col = 0; col < n; col++) {
            if (row == col) {
                m[row][col] = 1;
            } else {
                m[row][col] = 0;
            }
        }
    }
    std::cout << "Before:" << std::endl;
    std::cout << "Address of m[0][0]: " << &m[0][0] << std::endl;
    print_matrix(&m[0][0], n);
    return &m[0][0];
}

int main() {
    int n = 3;
    int *pm = get_identity_matrix(n);
    std::cout << "After:" << std::endl;
    std::cout << "Address of pm: " << pm << std::endl;
    print_matrix(pm, n);
    return 0;
}

when compiling I get the warning: (not sure if this is useful)

src/main.cpp:27:13: warning: address of stack memory associated with local variable 'm' returned [-Wreturn-stack-address]
    return &m[0][0];
            ^
1 warning generated.

When running the code, I get the following output:

Before:
Address of m[0][0]: 0x7ff7b42ff9b0
1   0   0   
0   1   0   
0   0   1   

After:
Address of pm: 0x7ff7b42ff9b0
0   32760   1   
1   197148432   1   
197144129   32760   -1271924224

TL;DR why are these matrices different when being printed? I am passing the same values for int *p, int n each time the print_matrix function is being called

I was expecting for the print_matrix function to print the same identity matrix twice

In C++, local variables go out of scope . When they do, the memory they occupy is freed. m is a local variable in get_identity_matrix ; as a result, after you return the memory address of m , the memory m occupies is freed as it goes out of scope once the function ends. Thus, you now have a pointer to a bunch of garbage memory, and when you print its values out, you get a bunch of garbage values.

Returning the memory address of a local array is generally undefined behavior. Instead, use the C++ equivalent std::array , instead of C-style arrays.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM