473,406 Members | 2,217 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

Copy Constructor and memory allocation

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::operator= ( 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


Jul 22 '05 #1
7 2814

"Michael" <sl***********@hotmail.com> wrote in message
news:ck**********@titan.btinternet.com...
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::operator= ( 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
Jul 22 '05 #2

"Michael" <sl***********@hotmail.com> wrote in message
news:ck**********@titan.btinternet.com...
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::operator= ( 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
Jul 22 '05 #3

"John Harrison" <jo*************@hotmail.com> wrote in message
news:2t*************@uni-berlin.de...

"Michael" <sl***********@hotmail.com> wrote in message
news:ck**********@titan.btinternet.com...
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::operator= ( 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

Jul 22 '05 #4
delete pImpl;

in the destructor :-)

Cheers guys
"Jeff Flinn" <NO****@nowhere.com> wrote in message
news:ck**********@bluegill.adi.com...

"Michael" <sl***********@hotmail.com> wrote in message
news:ck**********@titan.btinternet.com...
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::operator= ( 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

Jul 22 '05 #5
Michael wrote:

[snip]

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

http://www.netwood.net/~edwin/svmtl/
Jul 22 '05 #6
Sorry But I can't see the relevance, please explain further.
Fanks
Mike

"E. Robert Tisdale" <E.**************@jpl.nasa.gov> wrote in message
news:ck**********@nntp1.jpl.nasa.gov...
Michael wrote:

[snip]

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

http://www.netwood.net/~edwin/svmtl/

Jul 22 '05 #7

"Michael" <sl***********@hotmail.com> wrote in message
news:ck**********@titan.btinternet.com...
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
Jul 22 '05 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

26
by: Peter Olcott | last post by:
// // Is there something wrong with my syntax for the // Copy Constructor of an Array Element, or does // the C++ language not support this? // #include <stdio.h> #include <stdlib.h> ...
4
by: William Payne | last post by:
Hello, I was under the impression that if I made a class Foo and if I didn't specify a copy constructor I would get one anyway that simply assigns the member variables (and that won't work for...
14
by: MSR | last post by:
I have a couple of questions. 1. Copy Constructor. class A { private: int a1; double d1; char *ptr;
5
by: Martijn van Buul | last post by:
Hi. I'm having a peculiar problem at work. I've been googling for it, but haven't found an authorative answer. In a nutshell (Long story follows), what I'd like to know is: If I have a C++...
8
by: rKrishna | last post by:
I was trying to understand the real need for copy constructors. From literature, the main reason for redfinition of copy constructor in a program is to allow deep copying; meaning ability to make...
4
by: indrawati.yahya | last post by:
According to the FAQ, the best way to inform a class user of an error that occurs inside a constructor is to throw an exception. My question is, what happens when an object is instantiated using...
5
by: sam_cit | last post by:
Hi Everyone, I was just wondering, about the overloaded assignment operator for user defined objects. It is used to make sure that the following works properly, obj1 = obj; so the...
0
by: ecestd | last post by:
I did implement the copy constructor but still have a problem with it. It is not working. What could be wrong? #include "QueueP.h" #include <cassert // for assert #include <new // for...
12
by: Rahul | last post by:
Hi Everyone, I have the following code and i'm able to invoke the destructor explicitly but not the constructor. and i get a compile time error when i invoke the constructor, why is this so? ...
7
by: Jeffrey Barish | last post by:
(Pdb) myclass MyClass( 0, 0, 'A string', 123.45) (Pdb) copy.copy(myclass) *** TypeError: TypeError('__new__() takes at least 4 arguments (2 given)',) I see 4 arguments (actually, 5 because...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.