Article View: comp.std.c++
Article #2293Ownership of objects
From: Jeff Petersen
Date: Tue, 24 Oct 1995 17:35
Date: Tue, 24 Oct 1995 17:35
45 lines
2431 bytes
2431 bytes
This is my first post to this group, so hopefully I do not stir up too many old discussions. I have found in using third party class libraries that it is often very difficult to determine who is responsible (the owner) for a dynamically allocated object (or buffer or whatever) that is passed/returned from a function. I developed a convention that goes a little like this: #define gift gift char *GetString(); or AndNode::AndNode(gift Node *leftChild, gift Node *rightChild); The 'gift' keyword tells the caller that the ownership of the dynamically allocated object is being transferred and that they are responsible for transferring it to someone else or deleteing it. I was wondering if something like this had been proposed and what the status of it was. Although this can be implemented without changing the standard (as shown above), it would be nice if everybody would follow the convention. Most libraries I use end up fudging around the problem and avoiding passing gifts around at the cost of code clarity. The only problem I have ran into is dealing with passing built-in types as gifts (as in the first example above). The problem is that if a library uses it's own memory manager, the only way for the object to be properly freed is for the new/delete operators to be globally overloaded and pointed at the libraries memory manager. A language feature that would be nice is if the 'delete' operator corresponding to the 'new' operator used to allocate a built-in type would be guaranteed to be called regardless of where it was being called from. Comments? JeffP Jeff Petersen Artificial Software jeffp@itsnet.com [ comp.std.c++ is moderated. Submission address: std-c++@ncar.ucar.edu. Contact address: std-c++-request@ncar.ucar.edu. The moderation policy is summarized in http://dogbert.lbl.gov/~matt/std-c++/policy.html. ]
Technical Details
Message-ID:
<46jbha$8bj@itchy.itsnet.com>Path:
arc1-ca.pugleaf.net!engnews2.Eng.Sun.COM!taumet!clamageShow Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!engnews2.Eng.Sun.COM!taumet!clamage From: jeffp@itsnet.com (Jeff Petersen) Newsgroups: comp.std.c++ Subject: Ownership of objects Date: 24 Oct 1995 17:35:06 GMT Organization: Artificial Software Approved: clamage@eng.sun.com (comp.std.c++) Message-ID: <46jbha$8bj@itchy.itsnet.com> NNTP-Posting-Host: taumet.eng.sun.com Content-Type: text X-Nntp-Posting-Host: ip47.itsnet.com X-Newsreader: News Xpress Version 1.0 Beta #4 Content-Length: 1663 X-Lines: 41 Originator: clamage@taumet