Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Copy Constructor and memory allocation

Reply
Thread Tools

Copy Constructor and memory allocation

 
 
Michael
Guest
Posts: n/a
 
      10-13-2004
Hi,
I'm writing a vector class:

//mv3.h

class mv3Impl;

class mv3
{
public:
mv3();
mv3(mv3&);
mv3& operator= ( const mv3& RHS);

private:
mv3Impl* pImpl;
};



//mv3.cpp

class mv3Impl
{
public:
float x,y,z;
};

mv3::mv3()
{
pImpl = new mv3Impl;
pImpl->x = pImpl->y = pImpl->z = 1.0f;
}

mv3::mv3(mv3& v)
{
pImpl = new mv3Impl;
*pImpl = *(v->pImpl);
}

mv3& mv3:perator= ( const mv3& RHS)
{
pImpl = new mv3Impl; /* <----------------Do I need this line??
*/
*pImpl = *(RHS->pImpl);

}

Do i need the line that allocates memory or will this create a memory leak?
What functions are called in:

mv3 a;
mv3 b = a;

??
Thanks

Mike




 
Reply With Quote
 
 
 
 
John Harrison
Guest
Posts: n/a
 
      10-13-2004

"Michael" <(E-Mail Removed)> wrote in message
news:ckk0l4$i6s$(E-Mail Removed)...
> Hi,
> I'm writing a vector class:
>
> //mv3.h
>
> class mv3Impl;
>
> class mv3
> {
> public:
> mv3();
> mv3(mv3&);


Should be

mv3(const mv3&);

> mv3& operator= ( const mv3& RHS);
>
> private:
> mv3Impl* pImpl;
> };
>
>
>
> //mv3.cpp
>
> class mv3Impl
> {
> public:
> float x,y,z;
> };
>
> mv3::mv3()
> {
> pImpl = new mv3Impl;
> pImpl->x = pImpl->y = pImpl->z = 1.0f;
> }
>
> mv3::mv3(mv3& v)
> {
> pImpl = new mv3Impl;
> *pImpl = *(v->pImpl);
> }
>
> mv3& mv3:perator= ( const mv3& RHS)
> {
> pImpl = new mv3Impl; /* <----------------Do I need this line??
> */


No. You are assigning to an already existing object, so it will already have
allocated some memory.

> *pImpl = *(RHS->pImpl);
>
> }
>
> Do i need the line that allocates memory or will this create a memory
> leak?


A memory leak.

> What functions are called in:
>
> mv3 a;
> mv3 b = a;
>


First line - default constructor
Second line - copy constructor

John


 
Reply With Quote
 
 
 
 
Jeff Flinn
Guest
Posts: n/a
 
      10-13-2004

"Michael" <(E-Mail Removed)> wrote in message
news:ckk0l4$i6s$(E-Mail Removed)...
> Hi,
> I'm writing a vector class:
>
> //mv3.h
>
> class mv3Impl;
>
> class mv3
> {
> public:
> mv3();
> mv3(mv3&);
> mv3& operator= ( const mv3& RHS);
>
> private:
> mv3Impl* pImpl;
> };
>
>
>
> //mv3.cpp
>
> class mv3Impl
> {
> public:
> float x,y,z;
> };
>
> mv3::mv3()
> {
> pImpl = new mv3Impl;
> pImpl->x = pImpl->y = pImpl->z = 1.0f;
> }
>
> mv3::mv3(mv3& v)
> {
> pImpl = new mv3Impl;
> *pImpl = *(v->pImpl);
> }
>
> mv3& mv3:perator= ( const mv3& RHS)
> {
> pImpl = new mv3Impl; /* <----------------Do I need this line??
> */
> *pImpl = *(RHS->pImpl);
>
> }
>
> Do i need the line that allocates memory or will this create a memory

leak?

Yes, In addition to the already existing memory leak(s). Think about it.

Jeff F


 
Reply With Quote
 
Jeff Flinn
Guest
Posts: n/a
 
      10-13-2004

"John Harrison" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
>
> "Michael" <(E-Mail Removed)> wrote in message
> news:ckk0l4$i6s$(E-Mail Removed)...
> > Hi,
> > I'm writing a vector class:
> >
> > //mv3.h
> >
> > class mv3Impl;
> >
> > class mv3
> > {
> > public:
> > mv3();
> > mv3(mv3&);

>
> Should be
>
> mv3(const mv3&);
>
> > mv3& operator= ( const mv3& RHS);
> >
> > private:
> > mv3Impl* pImpl;
> > };
> >
> >
> >
> > //mv3.cpp
> >
> > class mv3Impl
> > {
> > public:
> > float x,y,z;
> > };
> >
> > mv3::mv3()
> > {
> > pImpl = new mv3Impl;
> > pImpl->x = pImpl->y = pImpl->z = 1.0f;
> > }
> >
> > mv3::mv3(mv3& v)


Also here:

> Should be
>
> mv3(const mv3&);



> > {
> > pImpl = new mv3Impl;
> > *pImpl = *(v->pImpl);
> > }
> >
> > mv3& mv3:perator= ( const mv3& RHS)
> > {
> > pImpl = new mv3Impl; /* <----------------Do I need this

line??
> > */

>
> No. You are assigning to an already existing object, so it will already

have
> allocated some memory.
>
> > *pImpl = *(RHS->pImpl);
> >
> > }
> >
> > Do i need the line that allocates memory or will this create a memory
> > leak?

>
> A memory leak.
>
> > What functions are called in:
> >
> > mv3 a;
> > mv3 b = a;
> >

>
> First line - default constructor
> Second line - copy constructor
>
> John
>
>



 
Reply With Quote
 
E. Robert Tisdale
Guest
Posts: n/a
 
      10-13-2004
Michael wrote:

[snip]

Take a look at
The C++ Scalar, Vector, Matrix and Tensor class library

http://www.netwood.net/~edwin/svmtl/
 
Reply With Quote
 
Michael
Guest
Posts: n/a
 
      10-13-2004
delete pImpl;

in the destructor

Cheers guys


"Jeff Flinn" <(E-Mail Removed)> wrote in message
news:ckk1le$9bt$(E-Mail Removed)...
>
> "Michael" <(E-Mail Removed)> wrote in message
> news:ckk0l4$i6s$(E-Mail Removed)...
> > Hi,
> > I'm writing a vector class:
> >
> > //mv3.h
> >
> > class mv3Impl;
> >
> > class mv3
> > {
> > public:
> > mv3();
> > mv3(mv3&);
> > mv3& operator= ( const mv3& RHS);
> >
> > private:
> > mv3Impl* pImpl;
> > };
> >
> >
> >
> > //mv3.cpp
> >
> > class mv3Impl
> > {
> > public:
> > float x,y,z;
> > };
> >
> > mv3::mv3()
> > {
> > pImpl = new mv3Impl;
> > pImpl->x = pImpl->y = pImpl->z = 1.0f;
> > }
> >
> > mv3::mv3(mv3& v)
> > {
> > pImpl = new mv3Impl;
> > *pImpl = *(v->pImpl);
> > }
> >
> > mv3& mv3:perator= ( const mv3& RHS)
> > {
> > pImpl = new mv3Impl; /* <----------------Do I need this

line??
> > */
> > *pImpl = *(RHS->pImpl);
> >
> > }
> >
> > Do i need the line that allocates memory or will this create a memory

> leak?
>
> Yes, In addition to the already existing memory leak(s). Think about it.
>
> Jeff F
>
>



 
Reply With Quote
 
Michael
Guest
Posts: n/a
 
      10-13-2004
Sorry But I can't see the relevance, please explain further.
Fanks
Mike

"E. Robert Tisdale" <(E-Mail Removed)> wrote in message
news:ckk4pu$fo4$(E-Mail Removed)...
> Michael wrote:
>
> [snip]
>
> Take a look at
> The C++ Scalar, Vector, Matrix and Tensor class library
>
> http://www.netwood.net/~edwin/svmtl/



 
Reply With Quote
 
John Harrison
Guest
Posts: n/a
 
      10-14-2004

"Michael" <(E-Mail Removed)> wrote in message
news:ckkd5d$8oq$(E-Mail Removed)...
> Sorry But I can't see the relevance, please explain further.
> Fanks
> Mike
>


More or less any post that is about vectors and the like, Robert advertises
his vector library.

john


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
template copy constructor vs normal copy constructor cinsk C++ 35 10-10-2010 11:14 PM
A constructor calling another constructor (default constructor)? Generic Usenet Account C++ 10 11-28-2007 04:12 AM
static memory allocation versus dynamic memory allocation Ken C Programming 24 11-30-2006 12:37 AM
What is the difference between dynamic memory allocation,and stack allocation ? chris C++ 6 10-28-2005 05:27 AM
Copy constructor hides default constructor Aire C++ 3 01-25-2004 05:47 PM



Advertisments