<p>I've learned in College that you always have to free your unused Objects but not how you actually do it. For example structuring your code right and so on. 
Are there any general rules on how to handle pointers in C++? </p>

<p>I'm currently not allowed to use boost. I have to stick to pure c++ because the framework I'm using forbids any use of generics.</p>

[{'answer_id': 27494, 'author': 'Josh', 'author_id': 257, 'author_profile': 'https://Stackoverflow.com/users/257', 'pm_score': 3, 'selected': False, 'text': '<p>Rules:</p>\n\n<ol>\n<li>Wherever possible, use a\n<a href="http://en.wikipedia.org/wiki/Smart_pointer" rel="nofollow noreferrer">smart pointer</a>.  Boost has some\n<a href="http://www.boost.org/doc/libs/1_35_0/libs/smart_ptr/sp_techniques.html" rel="nofollow noreferrer">good ones</a>.  </li>\n<li>If you\ncan\'t use a smart pointer, <a href="http://csis.pace.edu/~bergin/papers/PointerTraps.html" rel="nofollow noreferrer">null out\nyour pointer after deleting it</a>.</li>\n<li>Never work anywhere that won\'t let you use rule 1.</li>\n</ol>\n\n<p>If someone disallows rule 1, remember that if you grab someone else\'s code, change the variable names and delete the copyright notices, no-one will ever notice.  Unless it\'s a school project, where they actually check for that kind of shenanigans with quite sophisticated tools.  See also, <a href="https://stackoverflow.com/questions/21506/please-sir-send-me-the-codes">this question</a>.</p>\n'}, {'answer_id': 27500, 'author': 'Timbo', 'author_id': 1810, 'author_profile': 'https://Stackoverflow.com/users/1810', 'pm_score': 0, 'selected': False, 'text': "<ul>\n<li>When you have to use manage memory\nmanually, make sure you call delete\nin the same\nscope/function/class/module, which\never applies first, e.g.:</li>\n<li>Let the caller of a function allocate the memory that is filled by it,\ndo not return new'ed pointers.</li>\n<li>Always call delete in the same exe/dll as you called new in, because otherwise you may have problems with heap corruptions (different incompatible runtime libraries).</li>\n</ul>\n"}, {'answer_id': 27501, 'author': 'Sander', 'author_id': 2928, 'author_profile': 'https://Stackoverflow.com/users/2928', 'pm_score': 5, 'selected': True, 'text': '<p>I have worked with the embedded Symbian OS, which had an excellent system in place for this, based entirely on developer conventions.</p>\n\n<ol>\n<li>Only one object will ever own a pointer. By default this is the creator.</li>\n<li>Ownership can be passed on. To indicate passing of ownership, the object is passed as a pointer in the method signature (e.g. void Foo(Bar *zonk);).</li>\n<li>The owner will decide when to delete the object.</li>\n<li>To pass an object to a method just for use, the object is passed as a reference in the method signature (e.g. void Foo(Bat &amp;zonk);).</li>\n<li>Non-owner classes may store references (never pointers) to objects they are given only when they can be certain that the owner will not destroy it during use.</li>\n</ol>\n\n<p>Basically, if a class simply uses something, it uses a reference. If a class owns something, it uses a pointer.</p>\n\n<p>This worked beautifully and was a pleasure to use. Memory issues were very rare.</p>\n'}, {'answer_id': 27519, 'author': 'On Freund', 'author_id': 2150, 'author_profile': 'https://Stackoverflow.com/users/2150', 'pm_score': 2, 'selected': False, 'text': '<p>In the general case (resource management, where resource is not necessarily memory), you need to be familiar with the <a href="http://en.wikipedia.org/wiki/RAII" rel="nofollow noreferrer">RAII pattern</a>. This is one of the most important pieces of information for C++ developers.</p>\n'}, {'answer_id': 27551, 'author': 'mkrus', 'author_id': 2711, 'author_profile': 'https://Stackoverflow.com/users/2711', 'pm_score': 0, 'selected': False, 'text': '<p>you could derive everything from some base class that implement smart pointer like functionality (using ref()/unref() methods and a counter.</p>\n\n<p>All points highlighted by @Timbo are important when designing that base class.</p>\n'}, {'answer_id': 27642, 'author': 'Rob Wells', 'author_id': 2974, 'author_profile': 'https://Stackoverflow.com/users/2974', 'pm_score': 1, 'selected': False, 'text': '<p>G\'day,</p>\n\n<p>I\'d suggest reading the relevant sections of "Effective C++" by Scott Meyers. Easy to read and he covers some interesting gotchas to trap the unwary.</p>\n\n<p>I\'m also intrigued by the lack of templates. So no STL or Boost. Wow.</p>\n\n<p>BTW Getting people to agree on conventions is an excellent idea. As is getting everyone to agree on conventions for OOD. BTW The latest edition of Effective C++ doesn\'t have the excellent chapter about OOD conventions that the first edition had which is a pity, e.g. conventions such as public virtual inheritance <em>always</em> models an "isa" relationship.</p>\n\n<p>Rob</p>\n'}, {'answer_id': 28635, 'author': 'Nick', 'author_id': 1236, 'author_profile': 'https://Stackoverflow.com/users/1236', 'pm_score': 2, 'selected': False, 'text': '<p>I would add another rule here:</p>\n\n<ul>\n<li>Don\'t new/delete an object when an automatic object will do just fine.</li>\n</ul>\n\n<p>We have found that programmers who are new to C++, or programmers coming over from languages like Java, seem to learn about new and then obsessively use it whenever they want to create any object, regardless of the context. This is especially pernicious when an object is created locally within a function purely to do something useful. Using new in this way can be detrimental to performance and can make it all too easy to introduce silly memory leaks when the corresponding delete is forgotten. Yes, smart pointers can help with the latter but it won\'t solve the performance issues (assuming that new/delete or an equivalent is used behind the scenes). Interestingly (well, maybe), we have found that delete often tends to be more expensive than new when using Visual C++.</p>\n\n<p>Some of this confusion also comes from the fact that functions they call might take pointers, or even smart pointers, as arguments (when references would perhaps be better/clearer). This makes them think that they need to "create" a pointer (a lot of people seem to think that this is what new does) to be able to pass a pointer to a function. Clearly, this requires some rules about how APIs are written to make calling conventions as unambiguous as possible, which are reinforced with clear comments supplied with the function prototype.</p>\n'}, {'answer_id': 28885, 'author': 'Bruce Johnston', 'author_id': 3121, 'author_profile': 'https://Stackoverflow.com/users/3121', 'pm_score': 2, 'selected': False, 'text': '<p>In general, avoid allocating from the heap unless you have to. If you have to, use reference counting for objects that are long-lived and need to be shared between diverse parts of your code.</p>\n\n<p>Sometimes you need to allocate objects dynamically, but they will only be used within a certain span of time. For example, in a previous project I needed to create a complex in-memory representation of a database schema -- basically a complex cyclic graph of objects. However, the graph was only needed for the duration of a database connection, after which all the nodes could be freed in one shot. In this kind of scenario, a good pattern to use is something I call the "local GC idiom." I\'m not sure if it has an "official" name, as it\'s something I\'ve only seen in my own code, and in Cocoa (see <a href="http://developer.apple.com/documentation/Cocoa/Reference/Foundation/Classes/NSAutoreleasePool_Class/Reference/Reference.html" rel="nofollow noreferrer">NSAutoreleasePool</a> in Apple\'s Cocoa reference).</p>\n\n<p>In a nutshell, you create a "collector" object that keeps pointers to the temporary objects that you allocate using new. It is usually tied to some scope in your program, either a static scope (e.g. -- as a stack-allocated object that implements the RAII idiom) or a dynamic one (e.g. -- tied to the lifetime of a database connection, as in my previous project). When the "collector" object is freed, its destructor frees all of the objects that it points to.</p>\n\n<p>Also, like DrPizza I think the restriction to not use templates is too harsh. However, having done a lot of development on ancient versions of Solaris, AIX, and HP-UX (just recently - yes, these platforms are still alive in the Fortune 50), I can tell you that if you really care about portability, you should use templates as little as possible. Using them for containers and smart pointers ought to be ok, though (it worked for me). Without templates the technique I described is more painful to implement. It would require that all objects managed by the "collector" derive from a common base class.</p>\n'}]
['https://Stackoverflow.com/questions/27492', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/2450/']