473,396 Members | 2,052 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,396 software developers and data experts.

How do I know if memory is already allocated?

Hi

How does it work?

//This works
//Constructor is run and memory allocated for m_AnObject
CSomeClass SomeClass1;
//copying data works fine
SomeClass1 = SomeClass2;

//Oooops, constructor is not run. (Should it be?) Memory is not
//allocated for m_AnObject. Only copy constructor and assignment
//operator is run. How do I check if memory is allocated for
//m_AnObject???
SomeClass SomeClass1 = SomeClass2;

//The code
//Standard constructor
CSomeClass::CSomeClass()
{
m_AnObject = new CAnObject();
}

//Copy constructor
CSomeClass::CSomeClass(const CSomeClass& orig)
{
*this = orig;
}

//Assignment operator overriding
CSomeClass& CSomeClass::operator=(const CSomeClass& rhs)
{
if(this != &rhs)
{ //Does m_AnObject point to legal memory??
*m_AnObject = *rhs.m_AnObject;
}
return *this;
}

How do I find out if memory is already allocated?

Regards
Kjell Arne Johansen
Jul 19 '05 #1
8 2209
Kjell Arne Johansen wrote:
Hi

How does it work?

//This works
//Constructor is run and memory allocated for m_AnObject
CSomeClass SomeClass1;
//copying data works fine
SomeClass1 = SomeClass2;

//Oooops, constructor is not run. (Should it be?) Memory is not
//allocated for m_AnObject. Only copy constructor and assignment
//operator is run. How do I check if memory is allocated for
//m_AnObject???
SomeClass SomeClass1 = SomeClass2;

//The code
//Standard constructor
CSomeClass::CSomeClass()
{
m_AnObject = new CAnObject();
}

//Copy constructor
CSomeClass::CSomeClass(const CSomeClass& orig)
{
*this = orig;
}

//Assignment operator overriding
CSomeClass& CSomeClass::operator=(const CSomeClass& rhs)
{
if(this != &rhs)
{ //Does m_AnObject point to legal memory??
*m_AnObject = *rhs.m_AnObject;
}
return *this;
}

How do I find out if memory is already allocated?


Why do you think it is not? Your constructor make it sure it does. The
other functions only copy. In your code the pointer always points to a
valid memory area returned by the new CAnObject() until your destructor is
called (I hope you have one).

--
Attila aka WW
Jul 19 '05 #2
On Mon, 01 Sep 2003 09:55:04 +0000, Kjell Arne Johansen wrote:

//Copy constructor
CSomeClass::CSomeClass(const CSomeClass& orig)
{
m_AnObject = new CAnObject();
*this = orig;
}

//Assignment operator overriding
CSomeClass& CSomeClass::operator=(const CSomeClass& rhs)
{
if(this != &rhs)
{ //Does m_AnObject point to legal memory??
// By allocating memory for it in the copy constructor we can now be sure
// that it does.
*m_AnObject = *rhs.m_AnObject;
}
return *this;
}

CSomeClass::~CSomeClass()
{
delete m_AnObject;
// make sure memory is freed
}

hth
NPV
Jul 19 '05 #3
On Mon, 01 Sep 2003 13:02:49 +0300, Attila Feher wrote:
Kjell Arne Johansen wrote:


How do I find out if memory is already allocated?


Why do you think it is not? Your constructor make it sure it does. The
other functions only copy. In your code the pointer always points to a
valid memory area returned by the new CAnObject() until your destructor is
called (I hope you have one).


But the copy constructor does not allocate it's own memory, so if you
copy an object the two will share the internal object. Then when one of
them is destroyed (with a proper destructor) the other object will be left
with a dangling pointer just waiting to cause a crash.
If this is the desired behaviour the default copy constructor will do fine.

regards
NPV
Jul 19 '05 #4
Nils Petter Vaskinn wrote:
[SNIP]
But the copy constructor does not allocate it's own memory, so if you
copy an object the two will share the internal object. Then when one
of them is destroyed (with a proper destructor) the other object will
be left with a dangling pointer just waiting to cause a crash.
If this is the desired behaviour the default copy constructor will do
fine.


Hm. I have apparently missed the broken copy constructor. The OP needs to
do something like this (along the lines of his copy assignment operator):

//Copy constructor
CSomeClass::CSomeClass(const CSomeClass& orig)
{
m_AnObject = new CAnObject(*orig.m_AnObject);
}

Assuming his CAnObject type has a proper copy constructor.

--
Attila aka WW
Jul 19 '05 #5
Hi

Yes of course. Allocate memory in the copy constructor.
Thank You.

(I have a proper destructor :-)

Regards
Kjell Arne

On Mon, 01 Sep 2003 10:35:26 GMT, "Nils Petter Vaskinn"
<no@spam.for.me.invalid> wrote:
On Mon, 01 Sep 2003 09:55:04 +0000, Kjell Arne Johansen wrote:

//Copy constructor
CSomeClass::CSomeClass(const CSomeClass& orig)
{


m_AnObject = new CAnObject();
*this = orig;
}

//Assignment operator overriding
CSomeClass& CSomeClass::operator=(const CSomeClass& rhs)
{
if(this != &rhs)
{ //Does m_AnObject point to legal memory??


// By allocating memory for it in the copy constructor we can now be sure
// that it does.
*m_AnObject = *rhs.m_AnObject;
}
return *this;
}

CSomeClass::~CSomeClass()
{
delete m_AnObject;
// make sure memory is freed
}

hth
NPV


Jul 19 '05 #6
"Attila Feher" <at**********@lmf.ericsson.se> writes:
Nils Petter Vaskinn wrote:
[SNIP]
But the copy constructor does not allocate it's own memory, so if you
copy an object the two will share the internal object. Then when one
of them is destroyed (with a proper destructor) the other object will
be left with a dangling pointer just waiting to cause a crash.
If this is the desired behaviour the default copy constructor will do
fine.


Hm. I have apparently missed the broken copy constructor. The OP needs to
do something like this (along the lines of his copy assignment operator):

//Copy constructor
CSomeClass::CSomeClass(const CSomeClass& orig)
{
m_AnObject = new CAnObject(*orig.m_AnObject);
}

Assuming his CAnObject type has a proper copy constructor.


But if that is desirable why not just abandon new, the pointer, the
hassle of dynamic memory allocation, and do this:

class CSomeClass
{
//This class relies on the compiler-generated
// default constructor,
// copy constructor,
// destructor,
// and copy-assignment operator.
CAnObject m_AnObject;
public:
//... Some public interface the OP didn't show us ...
};

Why do work when the compiler will do it for you? :-)
Jul 19 '05 #7
llewelly wrote:
[SNIP]
class CSomeClass
{
//This class relies on the compiler-generated
// default constructor,
// copy constructor,
// destructor,
// and copy-assignment operator.
CAnObject m_AnObject;
public:
//... Some public interface the OP didn't show us ...
};

Why do work when the compiler will do it for you? :-)


Maybe he is using the PIMPL idiom?

--
Attila aka WW
Jul 19 '05 #8
On Tue, 2 Sep 2003 08:28:04 +0300, "Attila Feher"
<at**********@lmf.ericsson.se> wrote:
llewelly wrote:
[SNIP]
class CSomeClass
{
//This class relies on the compiler-generated
// default constructor,
// copy constructor,
// destructor,
// and copy-assignment operator.
CAnObject m_AnObject;
public:
//... Some public interface the OP didn't show us ...
};

Why do work when the compiler will do it for you? :-)


Maybe he is using the PIMPL idiom?

--
Attila aka WW

Thank you for your help. The problem is solved and everything is
fine. CSomeClass is an interface class operating on the CAnObject
methods.

Kjell Arne
Jul 19 '05 #9

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

Similar topics

8
by: Tron Thomas | last post by:
As part of applying for a programming position at a company, I recently I had submitted some code samples to one of the developers for review. This is the feedback I received: One of his...
1
by: Spur | last post by:
Hi all, I implemented a memory allocation/deallocation class that logs all new/delete calls (overloaded) and remembers for each allocated block where it was allocated from (using a macro that...
17
by: ~Gee | last post by:
Hi Folks! Please see the program below: 1 #include<iostream> 2 #include<list> 3 #include <unistd.h> 4 using namespace std; 5 int main() 6 { 7 {
22
by: xixi | last post by:
hi, we are using db2 udb v8.1 for windows, i have changed the buffer pool size to accommadate better performance, say size 200000, if i have multiple connection to the same database from...
9
by: Jim H | last post by:
In one of my functions I create a char string s, of dynamic allocated length. I want to free the memory before my function returns. Everywhere I find says free(s) is the way to do this, but I'm...
72
by: ravi | last post by:
I have a situation where i want to free the memory pointed by a pointer, only if it is not freed already. Is there a way to know whether the memory is freed or not?
74
by: ballpointpenthief | last post by:
If I have malloc()'ed a pointer and want to read from it as if it were an array, I need to know that I won't be reading past the last index. If this is a pointer to a pointer, a common technique...
26
by: Bill Reid | last post by:
Bear with me, as I am not a "professional" programmer, but I was working on part of program that reads parts of four text files into a buffer which I re-allocate the size as I read each file. I...
6
by: Mahendra | last post by:
I have two cases - 1. When I have a pointer A pointing to a heap memory - What happens when I dereference the pointer A using free(A). It deallocated the heap memory the pointer was pointing...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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.