I am learning C++ and I am running in a problem that I don't know the reason for. I have created a vector and matrix class that are light wrappers around a C array that manage a T* array on the heap.
The Matrix class has a function:
// in NBDMatrix.hpp class definition
void makeXMeshgridv1(NBDVector<T> v){ // takes a value
for(unsigned int j=0; j<ny; j++){
for(unsigned int i=0; i<nx; i++)
this->at(i,j) = v.at(i);
}
}
void makeXMeshgridv2(NBDVector<T>& v){ // takes a reference
for(unsigned int j=0; j<ny; j++){
for(unsigned int i=0; i<nx; i++)
this->at(i,j) = v.at(i);
}
}
in main()
NBDVector<float> vec1 = NBDVector<float>(0.0f, 12.6f, 4);
NBDVector<float> vec2 = NBDVector<float>(0.0f, 12.6f, 4);
NBDMatrix<float> mat1(4,8);
NBDMatrix<float> mat2(4,8);
mat1.makeXMeshgridv1(vec1); // causes problem at destruction
mat2.makeXMeshgridv2(vec2); // no problem at destruction
If I use makeXMeshgridv1() I get
malloc: *** error for object 0x100604c50: pointer being freed was not allocated at destruction
but when I use makeXMeshgridv2 everything goes well.
I would like to understand what is the problem when using makeXMeshgridv1().