Copy Constructor and memory allocation

Discussion in 'C++' started by Michael, Oct 13, 2004.

  1. Michael

    Michael Guest

    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::eek: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
     
    Michael, Oct 13, 2004
    #1
    1. Advertising

  2. "Michael" <> wrote in message
    news:ckk0l4$i6s$...
    > 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::eek: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
     
    John Harrison, Oct 13, 2004
    #2
    1. Advertising

  3. Michael

    Jeff Flinn Guest

    "Michael" <> wrote in message
    news:ckk0l4$i6s$...
    > 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::eek: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
     
    Jeff Flinn, Oct 13, 2004
    #3
  4. Michael

    Jeff Flinn Guest

    "John Harrison" <> wrote in message
    news:...
    >
    > "Michael" <> wrote in message
    > news:ckk0l4$i6s$...
    > > 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::eek: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
    >
    >
     
    Jeff Flinn, Oct 13, 2004
    #4
  5. E. Robert Tisdale, Oct 13, 2004
    #5
  6. Michael

    Michael Guest

    delete pImpl;

    in the destructor :)

    Cheers guys


    "Jeff Flinn" <> wrote in message
    news:ckk1le$9bt$...
    >
    > "Michael" <> wrote in message
    > news:ckk0l4$i6s$...
    > > 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::eek: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
    >
    >
     
    Michael, Oct 13, 2004
    #6
  7. Michael

    Michael Guest

    Sorry But I can't see the relevance, please explain further.
    Fanks
    Mike

    "E. Robert Tisdale" <> wrote in message
    news:ckk4pu$fo4$...
    > Michael wrote:
    >
    > [snip]
    >
    > Take a look at
    > The C++ Scalar, Vector, Matrix and Tensor class library
    >
    > http://www.netwood.net/~edwin/svmtl/
     
    Michael, Oct 14, 2004
    #7
  8. "Michael" <> wrote in message
    news:ckkd5d$8oq$...
    > 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
     
    John Harrison, Oct 14, 2004
    #8
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Aire
    Replies:
    3
    Views:
    487
    Mike Wahler
    Jan 25, 2004
  2. Ken
    Replies:
    24
    Views:
    3,947
    Ben Bacarisse
    Nov 30, 2006
  3. chris
    Replies:
    6
    Views:
    1,031
    chris
    Oct 28, 2005
  4. Generic Usenet Account
    Replies:
    10
    Views:
    2,344
  5. cinsk
    Replies:
    35
    Views:
    2,730
    James Kanze
    Oct 11, 2010
Loading...

Share This Page