🎉 Celebrating 25 Years of GameDev.net! 🎉

Not many can claim 25 years on the Internet! Join us in celebrating this milestone. Learn more about our history, and thank you for being a part of our community!

Copy Constructor/Memory Allocation

Started by
4 comments, last by magal 24 years, 9 months ago
well, when making things as flexible as you are making your matrix class, you are BOUND to cause more overhead.

you pretty much HAVE to allocate a new space into which to copy over the cells of the matrix, otherwise when the original matrix object is destroyed, the new one will crash, since the memory for the matrix cells would be freed.

Get off my lawn!

Advertisement
also, what is the return type from operator + and operator *?

it SHOULD be CMatrix (or whatever your matrix class is called) rather than CMatrix& or CMatrix*.

then you return the temp matrix on the stack (which is more overhead, but cant be helped)

however, if for some reason this doesnt work, you may have to just overload the += and *= operators.

Get off my lawn!

Thanks for the replies so far.
Now I started to test the class and, when I only compile the class (source and header, with implementation and declaration respectively), it is fine, no errors are found.

But when I add a void main() at the end of the file and put the line Matrix m(2) to declare a square matrix of integers with 2 rows and 2 columns, it says that the allocation line '_pMatrix = new T[_pNRows][_pNCols];' is using a non-constant boundary for the array, and won't compile. Now, how can that be??? (_pMatrix is declared as a protected member 'T * _pMatrix;' and T is the template class.
Please guys, help me out again...

As I recall a new statement of an array of arrays like you have requires that one of the sizes be constant... To get around this I believe you have to use single subscript arrays

blah = new T[_pNRows*_pnCols]

And then access it like this

blah[_pnCols*row + col]

I'm having a problem with the Matrix class that I'm creating. The class is a matrix of any numeric type (so I used 'template' before the declaration of the class and it contains a protected member T * _pMatrix. In the constructor, I allocate the memory for this pointer using the 'new' operator.

But now I'm starting to overload the operators like +, * to implement all the ops with matrices. When I use a statement like A = B + C (all these variables are matrices), the values from B and C are summed and put on a new temp matrix which is returned from the overloaded operator function. And it happens that the default copy constructor copies the POINTER _pMatrix, making two different and independent pointers point to the same resource. Now, after the temp matrix with the summed values is returned, it runs out of scope and its destructor (which deallocates the resource) is called. And then the A matrix contains a _pMatrix member pointing to a deleted resource!!!

All I could think of was to implement a new copy constructor that would copy all non-pointer members of the source class to the destination class, allocate memory for the destination class' _pMatrix member and copy the entire contents from the source _pMatrix. Then, I have two different pointers pointing to two different resources, but both resources are identical.
If I'm using a 4x4 matrix of double, imagine the memory it was going to use!!!! It would take a lot of resources from the heap and cycles from the processor to do all this!!!!

Isn't there any other viable solution to copy the source object and its allocated resource to the destination???

or, you could make a CMatrixArray class, which would take care of newing / deleting the whole slew of them, and could also do the array indexing stuff:

CMatrix *CMatrixArray::get(int x, int y)
{
return(&m_array[(y*m_dimx)+x])
}

or something. Not sure if it'd make sense in your situation, just thought I'd mention it as a wild thought.

Mason McCuskey
Spin Studios
http://www.spin-studios.com

Founder, Cuttlefish Industries
The Cuttlefish Engine lets anyone develop great games for iPad, iPhone, Android, WP7, the web, and more!

This topic is closed to new replies.

Advertisement