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