Quote:
Originally Posted by Sergei Steshenko
where do you allocate space for the array that is supposed to store matrix values ?
|
That is the bug, but I'm not sure Sergei explained it well enough for sys7em.
The following code to allocate space is only in matrix::input().
Code:
A = new int *[x];
for (k = 0; k < x; k++)
A[k] = new int [y];
You've kind of painted yourself into a corner with the overall design.
When you construct the object, you don't allocate the space, because for most of the objects you don't know what space to allocate at construction time.
When you copy the object, you don't allocate space for the copy nor duplicate the underlying data. Instead you just copy the pointer. That would work for simple examples, but means you won't know when to deallocate the space.
The actual crash, as Sergei described, happens when you try to use the space that should have been allocated for the object temp in matrix::operator*
But I don't see any clean way to correct it, because the implicit operator=() would still be losing ownership info. The implicit destructor would still be leaking memory and simply correcting that leak would cause crashes elsewhere.
You need to make an overall plan for allocating and deallocating the memory. To do that decently would involve duplicating a significant fraction of std::vector, which begs the question of why you didn't simply use std::vector