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).n ame() << 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).n ame() << 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 4 1710
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).n ame() << 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).n ame() << 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
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 An**********@gm ail.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
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_p tr
does this (well, depending on what you mean by "concurrent "). A smart
pointer with different semantics (e.g., boost::scoped_p tr or
std::auto_ptr) does not do what you describe here.
Cheers! --M This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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 iterators and how
they are behaving in situations like this.
#include <iostream>
#include <list>
class Test;
typedef std::list< Test* > Tlist;
|
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 take an example of user management against a database. Things I might
like to do include:
|
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.. (After reading that
|
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 static inside functions (i.e. local static
objects)
5. objects declared at file scope.
|
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
| |
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
|
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
|
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
| |
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
| |