I have built a constructor for a matrix object. The data is being stored in an array of struct val
, which in turn hold position (in the matrix) and value. This is the code:
SparseMatrix::SparseMatrix(const int numRow, const int numCol, vector<double> fill):
Matrix(numRow,numCol)
{
_matrix = new vector<val*>(fill.size());
vector<double>::iterator itr = fill.begin();
for(signed int i=0; i<numRow; i++)
{
for(signed int j=0; j<numCol; j++, itr++)
{
if (*itr == 0)
{
continue;
}
val *temp = new val;
temp->value = *itr;
temp->x = i;
temp->y = j;
_matrix->push_back(temp);
cout << "Psition: " << ": " << _matrix->front()->x << //<--ERROR
cout << " " << _matrix->back()->y << endl;
}
}
}
You'll notice that I've added cout just to verify that push_back does not really work for me. _matrix is on the heap and so does all the structs that are being held in it. All created using 'new'. I fail to see why this doesn't work. One line after I push a new struct pointer to the vector I can't read it (segmentation fault as I said).
Any ideas? thanks!
EDIT: Sorry, this is valgrind's message:
==13334== Invalid read of size 4
==13334== at 0x804AEAE: SparseMatrix::SparseMatrix(int, int, std::vector<double, std::allocator<double> >) (in /home/yotamoo/workspace/ex3/main)
==13334== by 0x8048C10: main (in /home/yotamoo/workspace/ex3/main)
==13334== Address 0x8 is not stack'd, malloc'd or (recently) free'd
==13334==
==13334==
==13334== Process terminating with default action of signal 11 (SIGSEGV)
==13334== Access not within mapped region at address 0x8
==13334== at 0x804AEAE: SparseMatrix::SparseMatrix(int, int, std::vector<double, std::allocator<double> >) (in /home/yotamoo/workspace/ex3/main)
==13334== by 0x8048C10: main (in /home/yotamoo/workspace/ex3/main)
==13334== If you believe this happened as a result of a stack
==13334== overflow in your program's main thread (unlikely but
==13334== possible), you can try to increase the size of the
==13334== main thread stack using the --main-stacksize= flag.
==13334== The main thread stack size used in this run was 8388608.
And - segmentation fault occurs during the first iteration!