I've encountered a situation where I gotta do some math in a function into a matrix (the dimensions of this are unknown at compile time so I gotta save it as a pointer) and then equate it to a pointer I pass as argument to the function.
The code throws a segmentation fault each time. I have a sample code here:
#include <iostream>
using namespace std;
void assign(int **a)
{
int **A = new int* [3];
int i, j;
for(i = 0; i < 3; i++)
A[i] = new int[3];
for(i = 0; i < 3; i++)
for(j = 0; j < 3; j++)
A[i][j] = 100;
a = A; /* equating the pointers */
}
int main()
{
int **ptr;
assign(ptr); /* Passing my pointer into the function */
cout << ptr[0][0] << endl;
return 0;
}
The code is in C++ and it is not an option for me to make the function have a return type other than void. And I don't want to save my matrix on a normal pointer by row/column major - I need to know why this doesn't work.