Problem
In my code I want to read several basis and modify them in a certain way. I want to do this in a structured manner to be sure that I do the same for every basis. I have created the function ReadBasis for this. Unfortunately if I read a basis using this function, I get a seg fault uppon using the basis outside the function. This might be scope related. I have created an example script below.
The returned pointer to a basis in Matrix* BasisReader::getSpatialBasis(double time) is created in the function itself.
I think that moving out of the function ReadBasis makes you move out of scope of this pointer and therefore it does not exist outside ReadBasis, but I am not sure about this.
Possible solutions
1) Make the pointer Matrix* spatial_basis_vectors a member variable of BasisReader.
2) Use the pointer Matrix* spatial_basis_vectors as an input of the function such that Matrix* BasisReader::getSpatialBasis( double time, Matrix* spatial_basis_vectors)
3) Others?
Do you agree that this is undesired behavior? If so, is there a preference for the solution?
Minimal example
void ReadBasis(CAROM::BasisReader *reader, const CAROM::Matrix* basis)
{
basis = reader->getSpatialBasis(0.0);
// This works
printf("Basis dim. %d x %d\n", basis->numRows(), basis->numColumns());
// This works
basis->print("print_basis_in_scope");
}
int main()
{
const CAROM::Matrix* V; // Spatial basis
CAROM::BasisReader *reader;
reader = new CAROM::BasisReader(basisName);
ReadBasis(reader, V);
// This has gives a seg fault
V->print("print_basis_out_scope");
delete reader;
delete V;
}
Problem In my code I want to read several basis and modify them in a certain way. I want to do this in a structured manner to be sure that I do the same for every basis. I have created the function
ReadBasis
for this. Unfortunately if I read a basis using this function, I get a seg fault uppon using the basis outside the function. This might be scope related. I have created an example script below.The returned pointer to a basis in
Matrix* BasisReader::getSpatialBasis(double time)
is created in the function itself.I think that moving out of the function
ReadBasis
makes you move out of scope of this pointer and therefore it does not exist outsideReadBasis
, but I am not sure about this.Possible solutions 1) Make the pointer
Matrix* spatial_basis_vectors
a member variable ofBasisReader
. 2) Use the pointerMatrix* spatial_basis_vectors
as an input of the function such thatMatrix* BasisReader::getSpatialBasis( double time, Matrix* spatial_basis_vectors)
3) Others?Do you agree that this is undesired behavior? If so, is there a preference for the solution?
Minimal example