Jump to content

  • Log In with Google      Sign In   
  • Create Account

Copy Constructor/Memory Allocation


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
5 replies to this topic

#1 magal   Members   -  Reputation: 122

Like
Likes
Like

Posted 08 September 1999 - 11:20 AM

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???


Sponsor:

#2 TANSTAAFL   Moderators   -  Reputation: 1152

Like
Likes
Like

Posted 07 September 1999 - 06:28 PM

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.


#3 TANSTAAFL   Moderators   -  Reputation: 1152

Like
Likes
Like

Posted 07 September 1999 - 06:31 PM

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.


#4 magal   Members   -  Reputation: 122

Like
Likes
Like

Posted 08 September 1999 - 10:08 AM

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...


#5 Anonymous Poster_Anonymous Poster_*   Guests   -  Reputation:

Likes

Posted 08 September 1999 - 10:23 AM

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]



#6 mason   Members   -  Reputation: 128

Like
Likes
Like

Posted 08 September 1999 - 11:20 AM

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





Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS