473,406 Members | 2,451 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.

owernship of ying and yang objects


Experimenting with smart pointers here but need a refresher here.

Consider the snippet:

# include <iostream>
# include <string>

using namespace std;

class ying {
public:
ying () {}
~ying() { cout << " deleted ying " << endl; }
void run_it()
{ cout << typeid(*this).name() << endl; }

ying& operator=(const ying& o )
{ cout << " op= ying " << endl;
return *this; }
ying(const ying& o )
{cout << " cc ying " << endl; }

};

class yang
{
public:
yang () {}
~yang() { cout << " deleted yang " << endl; }
yang& operator=(const yang& o )
{ cout << " op= yang " << endl;
return *this; }
yang(const yang& o )
{cout << " cc yang " << endl; }

void run_it()
{ cout << typeid(*this).name() << endl; }
};

class bar {
yang *ptr_ya;
ying *ptr_yi;
public:
bar ( yang* ptr1, ying* ptr2 )
: ptr_ya(ptr1)
, ptr_yi(ptr2)
{}

void test_yang() { ptr_ya->run_it(); }
void test_ying() { ptr_yi->run_it(); }

~bar() { delete ptr_ya; delete ptr_yi; }

};

int main()
{
yang *ptr_ya = new yang();
ying *ptr_yi = new ying();

bar b(ptr_ya, ptr_yi);
b.test_yang();
b.test_ying();

// no longer necessary. this is why we're better off with
// auto_ptrs (the smart 'pointer' stuff)
//delete ptr_ya;
//delete ptr_yi;

}

I've haven't retrofitted the source yet to use smart pointers,
nonetheless, simple question. The ying and yang objects are passed
(pass by pointer) to bar constructors. That said, it's bars
responsibility to delete ying and yang. Correct?

Now I'm off to retrofit this to use smart pointer and examine the
behavior.

Thanks

Feb 28 '06 #1
4 1693
The beauty of some smart pointers is that you don't have to delete
anything.

Take a look at, for instance, boost's shared_ptr. It keeps a reference
count of how many "share" the pointer. When it goes to zero it deletes
the object pointed to. You can pass the thing around, make copies, etc
and you never have to worry about deleting the thing that it points to.

Until you use them, you won't realize how a big concern of C++
programming - namely avoiding memory leaks - goes away.

ma740988 wrote:
Experimenting with smart pointers here but need a refresher here.

Consider the snippet:

# include <iostream>
# include <string>

using namespace std;

class ying {
public:
ying () {}
~ying() { cout << " deleted ying " << endl; }
void run_it()
{ cout << typeid(*this).name() << endl; }

ying& operator=(const ying& o )
{ cout << " op= ying " << endl;
return *this; }
ying(const ying& o )
{cout << " cc ying " << endl; }

};

class yang
{
public:
yang () {}
~yang() { cout << " deleted yang " << endl; }
yang& operator=(const yang& o )
{ cout << " op= yang " << endl;
return *this; }
yang(const yang& o )
{cout << " cc yang " << endl; }

void run_it()
{ cout << typeid(*this).name() << endl; }
};

class bar {
yang *ptr_ya;
ying *ptr_yi;
public:
bar ( yang* ptr1, ying* ptr2 )
: ptr_ya(ptr1)
, ptr_yi(ptr2)
{}

void test_yang() { ptr_ya->run_it(); }
void test_ying() { ptr_yi->run_it(); }

~bar() { delete ptr_ya; delete ptr_yi; }

};

int main()
{
yang *ptr_ya = new yang();
ying *ptr_yi = new ying();

bar b(ptr_ya, ptr_yi);
b.test_yang();
b.test_ying();

// no longer necessary. this is why we're better off with
// auto_ptrs (the smart 'pointer' stuff)
//delete ptr_ya;
//delete ptr_yi;

}

I've haven't retrofitted the source yet to use smart pointers,
nonetheless, simple question. The ying and yang objects are passed
(pass by pointer) to bar constructors. That said, it's bars
responsibility to delete ying and yang. Correct?

Now I'm off to retrofit this to use smart pointer and examine the
behavior.

Thanks


Feb 28 '06 #2

ma740988 wrote:
Experimenting with smart pointers here but need a refresher here.

Consider the snippet:

# include <iostream>
# include <string>

using namespace std;

class ying {
public:

};

class yang
{
public:
};

class bar {
yang *ptr_ya;
ying *ptr_yi;
public:
bar ( yang* ptr1, ying* ptr2 )
: ptr_ya(ptr1)
, ptr_yi(ptr2)
{}

void test_yang() { ptr_ya->run_it(); }
void test_ying() { ptr_yi->run_it(); }

~bar() { delete ptr_ya; delete ptr_yi; }

};

int main()
{
yang *ptr_ya = new yang();
ying *ptr_yi = new ying();

bar b(ptr_ya, ptr_yi);
b.test_yang();
b.test_ying();

// no longer necessary. this is why we're better off with
// auto_ptrs (the smart 'pointer' stuff)
//delete ptr_ya;
//delete ptr_yi;

}

I've haven't retrofitted the source yet to use smart pointers,
nonetheless, simple question. The ying and yang objects are passed
(pass by pointer) to bar constructors. That said, it's bars
responsibility to delete ying and yang. Correct?


Without some policy in place that we could apply to this situation that
wold tell us that bar now "owns" the pointers passed in its
constructor, then I would say the answer is "no": bar should not free
the pointers since it did not allocate them. Whichever class or
function allocated the memory has the responsibility for ensuring that
the memory is freed after it's no longer needed. The allocating code
"owns" the allocated memory, and that would be the function main in
this case.

Now, ownership of allocated memory can be transferred - but there has
to be a well-documented, consistently-followed convention to perform
such a transfer. It's not enough just to decide that bar owns the
pointers because we wrote the code that way. Memory management becomes
unmaintainable unless the API alone can tell us when a class or
function assumes ownership of passed in, allocated memory.

A smart pointer lets a block of allocated memory have multiple,
concurrent owners - and a smart pointer frees the allocated memory only
after it has no more owners left. Since ownership of a smart pointer
does not need to be transferred, any code can decide to "own" a smart
pointer, without having to coordinate the transfer from anywhere else.
So by simplifying the rules of memory ownership, smart pointers are not
only easier to use, they also reduce the likelihood of
memory-management related errors.

Greg

Feb 28 '06 #3

An**********@gmail.com wrote:
The beauty of some smart pointers is that you don't have to delete
anything.

Take a look at, for instance, boost's shared_ptr. It keeps a reference
count of how many "share" the pointer. When it goes to zero it deletes
the object pointed to. You can pass the thing around, make copies, etc
and you never have to worry about deleting the thing that it points to.

Until you use them, you won't realize how a big concern of C++
programming - namely avoiding memory leaks - goes away.


That's where I'm headed. smart pointer. Actually, I was trying to
figure out reference counted smart versus just 'smart'. I think a
reference counted pointer would be better for what I've shown. This
way, I dont care which order I delete the objects. In essence - from
what I've seen with reference counted smart pointer - that object will
exist until the last one remain standing (i.e 'count' is now 1). Only
then will the object get destroyed

Feb 28 '06 #4
Greg wrote:
Without some policy in place that we could apply to this situation that
wold tell us that bar now "owns" the pointers passed in its
constructor, then I would say the answer is "no": bar should not free
the pointers since it did not allocate them. Whichever class or
function allocated the memory has the responsibility for ensuring that
the memory is freed after it's no longer needed. The allocating code
"owns" the allocated memory, and that would be the function main in
this case.

Now, ownership of allocated memory can be transferred - but there has
to be a well-documented, consistently-followed convention to perform
such a transfer. It's not enough just to decide that bar owns the
pointers because we wrote the code that way. Memory management becomes
unmaintainable unless the API alone can tell us when a class or
function assumes ownership of passed in, allocated memory.
Agreed, and the standard convention for communicating that ownership is
transferred is to pass (or return) a std::auto_ptr.
A smart pointer lets a block of allocated memory have multiple,
concurrent owners - and a smart pointer frees the allocated memory only
after it has no more owners left. Since ownership of a smart pointer
does not need to be transferred, any code can decide to "own" a smart
pointer, without having to coordinate the transfer from anywhere else.
So by simplifying the rules of memory ownership, smart pointers are not
only easier to use, they also reduce the likelihood of
memory-management related errors.


Correction: a smart pointer with the semantics of boost::shared_ptr
does this (well, depending on what you mean by "concurrent"). A smart
pointer with different semantics (e.g., boost::scoped_ptr or
std::auto_ptr) does not do what you describe here.

Cheers! --M

Feb 28 '06 #5

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

Similar topics

2
by: dasod | last post by:
I would like to know if my method to remove list objects is correct in this small test program. It seems to me that there might be a simplier way, but I'm afraid I don't know enough about list...
11
by: thechaosengine | last post by:
Hi all, I have a very general but quite significant question about objects. My question is, when should I create them? I know thats a crap question so let me explain a bit further. Lets...
161
by: KraftDiner | last post by:
I was under the assumption that everything in python was a refrence... so if I code this: lst = for i in lst: if i==2: i = 4 print lst I though the contents of lst would be modified.....
14
by: Jess | last post by:
Hello, I learned that there are five kinds of static objects, namely 1. global objects 2. object defined in namespace scope 3. object declared static instead classes 4. objects declared...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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...
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
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
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
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.