<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" media="screen" href="/~d/styles/atom10full.xsl"?><?xml-stylesheet type="text/css" media="screen" href="http://feeds.feedburner.com/~d/styles/itemcontent.css"?><feed xmlns="http://www.w3.org/2005/Atom" xmlns:openSearch="http://a9.com/-/spec/opensearch/1.1/" xmlns:georss="http://www.georss.org/georss" xmlns:gd="http://schemas.google.com/g/2005" xmlns:thr="http://purl.org/syndication/thread/1.0" xmlns:feedburner="http://rssnamespace.org/feedburner/ext/1.0" gd:etag="W/&quot;D0QEQ3o4fip7ImA9WhRRF00.&quot;"><id>tag:blogger.com,1999:blog-3197434572108789628</id><updated>2011-11-30T18:08:22.436-08:00</updated><category term="correctness" /><category term="exception safety" /><category term="performance" /><category term="noob" /><category term="false sharing" /><category term="operator new" /><category term="c++" /><category term="new operator" /><category term="thread" /><title>c++ today</title><subtitle type="html">"Almost" day by day toughts on code inspected</subtitle><link rel="http://schemas.google.com/g/2005#feed" type="application/atom+xml" href="http://cpp-today.blogspot.com/feeds/posts/default" /><link rel="alternate" type="text/html" href="http://cpp-today.blogspot.com/" /><author><name>Gaetano</name><uri>http://www.blogger.com/profile/03637292910769902384</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="16" height="16" src="http://img2.blogblog.com/img/b16-rounded.gif" /></author><generator version="7.00" uri="http://www.blogger.com">Blogger</generator><openSearch:totalResults>13</openSearch:totalResults><openSearch:startIndex>1</openSearch:startIndex><openSearch:itemsPerPage>25</openSearch:itemsPerPage><atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="self" type="application/atom+xml" href="http://feeds.feedburner.com/blogspot/MJbOI" /><feedburner:info uri="blogspot/mjboi" /><atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="hub" href="http://pubsubhubbub.appspot.com/" /><entry gd:etag="W/&quot;CkUARXY5fCp7ImA9Wx5bFkk.&quot;"><id>tag:blogger.com,1999:blog-3197434572108789628.post-4122037632408263158</id><published>2010-11-01T12:36:00.000-07:00</published><updated>2010-11-01T12:37:24.824-07:00</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2010-11-01T12:37:24.824-07:00</app:edited><title>Temporary objects</title><content type="html">Most believe that temporary objects are const, well they are "almost const". It is possible indeed call on a temporary object a non const member. Nifty exception stated in the standard: "&lt;span class="Apple-style-span" style="font-family: sans-serif; font-size: 13px; line-height: 19px;"&gt;Section 3.10.10 in C++ ISO/IEC 14882:1998".&lt;/span&gt;&lt;br /&gt;
&lt;span class="Apple-style-span" style="font-family: sans-serif; font-size: small;"&gt;&lt;span class="Apple-style-span" style="font-size: 13px; line-height: 19px;"&gt;This exception permits to implement a movable constructor (through&amp;nbsp;a proxy) while waiting for&amp;nbsp;&lt;/span&gt;&lt;/span&gt;&lt;span class="Apple-style-span" style="font-family: sans-serif; font-size: 13px; line-height: 19px;"&gt;&lt;a class="external text" href="http://www.artima.com/cppsource/rvalue.html" rel="nofollow" style="background-attachment: initial; background-clip: initial; background-color: initial; background-image: url(http://bits.wikimedia.org/skins-1.5/vector/images/external-link-ltr-icon.png?2); background-origin: initial; background-position: 100% 50%; background-repeat: no-repeat no-repeat; color: #3366bb; padding-bottom: 0px; padding-left: 0px; padding-right: 13px; padding-top: 0px; text-decoration: none;"&gt;Rvalue references&lt;/a&gt;&amp;nbsp;in&amp;nbsp;&lt;/span&gt;&lt;span class="Apple-style-span" style="font-family: sans-serif; font-size: 13px; line-height: 19px;"&gt;C++0x language standard.&lt;/span&gt;&lt;br /&gt;
&lt;span class="Apple-style-span" style="font-family: sans-serif; font-size: small;"&gt;&lt;span class="Apple-style-span" style="font-size: 13px; line-height: 19px;"&gt;&lt;br /&gt;
&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span class="Apple-style-span" style="font-family: sans-serif; font-size: 13px; line-height: 19px;"&gt;&lt;br /&gt;
&lt;/span&gt;&lt;br /&gt;
&lt;span class="Apple-style-span" style="font-family: sans-serif; font-size: 13px; line-height: 19px;"&gt;&lt;br /&gt;
&lt;/span&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/3197434572108789628-4122037632408263158?l=cpp-today.blogspot.com' alt='' /&gt;&lt;/div&gt;
&lt;p&gt;&lt;a href="http://feedads.g.doubleclick.net/~a/Q07ShRM8tWRLDVzxp6aVzHpoDjQ/0/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/Q07ShRM8tWRLDVzxp6aVzHpoDjQ/0/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;br/&gt;
&lt;a href="http://feedads.g.doubleclick.net/~a/Q07ShRM8tWRLDVzxp6aVzHpoDjQ/1/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/Q07ShRM8tWRLDVzxp6aVzHpoDjQ/1/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;/p&gt;&lt;img src="http://feeds.feedburner.com/~r/blogspot/MJbOI/~4/o5boGQ7sUFQ" height="1" width="1"/&gt;</content><link rel="replies" type="application/atom+xml" href="http://cpp-today.blogspot.com/feeds/4122037632408263158/comments/default" title="Post Comments" /><link rel="replies" type="text/html" href="http://www.blogger.com/comment.g?blogID=3197434572108789628&amp;postID=4122037632408263158" title="0 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/3197434572108789628/posts/default/4122037632408263158?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/3197434572108789628/posts/default/4122037632408263158?v=2" /><link rel="alternate" type="text/html" href="http://feedproxy.google.com/~r/blogspot/MJbOI/~3/o5boGQ7sUFQ/temporary-objects.html" title="Temporary objects" /><author><name>Gaetano</name><uri>http://www.blogger.com/profile/03637292910769902384</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="16" height="16" src="http://img2.blogblog.com/img/b16-rounded.gif" /></author><thr:total>0</thr:total><feedburner:origLink>http://cpp-today.blogspot.com/2010/11/temporary-objects.html</feedburner:origLink></entry><entry gd:etag="W/&quot;AkUFSHs5eSp7ImA9Wx5RF0s.&quot;"><id>tag:blogger.com,1999:blog-3197434572108789628.post-8547858054645238157</id><published>2010-08-25T13:11:00.001-07:00</published><updated>2010-08-25T13:30:19.521-07:00</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2010-08-25T13:30:19.521-07:00</app:edited><title>Programmers hierarchy</title><content type="html">&lt;div style="text-align: center;"&gt;It's says all&lt;br /&gt;&lt;br /&gt;&lt;/div&gt;&lt;a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="http://4.bp.blogspot.com/_xxyADQu8yXQ/THV5G5jRzoI/AAAAAAAAAFA/UcrLEe_rf3g/s1600/G7WyP.gif"&gt;&lt;img style="display: block; margin: 0px auto 10px; text-align: center; cursor: pointer; width: 181px; height: 400px;" src="http://4.bp.blogspot.com/_xxyADQu8yXQ/THV5G5jRzoI/AAAAAAAAAFA/UcrLEe_rf3g/s400/G7WyP.gif" alt="" id="BLOGGER_PHOTO_ID_5509442878592634498" border="0" /&gt;&lt;/a&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/3197434572108789628-8547858054645238157?l=cpp-today.blogspot.com' alt='' /&gt;&lt;/div&gt;
&lt;p&gt;&lt;a href="http://feedads.g.doubleclick.net/~a/6TKsSMNSJNMbeNxHe7nUVabhJAY/0/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/6TKsSMNSJNMbeNxHe7nUVabhJAY/0/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;br/&gt;
&lt;a href="http://feedads.g.doubleclick.net/~a/6TKsSMNSJNMbeNxHe7nUVabhJAY/1/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/6TKsSMNSJNMbeNxHe7nUVabhJAY/1/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;/p&gt;&lt;img src="http://feeds.feedburner.com/~r/blogspot/MJbOI/~4/ITTD_rW6pbc" height="1" width="1"/&gt;</content><link rel="replies" type="application/atom+xml" href="http://cpp-today.blogspot.com/feeds/8547858054645238157/comments/default" title="Post Comments" /><link rel="replies" type="text/html" href="http://www.blogger.com/comment.g?blogID=3197434572108789628&amp;postID=8547858054645238157" title="2 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/3197434572108789628/posts/default/8547858054645238157?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/3197434572108789628/posts/default/8547858054645238157?v=2" /><link rel="alternate" type="text/html" href="http://feedproxy.google.com/~r/blogspot/MJbOI/~3/ITTD_rW6pbc/programmers-hierarchy.html" title="Programmers hierarchy" /><author><name>Gaetano</name><uri>http://www.blogger.com/profile/03637292910769902384</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="16" height="16" src="http://img2.blogblog.com/img/b16-rounded.gif" /></author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="http://4.bp.blogspot.com/_xxyADQu8yXQ/THV5G5jRzoI/AAAAAAAAAFA/UcrLEe_rf3g/s72-c/G7WyP.gif" height="72" width="72" /><thr:total>2</thr:total><feedburner:origLink>http://cpp-today.blogspot.com/2010/08/programmers-hierarchy.html</feedburner:origLink></entry><entry gd:etag="W/&quot;A04CRHs_fCp7ImA9WxBVEU4.&quot;"><id>tag:blogger.com,1999:blog-3197434572108789628.post-1830277550365277497</id><published>2010-02-12T02:40:00.001-08:00</published><updated>2010-02-14T01:32:45.544-08:00</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2010-02-14T01:32:45.544-08:00</app:edited><title>Asserts vs Exceptions</title><content type="html">I still see people with the doubt: "shall I use an assert or throw an exception?".&lt;br /&gt;Let me whine a bit about the usage of asserts first. Overall people code badly and I mean it, hence assertions are not used enough. Having say that, "assert" and "exception" have different&lt;br /&gt;meanings and behaves.&lt;br /&gt;&lt;br /&gt;A failure assert just terminates the normal program execution (reporting source file name and line position where the error occurred), an exception thrown has a chance to be caught. Asserts disappear/vanish when NDEBUG is defined (usually in production code); this fact is a newbie oversight and what they claim about the massive usage of assert is the overhead introduced by asserts. If I wasn't clear: "Asserts disappear in production code". What didn't you get from: "Asserts disappear in production code"?&lt;br /&gt;&lt;br /&gt;Those two facts already give an hint on when to use an assert and when an exception. If the error can be managed (by user or code itself) then an exception must be used, if the error has no chance to be managed by anyone then assert is your friend. This is not the only rule to follow indeed as said: "Asserts disappear in production code" and then in production code that error will not be "detected", errors that you expect to happen in production code can not be spotted by an assert.&lt;br /&gt;Asserts are meant to detect coding error and there is no reason to happen in production code. When writing a method there are some assumption about the internal class status (pre-condition), better to check those first in order to not do something bad, at the end of the same method better check that the internal status of class is in a consistent status (post-condition).&lt;br /&gt;Unfortunately, there are a great deal of people that are using exceptions for case that really ought to be assertions. Throwing exceptions instead of wrapping simple pre/post-conditions into a simple assertion macro is an hint of the fact that you're coding badly. Asserts are used to protect by coding error: using a null pointer for example; exception are to protect by normal program life: server not available, file not writable, etc.&lt;br /&gt;&lt;br /&gt;It should be always possible to write a unit test that is able to make an exception to be thrown, if you are not able to then it means that that piece of code is a dead code; to the other side it should be impossible do the same with assert, if you are able to write an unit test that is able to make an assert fail then most likely that assert should be an exception, or of course the code is wrong.&lt;br /&gt;&lt;br /&gt;A nice rule that drives the lazy bone programmers to use more assert is: who ever write a code that provides a set of data that asserted then the same person is responsible to fix it. This rule drives library writer to protect them self by an incorrect usage of their library at the cost to fix even the code using the library.&lt;br /&gt;&lt;br /&gt;If you have missed it: "Asserts disappear in production code".&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/3197434572108789628-1830277550365277497?l=cpp-today.blogspot.com' alt='' /&gt;&lt;/div&gt;
&lt;p&gt;&lt;a href="http://feedads.g.doubleclick.net/~a/3kKfjdEQTt5BRfdcMwEgKPY8uew/0/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/3kKfjdEQTt5BRfdcMwEgKPY8uew/0/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;br/&gt;
&lt;a href="http://feedads.g.doubleclick.net/~a/3kKfjdEQTt5BRfdcMwEgKPY8uew/1/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/3kKfjdEQTt5BRfdcMwEgKPY8uew/1/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;/p&gt;&lt;img src="http://feeds.feedburner.com/~r/blogspot/MJbOI/~4/oXnVoPP36Cs" height="1" width="1"/&gt;</content><link rel="replies" type="application/atom+xml" href="http://cpp-today.blogspot.com/feeds/1830277550365277497/comments/default" title="Post Comments" /><link rel="replies" type="text/html" href="http://www.blogger.com/comment.g?blogID=3197434572108789628&amp;postID=1830277550365277497" title="0 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/3197434572108789628/posts/default/1830277550365277497?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/3197434572108789628/posts/default/1830277550365277497?v=2" /><link rel="alternate" type="text/html" href="http://feedproxy.google.com/~r/blogspot/MJbOI/~3/oXnVoPP36Cs/asserts-vs-exceptions.html" title="Asserts vs Exceptions" /><author><name>Gaetano</name><uri>http://www.blogger.com/profile/03637292910769902384</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="16" height="16" src="http://img2.blogblog.com/img/b16-rounded.gif" /></author><thr:total>0</thr:total><feedburner:origLink>http://cpp-today.blogspot.com/2010/02/asserts-vs-exceptions.html</feedburner:origLink></entry><entry gd:etag="W/&quot;A0UAQHczcSp7ImA9WxVQFkQ.&quot;"><id>tag:blogger.com,1999:blog-3197434572108789628.post-2452759042983403471</id><published>2009-02-03T13:57:00.001-08:00</published><updated>2009-02-03T14:14:01.989-08:00</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2009-02-03T14:14:01.989-08:00</app:edited><title>Programmer competency</title><content type="html">&lt;span style="font-size:130%;"&gt;Programmers&lt;/span&gt; &lt;span style="font-size:130%;"&gt;&lt;a href="http://www.indiangeek.net/wp-content/uploads/Programmer%20competency%20matrix.htm"&gt;competency &lt;/a&gt;matrix.&lt;/span&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/3197434572108789628-2452759042983403471?l=cpp-today.blogspot.com' alt='' /&gt;&lt;/div&gt;
&lt;p&gt;&lt;a href="http://feedads.g.doubleclick.net/~a/Hf3maXo-2NWrekaYXxRLDxgyZTM/0/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/Hf3maXo-2NWrekaYXxRLDxgyZTM/0/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;br/&gt;
&lt;a href="http://feedads.g.doubleclick.net/~a/Hf3maXo-2NWrekaYXxRLDxgyZTM/1/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/Hf3maXo-2NWrekaYXxRLDxgyZTM/1/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;/p&gt;&lt;img src="http://feeds.feedburner.com/~r/blogspot/MJbOI/~4/hob8PB1-2hM" height="1" width="1"/&gt;</content><link rel="replies" type="application/atom+xml" href="http://cpp-today.blogspot.com/feeds/2452759042983403471/comments/default" title="Post Comments" /><link rel="replies" type="text/html" href="http://www.blogger.com/comment.g?blogID=3197434572108789628&amp;postID=2452759042983403471" title="1 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/3197434572108789628/posts/default/2452759042983403471?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/3197434572108789628/posts/default/2452759042983403471?v=2" /><link rel="alternate" type="text/html" href="http://feedproxy.google.com/~r/blogspot/MJbOI/~3/hob8PB1-2hM/programmer-competency.html" title="Programmer competency" /><author><name>Gaetano</name><uri>http://www.blogger.com/profile/03637292910769902384</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="16" height="16" src="http://img2.blogblog.com/img/b16-rounded.gif" /></author><thr:total>1</thr:total><feedburner:origLink>http://cpp-today.blogspot.com/2009/02/programmer-competency.html</feedburner:origLink></entry><entry gd:etag="W/&quot;CUAGQngzfCp7ImA9WxVTFUg.&quot;"><id>tag:blogger.com,1999:blog-3197434572108789628.post-5807082661220345280</id><published>2008-12-29T04:18:00.000-08:00</published><updated>2008-12-29T04:28:43.684-08:00</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2008-12-29T04:28:43.684-08:00</app:edited><title>Language programming benchmark</title><content type="html">I don't want to start a flame-ware about which language is better, I'm just reporting what I found today during some research. The site &lt;a href="http://shootout.alioth.debian.org/"&gt;language benchmark&lt;/a&gt; shows the result of some benchmark using different hardware architecture, operating systems, language, language implementation. I was astonished by some facts:&lt;br /&gt;&lt;ul&gt;&lt;li&gt;C++ (gnu/intel) implementation performs better than the corresponding C implementation both in time and memory usage&lt;br /&gt;&lt;/li&gt;&lt;li&gt;Intel C++ implementation out perform C++ GNU implementation, both in time and memory usage&lt;/li&gt;&lt;li&gt;GNU C implementation out perform Intel C implementation, both in time and memory usage&lt;/li&gt;&lt;/ul&gt;I don't report cross comparison between C++/C and other languages, you can do it yourself...&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/3197434572108789628-5807082661220345280?l=cpp-today.blogspot.com' alt='' /&gt;&lt;/div&gt;
&lt;p&gt;&lt;a href="http://feedads.g.doubleclick.net/~a/RxjpnqpHz1tDCYrFcQxpiooVHEU/0/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/RxjpnqpHz1tDCYrFcQxpiooVHEU/0/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;br/&gt;
&lt;a href="http://feedads.g.doubleclick.net/~a/RxjpnqpHz1tDCYrFcQxpiooVHEU/1/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/RxjpnqpHz1tDCYrFcQxpiooVHEU/1/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;/p&gt;&lt;img src="http://feeds.feedburner.com/~r/blogspot/MJbOI/~4/AINz9YYGkbM" height="1" width="1"/&gt;</content><link rel="replies" type="application/atom+xml" href="http://cpp-today.blogspot.com/feeds/5807082661220345280/comments/default" title="Post Comments" /><link rel="replies" type="text/html" href="http://www.blogger.com/comment.g?blogID=3197434572108789628&amp;postID=5807082661220345280" title="1 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/3197434572108789628/posts/default/5807082661220345280?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/3197434572108789628/posts/default/5807082661220345280?v=2" /><link rel="alternate" type="text/html" href="http://feedproxy.google.com/~r/blogspot/MJbOI/~3/AINz9YYGkbM/language-programming-benchmark.html" title="Language programming benchmark" /><author><name>Gaetano</name><uri>http://www.blogger.com/profile/03637292910769902384</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="16" height="16" src="http://img2.blogblog.com/img/b16-rounded.gif" /></author><thr:total>1</thr:total><feedburner:origLink>http://cpp-today.blogspot.com/2008/12/language-programming-benchmark.html</feedburner:origLink></entry><entry gd:etag="W/&quot;CEcAQXo6fyp7ImA9WxRREEU.&quot;"><id>tag:blogger.com,1999:blog-3197434572108789628.post-8479784626318888784</id><published>2008-09-18T04:06:00.000-07:00</published><updated>2008-09-22T04:14:00.417-07:00</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2008-09-22T04:14:00.417-07:00</app:edited><title>Assignment operator</title><content type="html">Today I went through a piece of code similar to this (what matters here is how the assignment operator was written):&lt;br /&gt;&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;&lt;span style="font-weight: bold; color: rgb(0, 0, 0);"&gt;class&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; Test {&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;    Test()&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;    :theId(&lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;0&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;), theName()&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;    {}&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;    Test&amp;amp; &lt;/span&gt;&lt;span style="font-weight: bold; color: rgb(0, 0, 0);"&gt;operator&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;=(&lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;const&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; Test&amp;amp; aRhs) {&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;        theID   = aRhs.theID;&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;        theName = aRhs.theName;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;        &lt;/span&gt;&lt;span style="font-weight: bold; color: rgb(0, 0, 0);"&gt;return&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; *&lt;/span&gt;&lt;span style="font-weight: bold; color: rgb(0, 0, 0);"&gt;this&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;;&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;    }&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="font-weight: bold; color: rgb(0, 0, 0);"&gt;private&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;:&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;    &lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;int&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;         theID;&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;    std::string theName;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;};&lt;/span&gt;&lt;/pre&gt;&lt;br /&gt;&lt;div style="text-align: justify;"&gt;as it is, the code works and I have nothing to say at first glance. However , who knows how the class will be extended in the future?&lt;br /&gt;&lt;br /&gt;Imagine that in a future version some members are added:&lt;br /&gt;&lt;/div&gt;&lt;pre&gt;&lt;br /&gt;&lt;span style="font-weight: bold; color: rgb(0, 0, 0);"&gt;class&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; Test {&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;    Test()&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;    :theId(&lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;0&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;), theName(), theHammer(), theHeap(&lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;0&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;)&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;    {}&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;    Test&amp;amp; &lt;/span&gt;&lt;span style="font-weight: bold; color: rgb(0, 0, 0);"&gt;operator&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;=(&lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;const&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; Test&amp;amp; aRhs) {&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;        theID     = aRhs.theID;&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;        theName   = aRhs.theName;&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;        theHammer = aRhs.theHammer;  &lt;/span&gt;&lt;span style="font-style: italic; color: rgb(128, 128, 128);"&gt;//This is going to be a bottleneck&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;        theHeap   = aRhs.theHeap;    &lt;/span&gt;&lt;span style="font-style: italic; color: rgb(128, 128, 128);"&gt;//This is wrong&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;        &lt;/span&gt;&lt;span style="font-weight: bold; color: rgb(0, 0, 0);"&gt;return&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; *&lt;/span&gt;&lt;span style="font-weight: bold; color: rgb(0, 0, 0);"&gt;this&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;;&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;    }&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="font-weight: bold; color: rgb(0, 0, 0);"&gt;private&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;:&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;    &lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;int&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;          theID;&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;    std::string  theName;&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;    HugeClass    theHammer;  &lt;/span&gt;&lt;span style="font-style: italic; color: rgb(128, 128, 128);"&gt;//Extra member with huge footprint&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;    HeapClass*   theHeap;    &lt;/span&gt;&lt;span style="font-style: italic; color: rgb(128, 128, 128);"&gt;//Extra member on heap&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;};&lt;/span&gt;&lt;/pre&gt;&lt;div style="text-align: justify;"&gt;as you can see with these two extra members following the same code line as the previous version the code becomes invalid. The objection I get is: "if one day someone adds those two members then they will take care to write it correctly", unfortunately in the real world it doesn't work like this. The average programmer will just add those two extra members following the code line already in place, following the rule: "after all if the code works for the already present members why shouldn't it be the same for the two extra members I have been told to add?"&lt;br /&gt;&lt;/div&gt;&lt;br /&gt;&lt;div style="text-align: justify;"&gt;The trick to avoid problems like this is to write the right code, from the start thinking of what will happen in the future, when possible of course. Usually the change to make is just a matter of a few lines of code and a two line comment in order to warn the future coders.&lt;br /&gt;&lt;br /&gt;The original well written class would have been:&lt;/div&gt;&lt;pre&gt;&lt;br /&gt;&lt;span style="font-weight: bold; color: rgb(0, 0, 0);"&gt;class&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; Test {&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;    Test()&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;    :theId(&lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;0&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;), theName()&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;    {}&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;    Test&amp;amp; &lt;/span&gt;&lt;span style="font-weight: bold; color: rgb(0, 0, 0);"&gt;operator&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;=(&lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;const&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; Test&amp;amp; aRhs) {&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;        &lt;/span&gt;&lt;span style="font-style: italic; color: rgb(128, 128, 128);"&gt;//Check for a self assignment&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;        &lt;/span&gt;&lt;span style="font-weight: bold; color: rgb(0, 0, 0);"&gt;if&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; (&lt;/span&gt;&lt;span style="font-weight: bold; color: rgb(0, 0, 0);"&gt;this&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; != &amp;amp;aRhs) {&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;            theID     = aRhs.theID;&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;            theName   = aRhs.theName;&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;        }&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;        &lt;/span&gt;&lt;span style="font-weight: bold; color: rgb(0, 0, 0);"&gt;return&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; *&lt;/span&gt;&lt;span style="font-weight: bold; color: rgb(0, 0, 0);"&gt;this&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;;&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;    }&lt;/span&gt;&lt;br /&gt;&lt;span style="font-weight: bold; color: rgb(0, 0, 0);"&gt;private&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;:&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;    &lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;int&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;          theID;&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;    std::string  theName;&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;};&lt;/span&gt;&lt;/pre&gt;&lt;br /&gt;At this point the objection is: why check for a self assignment when the event is never going to happen?  Who will ever write:&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;Test t;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;t=t;&lt;/span&gt;&lt;/pre&gt;well, the code is valid so be sure someone will, also it is not always easy to spot a self assignment, consider this:&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;t[j] = t[i];&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;or even:&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;Test t;&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;Test &amp;amp;a = t;&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;...&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;t = a;&lt;br /&gt;&lt;/span&gt;&lt;/pre&gt;&lt;br /&gt;&lt;div style="text-align: justify;"&gt;The real question here is: "Why was the assignment operator in that class was ever implemented?" The question makes a point, the operator was not needed at all, in that case the right thing to do is to remove it.&lt;br /&gt;&lt;br /&gt;Let's then suppose the class is the one with the two extra members, the class is managing dynamically allocated memory then the operator &lt;span style="font-weight: bold;"&gt;must&lt;/span&gt; be implemented. The almost correct version is:&lt;br /&gt;&lt;/div&gt;&lt;br /&gt;&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;&lt;span style="font-weight: bold; color: rgb(0, 0, 0);"&gt;class&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; Test {&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;    Test()&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;    :theId(&lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;0&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;), theName(), theHammer(), theHeap(&lt;/span&gt;&lt;span style="font-weight: bold; color: rgb(0, 0, 0);"&gt;new&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; HeapClass)&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;    {}&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;    Test&amp;amp; &lt;/span&gt;&lt;span style="font-weight: bold; color: rgb(0, 0, 0);"&gt;operator&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;=(&lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;const&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; Test&amp;amp; aRhs) {&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;        &lt;/span&gt;&lt;span style="font-weight: bold; color: rgb(0, 0, 0);"&gt;if&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; (&lt;/span&gt;&lt;span style="font-weight: bold; color: rgb(0, 0, 0);"&gt;this&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; != &amp;amp;aRhs) {&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;            theID     = aRhs.theID;&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;            theName   = aRhs.theName;&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;            theHammer = aRhs.theHammer;  &lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;            &lt;/span&gt;&lt;span style="font-weight: bold; color: rgb(0, 0, 0);"&gt;delete&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; theHeap;&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;            theHeap   = &lt;/span&gt;&lt;span style="font-weight: bold; color: rgb(0, 0, 0);"&gt;new&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; HeapClass(*aRhs.theHeap);&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;        }&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;        &lt;/span&gt;&lt;span style="font-weight: bold; color: rgb(0, 0, 0);"&gt;return&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; *&lt;/span&gt;&lt;span style="font-weight: bold; color: rgb(0, 0, 0);"&gt;this&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;;&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;    }&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="font-weight: bold; color: rgb(0, 0, 0);"&gt;private&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;:&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;    &lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;int&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;          theID;&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;    std::string  theName;&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;    HugeClass    theHammer;  &lt;/span&gt;&lt;span style="font-style: italic; color: rgb(128, 128, 128);"&gt;//Extra member with huge footprint&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;    HeapClass*   theHeap;    &lt;/span&gt;&lt;span style="font-style: italic; color: rgb(128, 128, 128);"&gt;//Extra member on heap&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;};&lt;/span&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;&lt;div style="text-align: justify;"&gt;I wrote "almost correct" because the assignment operator is correct but not exception safe, imagine what will happen if an exception is thrown, if that is the case then the class will be left in an inconsistent state. The goal is not an easy one, in order to make an assignment operator exception safe before modifying the internal state it is better to create a temporary object and then swap it with the internal state with operations that do not throw exceptions. This is achieved using the Pimpl idiom, moving all the internal state of Test inside another class and then leaving inside the class Test a pointer to this new class, it is better to use a boost::shared_ptr in this case to avoid exception catches:&lt;/div&gt;&lt;br /&gt;&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;&lt;span style="font-weight: bold; color: rgb(0, 0, 0);"&gt;class&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; TestImpl {&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;    &lt;/span&gt;&lt;span style="font-weight: bold; color: rgb(0, 0, 0);"&gt;friend&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; &lt;/span&gt;&lt;span style="font-weight: bold; color: rgb(0, 0, 0);"&gt;class&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; Test;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="font-weight: bold; color: rgb(0, 0, 0);"&gt;private&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;:&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;    TestImpl()&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;    :theID(&lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;0&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;), theName(), theHammer(), theHeap(&lt;/span&gt;&lt;span style="font-weight: bold; color: rgb(0, 0, 0);"&gt;new&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; HeapClass)&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;    {}&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;    TestImpl(&lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;const&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; TestImpl&amp;amp; aRhs)&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;    :theId(aRhs.theID), theName(aRhs.theName),&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;     theHammer(aRhs.theHammer), theHeap(&lt;/span&gt;&lt;span style="font-weight: bold; color: rgb(0, 0, 0);"&gt;new&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; HeapClass(aRhs.theHeap))&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;    {}&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;    &lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;int&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;          theID;&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;    std::string  theName;&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;    HugeClass    theHammer;  &lt;/span&gt;&lt;span style="font-style: italic; color: rgb(128, 128, 128);"&gt;//Extra member with huge footprint&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;    HeapClass*   theHeap;    &lt;/span&gt;&lt;span style="font-style: italic; color: rgb(128, 128, 128);"&gt;//Extra member on heap&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;};&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="font-weight: bold; color: rgb(0, 0, 0);"&gt;class&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; Test {&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;    Test()&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;    :theImplementation(&lt;/span&gt;&lt;span style="font-weight: bold; color: rgb(0, 0, 0);"&gt;new&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; TestImpl)&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;    {}&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;    Test&amp;amp; &lt;/span&gt;&lt;span style="font-weight: bold; color: rgb(0, 0, 0);"&gt;operator&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;=(&lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;const&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; Test&amp;amp; aRhs) {&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;        &lt;/span&gt;&lt;span style="font-weight: bold; color: rgb(0, 0, 0);"&gt;if&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; (&lt;/span&gt;&lt;span style="font-weight: bold; color: rgb(0, 0, 0);"&gt;this&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; != &amp;amp;aRhs) {&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;            boost::shared_ptr&amp;lt;TestImpl&amp;gt; tmp(&lt;/span&gt;&lt;span style="font-weight: bold; color: rgb(0, 0, 0);"&gt;new&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; TestImpl(*aRhs.theImplementation));&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;            std::swap(theImplementation, tmp);&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;        }&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;        &lt;/span&gt;&lt;span style="font-weight: bold; color: rgb(0, 0, 0);"&gt;return&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; *&lt;/span&gt;&lt;span style="font-weight: bold; color: rgb(0, 0, 0);"&gt;this&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;;&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;    }&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="font-weight: bold; color: rgb(0, 0, 0);"&gt;private&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;:&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;    boost::shared_ptr&amp;lt;TestImpl&amp;gt; theImplementation;&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;};&lt;/span&gt;&lt;/pre&gt;&lt;br /&gt;As you can see writing right code is not easy, and this becomes more difficult if you want to write exception safe code. What I suggest is, to remove assignment operator and copy constructor and write those only if really needed:&lt;br /&gt;&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;&lt;span style="font-weight: bold; color: rgb(0, 0, 0);"&gt;class&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; Test {&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;    Test()&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;    :theID(&lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;0&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;), theName(), theHammer(), theHeap(&lt;/span&gt;&lt;span style="font-weight: bold; color: rgb(0, 0, 0);"&gt;new&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; HeapClass)&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;    {}&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="font-weight: bold; color: rgb(0, 0, 0);"&gt;private&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;:&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;    Test&amp;amp; &lt;/span&gt;&lt;span style="font-weight: bold; color: rgb(0, 0, 0);"&gt;operator&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;=(&lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;const&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; Test&amp;amp; aRhs);  &lt;/span&gt;&lt;span style="font-style: italic; color: rgb(128, 128, 128);"&gt;//disabled (do not even implement it)&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;    Test(&lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;const&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; Test&amp;amp; aRhs);             &lt;/span&gt;&lt;span style="font-style: italic; color: rgb(128, 128, 128);"&gt;//disabled (do not even implement it)&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;    &lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;int&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;          theID;&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;    std::string  theName;&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;    HugeClass    theHammer;  &lt;/span&gt;&lt;span style="font-style: italic; color: rgb(128, 128, 128);"&gt;//Extra member with huge footprint&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;    HeapClass*   theHeap;    &lt;/span&gt;&lt;span style="font-style: italic; color: rgb(128, 128, 128);"&gt;//Extra member on heap&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;};&lt;/span&gt;&lt;/pre&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/3197434572108789628-8479784626318888784?l=cpp-today.blogspot.com' alt='' /&gt;&lt;/div&gt;
&lt;p&gt;&lt;a href="http://feedads.g.doubleclick.net/~a/SCojOMj8gbIL2-VYy8yVnAcUItA/0/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/SCojOMj8gbIL2-VYy8yVnAcUItA/0/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;br/&gt;
&lt;a href="http://feedads.g.doubleclick.net/~a/SCojOMj8gbIL2-VYy8yVnAcUItA/1/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/SCojOMj8gbIL2-VYy8yVnAcUItA/1/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;/p&gt;&lt;img src="http://feeds.feedburner.com/~r/blogspot/MJbOI/~4/PWI90TpQUuY" height="1" width="1"/&gt;</content><link rel="replies" type="application/atom+xml" href="http://cpp-today.blogspot.com/feeds/8479784626318888784/comments/default" title="Post Comments" /><link rel="replies" type="text/html" href="http://www.blogger.com/comment.g?blogID=3197434572108789628&amp;postID=8479784626318888784" title="0 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/3197434572108789628/posts/default/8479784626318888784?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/3197434572108789628/posts/default/8479784626318888784?v=2" /><link rel="alternate" type="text/html" href="http://feedproxy.google.com/~r/blogspot/MJbOI/~3/PWI90TpQUuY/assignment-operator.html" title="Assignment operator" /><author><name>Gaetano</name><uri>http://www.blogger.com/profile/03637292910769902384</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="16" height="16" src="http://img2.blogblog.com/img/b16-rounded.gif" /></author><thr:total>0</thr:total><feedburner:origLink>http://cpp-today.blogspot.com/2008/09/assignment-operator.html</feedburner:origLink></entry><entry gd:etag="W/&quot;D0cARnc6cCp7ImA9WxdQGEQ.&quot;"><id>tag:blogger.com,1999:blog-3197434572108789628.post-9055637028213934611</id><published>2008-06-19T07:31:00.000-07:00</published><updated>2008-06-19T10:04:07.918-07:00</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2008-06-19T10:04:07.918-07:00</app:edited><category scheme="http://www.blogger.com/atom/ns#" term="c++" /><category scheme="http://www.blogger.com/atom/ns#" term="thread" /><title>Threading mess (2)!</title><content type="html">I got some comments about my last post about the fact that the showed solution was just detecting the scenario of two or more threads entering at the same time a critical section (in the last post example the method &lt;span style="font-style: italic;"&gt;Shared::foo&lt;/span&gt;). What it doesn't answer is the real question: "is this class during its life being used by more than a single thread, or more specifically, is a certain section of code used by more than a thread"? Indeed if you remember after a SCOPED_LOCK leaves his scope, it "releases"  the  stored current ID thread allowing another thread to enter it.&lt;br /&gt;&lt;br /&gt;I have added a nested Watch class to ThreadCollisionWarning class that detects also if a critical section is ever used by two different threads ( for example you can detect if a given class is constructed and destroyed within the same thread).&lt;br /&gt;&lt;br /&gt;The code is the following:&lt;br /&gt;&lt;br /&gt;&lt;pre   style="border: 1px dashed ; overflow: auto; color: rgb(0, 0, 0); background-color: rgb(238, 238, 238);font-family:Andale Mono,Lucida Console,Monaco,fixed,monospace;font-size:12px;"&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 128, 0);"&gt;#ifndef THREAD_COLLISION_WARNING&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 128, 0);"&gt;#define THREAD_COLLISION_WARNING&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 128, 0);"&gt;#include &amp;lt;stdexcept&amp;gt;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 128, 0);"&gt;#ifdef NDEBUG&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 128, 0);"&gt;#define THREAD_WATCH(obj)&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 128, 0);"&gt;#define SCOPED_WATCH(obj)&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 128, 0);"&gt;#define WATCH(obj)&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 128, 0);"&gt;#else&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 128, 0);"&gt;#define THREAD_WATCH(obj) ThreadCollisionWarning _##obj;&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 128, 0);"&gt;#define SCOPED_WATCH(obj) ThreadCollisionWarning::ScopedWatch scoped_watch_##obj(_##obj);&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 128, 0);"&gt;#define WATCH(obj)        ThreadCollisionWarning::Watch watch_##obj(_##obj);&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 128, 0);"&gt;#endif&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="font-weight: bold; color: rgb(0, 0, 0);"&gt;class&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; ThreadCollisionWarning {&lt;/span&gt;&lt;br /&gt;&lt;span style="font-weight: bold; color: rgb(0, 0, 0);"&gt;public&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;:&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;    ThreadCollisionWarning()&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;    :theActiveThread(&lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;0&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;)&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;    { }&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;    ~ThreadCollisionWarning() { }&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;    &lt;/span&gt;&lt;span style="font-weight: bold; color: rgb(0, 0, 0);"&gt;class&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; Watch {&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;        &lt;/span&gt;&lt;span style="font-weight: bold; color: rgb(0, 0, 0);"&gt;public&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;:&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;            Watch(ThreadCollisionWarning&amp;amp; aTCW)&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;            :theWarner(aTCW)&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;            { theWarner.enter_self(); }&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;            ~Watch() { }&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;        &lt;/span&gt;&lt;span style="font-weight: bold; color: rgb(0, 0, 0);"&gt;private&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;:&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;            ThreadCollisionWarning&amp;amp; theWarner;&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;    };&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;    &lt;/span&gt;&lt;span style="font-weight: bold; color: rgb(0, 0, 0);"&gt;class&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; ScopedWatch {&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;        &lt;/span&gt;&lt;span style="font-weight: bold; color: rgb(0, 0, 0);"&gt;public&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;:&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;            ScopedWatch(ThreadCollisionWarning&amp;amp; aTCW)&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;            :theWarner(aTCW)&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;            { theWarner.enter(); }&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;            ~ScopedWatch() { theWarner.leave(); }&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;        &lt;/span&gt;&lt;span style="font-weight: bold; color: rgb(0, 0, 0);"&gt;private&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;:&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;            ThreadCollisionWarning&amp;amp; theWarner;&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;    };&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="font-weight: bold; color: rgb(0, 0, 0);"&gt;private&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;:&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;    &lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;void&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; enter_self() { &lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;        &lt;/span&gt;&lt;span style="font-style: italic; color: rgb(128, 128, 128);"&gt;//If the active thread is 0 then I'll write the current thread ID&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;        &lt;/span&gt;&lt;span style="font-style: italic; color: rgb(128, 128, 128);"&gt;//if two or more threads arrive here only one will success to write on theActiveThread &lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;        &lt;/span&gt;&lt;span style="font-style: italic; color: rgb(128, 128, 128);"&gt;//the current thread ID&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;        &lt;/span&gt;&lt;span style="font-weight: bold; color: rgb(0, 0, 0);"&gt;if&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; (! __sync_bool_compare_and_swap(&amp;amp;theActiveThread, &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;0&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;, pthread_self())) { &lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;            &lt;/span&gt;&lt;span style="font-style: italic; color: rgb(128, 128, 128);"&gt;//Last chance! may be is the thread itself calling from a critical section&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;            &lt;/span&gt;&lt;span style="font-style: italic; color: rgb(128, 128, 128);"&gt;//another critical section&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;            &lt;/span&gt;&lt;span style="font-weight: bold; color: rgb(0, 0, 0);"&gt;if&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; (!__sync_bool_compare_and_swap(&amp;amp;theActiveThread, pthread_self(), theActiveThread)) {&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;                &lt;/span&gt;&lt;span style="font-weight: bold; color: rgb(0, 0, 0);"&gt;throw&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; std::runtime_error(&lt;/span&gt;&lt;span style="color: rgb(221, 0, 0);"&gt;"Thread Collision"&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;);&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;            }&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;        }&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;    }&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;    &lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;void&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; enter() { &lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;        &lt;/span&gt;&lt;span style="font-weight: bold; color: rgb(0, 0, 0);"&gt;if&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; (!__sync_bool_compare_and_swap(&amp;amp;theActiveThread, &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;0&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;, pthread_self())) {&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;            &lt;/span&gt;&lt;span style="font-style: italic; color: rgb(128, 128, 128);"&gt;//gotcha! another thread is trying to use the same class&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;            &lt;/span&gt;&lt;span style="font-weight: bold; color: rgb(0, 0, 0);"&gt;throw&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; std::runtime_error(&lt;/span&gt;&lt;span style="color: rgb(221, 0, 0);"&gt;"Thread Collision"&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;);&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;        }&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;    }&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;    &lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;void&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; leave() { &lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;        __sync_fetch_and_xor(&amp;amp;theActiveThread, theActiveThread);&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;    }&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;    pthread_t theActiveThread;&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;};&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 128, 0);"&gt;#endif&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;The nested Watch class (used by WATCH macro) just during his constructor initializes theActiveThread member with the current id thread if  it isn't still initialized, in case it gives another chance to check if the active thread is itself.&lt;br /&gt;&lt;br /&gt;So let's see some examples of use:&lt;br /&gt;&lt;br /&gt;Case #1: Check that one thread ever uses some critical section (recursion allowed)&lt;br /&gt;&lt;br /&gt;&lt;pre   style="border: 1px dashed ; overflow: auto; color: rgb(0, 0, 0); background-color: rgb(238, 238, 238);font-family:Andale Mono,Lucida Console,Monaco,fixed,monospace;font-size:12px;"&gt;&lt;br /&gt;&lt;span style="font-weight: bold; color: rgb(0, 0, 0);"&gt;struct&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; Shared {&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;   &lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;void&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; foo() { &lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;       WATCH(CriticaSectionA);&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;       bar();&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;   }&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;   &lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;void&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; bar() {&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;       WATCH(CriticaSectionA);&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;   }&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;   THREAD_WATCH(CriticaSectionA);&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;};&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;Case #2: Check that a class is constructed and destroyed inside the same thread&lt;br /&gt;&lt;br /&gt;&lt;pre   style="border: 1px dashed ; overflow: auto; color: rgb(0, 0, 0); background-color: rgb(238, 238, 238);font-family:Andale Mono,Lucida Console,Monaco,fixed,monospace;font-size:12px;"&gt;&lt;br /&gt;&lt;span style="font-weight: bold; color: rgb(0, 0, 0);"&gt;struct&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; Shared {&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;    Shared() {&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;        WATCH(CTOR_DTOR_SECTION);&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;        ...&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;    }&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;   ~Shared() { &lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;       WATCH(CTOR_DTOR_SECTION);&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;       ...&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;   }&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;   THREAD_WATCH(CTOR_DTOR_SECTION);&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;};&lt;/span&gt;&lt;/pre&gt;&lt;br /&gt;note that doing so the Shared destructor can throw an exception, so do not use this in a production code (put the WATCH between a try-catch and just notify it in some way).&lt;br /&gt;&lt;br /&gt;Case #3: Two or more different threads can enter a critical section but in exclusive way (useful to check if external sync mechanism are working).&lt;br /&gt;&lt;br /&gt;&lt;pre   style="border: 1px dashed ; overflow: auto; color: rgb(0, 0, 0); background-color: rgb(238, 238, 238);font-family:Andale Mono,Lucida Console,Monaco,fixed,monospace;font-size:12px;"&gt;&lt;br /&gt;&lt;span style="font-weight: bold; color: rgb(0, 0, 0);"&gt;struct&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; Shared {&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;    foo() {&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;        SCOPED_WATCH(CriticalSectionA);&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;    }&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;   THREAD_WATCH(CriticalSectionA);&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;};&lt;/span&gt;&lt;/pre&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/3197434572108789628-9055637028213934611?l=cpp-today.blogspot.com' alt='' /&gt;&lt;/div&gt;
&lt;p&gt;&lt;a href="http://feedads.g.doubleclick.net/~a/eUZGkwdJPIKltw7DL_FV-33Ncu4/0/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/eUZGkwdJPIKltw7DL_FV-33Ncu4/0/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;br/&gt;
&lt;a href="http://feedads.g.doubleclick.net/~a/eUZGkwdJPIKltw7DL_FV-33Ncu4/1/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/eUZGkwdJPIKltw7DL_FV-33Ncu4/1/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;/p&gt;&lt;img src="http://feeds.feedburner.com/~r/blogspot/MJbOI/~4/U7liTjiHvIU" height="1" width="1"/&gt;</content><link rel="replies" type="application/atom+xml" href="http://cpp-today.blogspot.com/feeds/9055637028213934611/comments/default" title="Post Comments" /><link rel="replies" type="text/html" href="http://www.blogger.com/comment.g?blogID=3197434572108789628&amp;postID=9055637028213934611" title="0 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/3197434572108789628/posts/default/9055637028213934611?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/3197434572108789628/posts/default/9055637028213934611?v=2" /><link rel="alternate" type="text/html" href="http://feedproxy.google.com/~r/blogspot/MJbOI/~3/U7liTjiHvIU/threading-mess-2.html" title="Threading mess (2)!" /><author><name>Gaetano</name><uri>http://www.blogger.com/profile/03637292910769902384</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="16" height="16" src="http://img2.blogblog.com/img/b16-rounded.gif" /></author><thr:total>0</thr:total><feedburner:origLink>http://cpp-today.blogspot.com/2008/06/threading-mess-2.html</feedburner:origLink></entry><entry gd:etag="W/&quot;DEMBRXg-cCp7ImA9WxdQGUg.&quot;"><id>tag:blogger.com,1999:blog-3197434572108789628.post-2799767212294860004</id><published>2008-06-18T08:19:00.000-07:00</published><updated>2008-06-20T03:07:34.658-07:00</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2008-06-20T03:07:34.658-07:00</app:edited><category scheme="http://www.blogger.com/atom/ns#" term="c++" /><category scheme="http://www.blogger.com/atom/ns#" term="thread" /><title>Threading mess!</title><content type="html">Software development requires discipline, you know what I mean: brainstorming, coding rules, code inspections, pair programming. Unfortunately all these activities for the management are a waste of time  so at the end you end up to just act as a "code monkey"; to rub salt to the wound "multithread programming" requires ten time the discipline you need in a single thread environment. I've recently stepped in a project of medium size, and at the easy question: "are those class instances shared between two or more threads" the response was: "no... wait... yes, well I'm not sure... I don't know...". Riiiight.&lt;br /&gt;Let's see a quick technique that should permit to detect (at runtime, sigh!) if two or more threads are using concurrently a class.&lt;br /&gt;&lt;br /&gt;Suppose we have the following class:&lt;br /&gt;&lt;br /&gt;&lt;pre   style="border: 1px dashed ; overflow: auto; color: rgb(0, 0, 0); background-color: rgb(238, 238, 238);font-family:Andale Mono,Lucida Console,Monaco,fixed,monospace;font-size:12px;"&gt;&lt;br /&gt;&lt;span style="font-weight: bold; color: rgb(0, 0, 0);"&gt;struct&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; Shared {&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;   &lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;void&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; foo() { ...  }&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;};&lt;/span&gt;&lt;/pre&gt;&lt;br /&gt;and we are unsure if two threads are calling the Shared::foo() at same time. One way is to add a mutex to the class Shared and then attempt a "try lock" as first thing to do inside the foo and raise an error in case the try lock failed.&lt;br /&gt;&lt;br /&gt;Something like:&lt;br /&gt;&lt;br /&gt;&lt;pre   style="border: 1px dashed ; overflow: auto; color: rgb(0, 0, 0); background-color: rgb(238, 238, 238);font-family:Andale Mono,Lucida Console,Monaco,fixed,monospace;font-size:12px;"&gt;&lt;br /&gt;&lt;span style="font-weight: bold; color: rgb(0, 0, 0);"&gt;class&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; Shared {&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;   &lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;void&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; foo() {&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;      TryScopedLock aLock(theMutex);&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;      &lt;/span&gt;&lt;span style="font-weight: bold; color: rgb(0, 0, 0);"&gt;if&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; (!aLock) { &lt;/span&gt;&lt;span style="font-weight: bold; color: rgb(0, 0, 0);"&gt;throw&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; std::runtime_error(&lt;/span&gt;&lt;span style="color: rgb(221, 0, 0);"&gt;"BOOM"&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;); }&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;      ...&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;   }&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="font-weight: bold; color: rgb(0, 0, 0);"&gt;private&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;:&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;   &lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;volatile&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; mutex theMutex;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;};&lt;/span&gt;&lt;/pre&gt;&lt;br /&gt;this approach works but it will slow down your software, hiding other problems around and, most of all, introduces useless synchronization; a mutex lock is not exactly cheap.&lt;br /&gt;&lt;br /&gt;The idea is to use the technique above but lock less, GCC gives us some functions for atomic memory access, and we can use for example:&lt;br /&gt;&lt;br /&gt;&lt;pre   style="border: 1px dashed ; overflow: auto; color: rgb(0, 0, 0); background-color: rgb(238, 238, 238);font-family:Andale Mono,Lucida Console,Monaco,fixed,monospace;font-size:12px;"&gt;&lt;br /&gt;&lt;code&gt;bool __sync_bool_compare_and_swap (&lt;/code&gt;&lt;var&gt;type&lt;/var&gt;&lt;code&gt; *ptr, &lt;/code&gt;&lt;var&gt;type&lt;/var&gt;&lt;code&gt; oldval &lt;/code&gt;&lt;var&gt;type&lt;/var&gt;&lt;code&gt; newval, ...)&lt;/code&gt;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;for our very purpose. That function assigns at *ptr the value newval only if the current value of *ptr is oldval, it returns true if the comparison is successful and newval was written. We can use it to store the threadId when we enter the critical section, "zeroing" the value when we exit.&lt;br /&gt;&lt;br /&gt;Basically I wrote a class that store (with an atomic operation) the threadID of the thread entering the critical section, and when it leaves forgets about the threadID. This was the result:&lt;br /&gt;&lt;br /&gt;&lt;pre face="Andale Mono,Lucida Console,Monaco,fixed,monospace" size="12px" style="border: 1px dashed ; overflow: auto; color: rgb(0, 0, 0); background-color: rgb(238, 238, 238);"&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 128, 0);"&gt;#ifndef THREAD_COLLISION_WARNING&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 128, 0);"&gt;#define THREAD_COLLISION_WARNING&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 128, 0);"&gt;#include &amp;lt;stdexcept&amp;gt;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 128, 0);"&gt;#ifdef NDEBUG&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 128, 0);"&gt;#define THREAD_WATCH(obj)&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 128, 0);"&gt;#define SCOPED_WATCH(obj)&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 128, 0);"&gt;#else&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 128, 0);"&gt;#define THREAD_WATCH(obj) ThreadCollisionWarning _##obj;&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 128, 0);"&gt;#define SCOPED_WATCH(obj) ThreadCollisionWarning::ScopedWatch scoped_watch_##obj(_##obj);&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 128, 0);"&gt;#endif&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="font-weight: bold; color: rgb(0, 0, 0);"&gt;class&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; ThreadCollisionWarning {&lt;/span&gt;&lt;br /&gt;&lt;span style="font-weight: bold; color: rgb(0, 0, 0);"&gt;public&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;:&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;    ThreadCollisionWarning()&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;    :theActiveThread(&lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;0&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;)&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;    { }&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;    ~ThreadCollisionWarning() { }&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;    &lt;/span&gt;&lt;span style="font-weight: bold; color: rgb(0, 0, 0);"&gt;class&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; ScopedWatch {&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;        &lt;/span&gt;&lt;span style="font-weight: bold; color: rgb(0, 0, 0);"&gt;public&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;:&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;            ScopedWatch(ThreadCollisionWarning&amp;amp; aTCW)&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;            :theWarner(aTCW)&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;            { theWarner.enter(); }&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;            ~ScopedWatch() { theWarner.leave(); }&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;        &lt;/span&gt;&lt;span style="font-weight: bold; color: rgb(0, 0, 0);"&gt;private&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;:&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;            ThreadCollisionWarning&amp;amp; theWarner;&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;    };&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="font-weight: bold; color: rgb(0, 0, 0);"&gt;private&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;:&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;    &lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;void&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; enter() { &lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;        &lt;/span&gt;&lt;span style="font-weight: bold; color: rgb(0, 0, 0);"&gt;if&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; (!__sync_bool_compare_and_swap(&amp;amp;theActiveThread, &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;0&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;, pthread_self())) {&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;            &lt;/span&gt;&lt;span style="font-style: italic; color: rgb(128, 128, 128);"&gt;//gotcha! another thread is trying to use the same class&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;            &lt;/span&gt;&lt;span style="font-weight: bold; color: rgb(0, 0, 0);"&gt;throw&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; std::runtime_error(&lt;/span&gt;&lt;span style="color: rgb(221, 0, 0);"&gt;"Thread Collision"&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;);&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;        }&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;    }&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;    &lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;void&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; leave() { &lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;        __sync_fetch_and_xor(&amp;amp;theActiveThread, theActiveThread);&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;    }&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;    pthread_t theActiveThread;&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;};&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 128, 0);"&gt;#endif&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;The class &lt;span style="font-style: italic;"&gt;ThreadCollisionWarning&lt;/span&gt; has the responsibility to store the thread using the class (or more in general entering a critical section) while the nested class &lt;span style="font-style: italic;"&gt;ScopedWatch&lt;/span&gt; is used to notify the entering and the leaving the critical section. Look the implementation of the two ThreadCollisionWarning::enter and ThreadCollisionWarning::leave, the former stores the thread Id only if the old value was 0 the latter just zeroes it. The macros simplify the usage.&lt;br /&gt;&lt;br /&gt;So there we go, the class Shared becomes then:&lt;br /&gt;&lt;br /&gt;&lt;pre style="border: 1px dashed ; overflow: auto; color: rgb(0, 0, 0); background-color: rgb(238, 238, 238); font-family: Andale Mono,Lucida Console,Monaco,fixed,monospace; font-size: 12px;"&gt;&lt;br /&gt;&lt;span style="font-weight: bold; color: rgb(0, 0, 0);"&gt;struct&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; Shared {&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;   &lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;void&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; foo(&lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;char&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; aC) { &lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;       SCOPED_WATCH(Shared)&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;       ...&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;   }&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;   THREAD_WATCH(Shared)&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;};&lt;/span&gt;&lt;/pre&gt;&lt;br /&gt;using SCOPED_WATCH we just check that two threads are not using the method foo concurrently.&lt;br /&gt;&lt;br /&gt;Of course the implementation above is not in any mean a complete solution to the problem I exposed at the beginning, it helps and it can be a good start point to create a better tool to detect if someone messed around.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/3197434572108789628-2799767212294860004?l=cpp-today.blogspot.com' alt='' /&gt;&lt;/div&gt;
&lt;p&gt;&lt;a href="http://feedads.g.doubleclick.net/~a/OmNxHxRBAVFbYTMGteXqaWYjwHU/0/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/OmNxHxRBAVFbYTMGteXqaWYjwHU/0/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;br/&gt;
&lt;a href="http://feedads.g.doubleclick.net/~a/OmNxHxRBAVFbYTMGteXqaWYjwHU/1/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/OmNxHxRBAVFbYTMGteXqaWYjwHU/1/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;/p&gt;&lt;img src="http://feeds.feedburner.com/~r/blogspot/MJbOI/~4/5aIUbkn1znE" height="1" width="1"/&gt;</content><link rel="replies" type="application/atom+xml" href="http://cpp-today.blogspot.com/feeds/2799767212294860004/comments/default" title="Post Comments" /><link rel="replies" type="text/html" href="http://www.blogger.com/comment.g?blogID=3197434572108789628&amp;postID=2799767212294860004" title="0 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/3197434572108789628/posts/default/2799767212294860004?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/3197434572108789628/posts/default/2799767212294860004?v=2" /><link rel="alternate" type="text/html" href="http://feedproxy.google.com/~r/blogspot/MJbOI/~3/5aIUbkn1znE/threading-mess.html" title="Threading mess!" /><author><name>Gaetano</name><uri>http://www.blogger.com/profile/03637292910769902384</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="16" height="16" src="http://img2.blogblog.com/img/b16-rounded.gif" /></author><thr:total>0</thr:total><feedburner:origLink>http://cpp-today.blogspot.com/2008/06/threading-mess.html</feedburner:origLink></entry><entry gd:etag="W/&quot;A0QFQX45eyp7ImA9WxRbGUs.&quot;"><id>tag:blogger.com,1999:blog-3197434572108789628.post-5988772358219270856</id><published>2008-05-31T00:56:00.001-07:00</published><updated>2008-12-10T20:21:50.023-08:00</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2008-12-10T20:21:50.023-08:00</app:edited><category scheme="http://www.blogger.com/atom/ns#" term="performance" /><category scheme="http://www.blogger.com/atom/ns#" term="c++" /><category scheme="http://www.blogger.com/atom/ns#" term="thread" /><category scheme="http://www.blogger.com/atom/ns#" term="false sharing" /><title>False Sharing hits again!</title><content type="html">You can ask what "false sharing" is? False sharing is an annoying effect that occurs when two processors apparently do not share any resource but due to undergoing hardware architecture they actually do. For example consider two threads writing in not overlapping memory locations, they do not need any synchronization so happily you are driven to think that you are able to split your data in order to implement a lock less algorithm. Unfortunately you hold a sparkling "centrino duo" processor in where both cores do share the L2 cache and then the data you partition in memory can be mapped on the same cache line. The same scenario is triggered on L1 caches, indeed due to coherency protocols if a thread write to a cache line then the cache line referring the same memory is invalidated on the other processor (cache trashing).&lt;br /&gt;&lt;br /&gt;Consider for example the following case:&lt;br /&gt;&lt;br /&gt;&lt;pre   style="border: 1px dashed ; overflow: auto; color: rgb(0, 0, 0); background-color: rgb(238, 238, 238);font-family:Andale Mono,Lucida Console,Monaco,fixed,monospace;font-size:12px;"&gt;&lt;br /&gt;char a[10];&lt;br /&gt;char b[10];&lt;br /&gt;&lt;br /&gt;start_thread_that_works_on_a;&lt;br /&gt;start_thread_that_works_on_b;&lt;br /&gt;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;Very likely that 20 bytes will lie on contiguous memory location and then will be mapped inside the same single cache line, so each time a thread works on his own vector it invalidates the cache line for the other thread and then the hardware has to write and fetch the cache line in and from memory even if it is not strictly needed.&lt;br /&gt;&lt;br /&gt;I've had to work on an algorithm that had that very issue and the following code, even if useless, exploits the same problem:&lt;br /&gt;&lt;br /&gt;&lt;pre style="border: 1px dashed ; overflow: auto; color: rgb(0, 0, 0); background-color: rgb(238, 238, 238); font-family: Andale Mono,Lucida Console,Monaco,fixed,monospace; font-size: 12px;"&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 128, 0);"&gt;#include &amp;lt;iostream&amp;gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 128, 0);"&gt;#include &amp;lt;boost/thread.hpp&amp;gt;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="font-weight: bold; color: rgb(0, 0, 0);"&gt;class&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; threadS {&lt;/span&gt;&lt;br /&gt;&lt;span style="font-weight: bold; color: rgb(0, 0, 0);"&gt;public&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;:&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;   threadS(&lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;unsigned&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; &lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;char&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; *aVector, &lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;unsigned&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; &lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;int&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; aVSize) &lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;   :theVector(aVector),&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;    theVSize(aVSize)&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;   { }&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;   &lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;void&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; &lt;/span&gt;&lt;span style="font-weight: bold; color: rgb(0, 0, 0);"&gt;operator&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;()() {&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;      &lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;unsigned&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; &lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;long&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; &lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;long&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; myCounter = &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;100000000&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;;&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;      &lt;/span&gt;&lt;span style="font-weight: bold; color: rgb(0, 0, 0);"&gt;while&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;(--myCounter) {&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;          &lt;/span&gt;&lt;span style="font-weight: bold; color: rgb(0, 0, 0);"&gt;for&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; (&lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;int&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; i=&lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;0&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;; i&amp;lt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;10&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;; ++i) {&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;              ++theVector[i];&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;          }&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;      }&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;   }&lt;/span&gt;&lt;br /&gt;&lt;span style="font-weight: bold; color: rgb(0, 0, 0);"&gt;private&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;:&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;   &lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;unsigned&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; &lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;char&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;* theVector; &lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;   &lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;unsigned&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; &lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;int&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;   theVSize; &lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;};&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;int&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; main() &lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;{&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;   &lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;unsigned&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; &lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;char&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; vectorA[&lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;10&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;]; &lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;   &lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;unsigned&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; &lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;char&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; vectorB[&lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;10&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;];&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;   std::cout &amp;lt;&amp;lt; std::hex;&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;   std::cout &amp;lt;&amp;lt; &lt;/span&gt;&lt;span style="color: rgb(221, 0, 0);"&gt;"A:["&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; &amp;lt;&amp;lt;  (&lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;int&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;)&amp;amp;vectorA[&lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;0&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;] &amp;lt;&amp;lt; &lt;/span&gt;&lt;span style="color: rgb(221, 0, 0);"&gt;"-"&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; &amp;lt;&amp;lt; (&lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;int&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;)&amp;amp;vectorA[&lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;9&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;] &amp;lt;&amp;lt; &lt;/span&gt;&lt;span style="color: rgb(221, 0, 0);"&gt;"]"&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; &amp;lt;&amp;lt; std::endl;&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;   std::cout &amp;lt;&amp;lt; &lt;/span&gt;&lt;span style="color: rgb(221, 0, 0);"&gt;"B:["&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; &amp;lt;&amp;lt;  (&lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;int&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;)&amp;amp;vectorB[&lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;0&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;] &amp;lt;&amp;lt; &lt;/span&gt;&lt;span style="color: rgb(221, 0, 0);"&gt;"-"&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; &amp;lt;&amp;lt; (&lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;int&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;)&amp;amp;vectorB[&lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;9&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;] &amp;lt;&amp;lt; &lt;/span&gt;&lt;span style="color: rgb(221, 0, 0);"&gt;"]"&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; &amp;lt;&amp;lt; std::endl;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;   threadS threadA(vectorA, &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;10&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;);&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;   threadS threadB(vectorB, &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;10&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;);&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;   boost::thread_group tg;&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;   tg.create_thread(threadA);&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;   tg.create_thread(threadB);&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;   tg.join_all();&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;}&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;You should be able to compile and link it with:&lt;br /&gt;&lt;br /&gt;g++ main.cpp -o false_sharing -lboost_thread -O3&lt;br /&gt;&lt;br /&gt;Let see what that codes does.&lt;br /&gt;The class threadS stores the vector on which it will operate. The thread body (the operator()) just increases all the vector elements. As you can see I used the boost thread library to start two threads: threadA and threadB.&lt;br /&gt;&lt;br /&gt;On my system I obtain an execution time that goes from 6 to 8 seconds and the following boundaries for the two vectors:&lt;br /&gt;&lt;br /&gt;B:[bfa7d010-bfa7d019]  -   A:[bfa7d01a-bfa7d023]&lt;br /&gt;&lt;br /&gt;as you can see vectorB and vectorA are at contiguous memory locations.&lt;br /&gt;&lt;br /&gt;How to eliminate the false sharing in this case? The objective is to have both threads working on an underling different cache line, we can achieve this goal separating both data with some extra bytes. Declaring the two vector bigger than we need it's a dirty and quick way to do it, executing the same program with the following vector declaration:&lt;br /&gt;&lt;br /&gt;&lt;pre face="Andale Mono,Lucida Console,Monaco,fixed,monospace" size="12px" style="border: 1px dashed ; overflow: auto; color: rgb(0, 0, 0); background-color: rgb(238, 238, 238);"&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;   &lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;unsigned&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; &lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;char&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; vectorA[&lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;100&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;]; &lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;   &lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;unsigned&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; &lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;char&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; vectorB[&lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;100&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;];&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;I'm able to obtain an execution time that goes from 1 to 1.5 seconds.&lt;br /&gt;&lt;br /&gt;Same problem would happen with a single vector "unsigned char vector[1000]" but with a thread working on elements [0,10] and another thread working on elements [11,20]. I wrote a simple program that creates two threads,one performs writes at locations [0,10] and the other that performs writes at [z,z+10] with z inside the interval [11,100]. The following graph shows the execution time while the bytes of separation between the two data increase; z=11 means that data have a 0 bytes separation.&lt;br /&gt;&lt;br /&gt;&lt;a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="http://4.bp.blogspot.com/_xxyADQu8yXQ/SEF_lhh2sgI/AAAAAAAAAAM/nUC3Twfw5nE/s1600-h/latency.png"&gt;&lt;img style="margin: 0px auto 10px; display: block; text-align: center; cursor: pointer;" src="http://4.bp.blogspot.com/_xxyADQu8yXQ/SEF_lhh2sgI/AAAAAAAAAAM/nUC3Twfw5nE/s320/latency.png" alt="" id="BLOGGER_PHOTO_ID_5206582926849257986" border="0" /&gt;&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;As you can see as soon the two data "false shared" have 51 bytes separation the execution time collapses from ~8 secs to ~1.8 secs, so just wasting 51 bytes I'm able to obtain a x5 speed up, nice isn't it?&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/3197434572108789628-5988772358219270856?l=cpp-today.blogspot.com' alt='' /&gt;&lt;/div&gt;
&lt;p&gt;&lt;a href="http://feedads.g.doubleclick.net/~a/UQrzZRV6kBmPJS2zg_s9i7lk7Mw/0/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/UQrzZRV6kBmPJS2zg_s9i7lk7Mw/0/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;br/&gt;
&lt;a href="http://feedads.g.doubleclick.net/~a/UQrzZRV6kBmPJS2zg_s9i7lk7Mw/1/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/UQrzZRV6kBmPJS2zg_s9i7lk7Mw/1/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;/p&gt;&lt;img src="http://feeds.feedburner.com/~r/blogspot/MJbOI/~4/FVL35x59IGE" height="1" width="1"/&gt;</content><link rel="replies" type="application/atom+xml" href="http://cpp-today.blogspot.com/feeds/5988772358219270856/comments/default" title="Post Comments" /><link rel="replies" type="text/html" href="http://www.blogger.com/comment.g?blogID=3197434572108789628&amp;postID=5988772358219270856" title="0 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/3197434572108789628/posts/default/5988772358219270856?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/3197434572108789628/posts/default/5988772358219270856?v=2" /><link rel="alternate" type="text/html" href="http://feedproxy.google.com/~r/blogspot/MJbOI/~3/FVL35x59IGE/false-sharing-hits-again.html" title="False Sharing hits again!" /><author><name>Gaetano</name><uri>http://www.blogger.com/profile/03637292910769902384</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="16" height="16" src="http://img2.blogblog.com/img/b16-rounded.gif" /></author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="http://4.bp.blogspot.com/_xxyADQu8yXQ/SEF_lhh2sgI/AAAAAAAAAAM/nUC3Twfw5nE/s72-c/latency.png" height="72" width="72" /><thr:total>0</thr:total><feedburner:origLink>http://cpp-today.blogspot.com/2008/05/false-sharing-hits-again.html</feedburner:origLink></entry><entry gd:etag="W/&quot;CEMEQ30_cCp7ImA9WxdQGE8.&quot;"><id>tag:blogger.com,1999:blog-3197434572108789628.post-1159331920579165593</id><published>2007-11-26T07:30:00.000-08:00</published><updated>2008-06-18T13:53:22.348-07:00</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2008-06-18T13:53:22.348-07:00</app:edited><category scheme="http://www.blogger.com/atom/ns#" term="noob" /><category scheme="http://www.blogger.com/atom/ns#" term="c++" /><category scheme="http://www.blogger.com/atom/ns#" term="correctness" /><title>A local variable is "local" after all</title><content type="html">I have found a code that "sounds" like this:&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;pre   style="border: 1px dashed rgb(153, 153, 153); padding: 5px; overflow: auto; color: rgb(0, 0, 0); background-color: rgb(238, 238, 238); line-height: 14px; width: 100%;font-family:Andale Mono,Lucida Console,Monaco,fixed,monospace;font-size:12px;"&gt;&lt;br /&gt;&lt;span style="font-weight: bold; color: rgb(0, 0, 0);"&gt;struct&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; StructureC {&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;   &lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;char&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; * theString;&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;};&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="font-weight: bold; color: rgb(0, 0, 0);"&gt;class&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; ClassCPP : &lt;/span&gt;&lt;span style="font-weight: bold; color: rgb(0, 0, 0);"&gt;public&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; StructureC {&lt;/span&gt;&lt;br /&gt;&lt;span style="font-weight: bold; color: rgb(0, 0, 0);"&gt;public&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;:&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;   ClassCPP();&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;   ClassCPP(&lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;const&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; ClassCPP&amp;amp; aClass);&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;   ~ClassCPP();&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;};&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;ClassCPP::ClassCPP() {&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;   std::string myString = foo();&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;   theString = myString.c_str();&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;}&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;ClassCPP::~ClassCPP() {&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;}&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;ClassCPP::ClassCPP(&lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;const&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; ClassCPP&amp;amp; aClass) {&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;   theString = aClass.theString;&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;}&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;someone failed here, badly!&lt;br /&gt;&lt;br /&gt;Let's see.&lt;br /&gt;&lt;br /&gt;Of course the code above is not useful, it was simplified and extracted from a real case; StructureC can not be changed at all.&lt;br /&gt;&lt;br /&gt;std::string has the method c_str() that returns a null terminated sequence of characters (same content as std::string) and it points to an internal location of std::string, when the scope of myString (note how I use the prefix "my" for local variables) ends, any reference to its internal status is then not valid, unfortunately theString is a member of the object being constructed. The code can "work" but then you are very lucky if it does.&lt;br /&gt;&lt;br /&gt;The correct way is to allocate memory in the constructor and then copy the character sequence:&lt;br /&gt;&lt;br /&gt;&lt;pre   style="border: 1px dashed rgb(153, 153, 153); padding: 5px; overflow: auto; color: rgb(0, 0, 0); background-color: rgb(238, 238, 238); line-height: 14px; width: 100%;font-family:Andale Mono,Lucida Console,Monaco,fixed,monospace;font-size:12px;"&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;ClassCPP::ClassCPP() {&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;  std::string myString = foo();&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;  theString = &lt;/span&gt;&lt;span style="font-weight: bold; color: rgb(0, 0, 0);"&gt;new&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; &lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;char&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;[myString.size()+&lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;1&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;]; &lt;/span&gt;&lt;span style="font-style: italic; color: rgb(128, 128, 128);"&gt;//+1 to store the null termination&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;  strcpy(theString, myString.c_str());&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;}&lt;/span&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;so, you think that's all, don't you?&lt;br /&gt;&lt;br /&gt;Still some errors left.&lt;br /&gt;&lt;br /&gt;&lt;span style="font-style: italic;"&gt;Copy&lt;/span&gt; constructor shall be rewritten, if we leave it unchanged like this&lt;br /&gt;&lt;br /&gt;&lt;pre   style="border: 1px dashed rgb(153, 153, 153); padding: 5px; overflow: auto; color: rgb(0, 0, 0); background-color: rgb(238, 238, 238); line-height: 14px; width: 100%;font-family:Andale Mono,Lucida Console,Monaco,fixed,monospace;font-size:12px;"&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;ClassCPP::ClassCPP(&lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;const&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; ClassCPP&amp;amp;amp; aClass) {&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;  theString = aClass.theString;&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;}&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;then as soon we copy an object of type ClassCPP we will have two instances that are pointing to the same memory area (the one that contains the string).&lt;br /&gt;&lt;br /&gt;So it'd be better to write it in the correct way:&lt;br /&gt;&lt;br /&gt;&lt;pre   style="border: 1px dashed rgb(153, 153, 153); padding: 5px; overflow: auto; color: rgb(0, 0, 0); background-color: rgb(238, 238, 238); line-height: 14px; width: 100%;font-family:Andale Mono,Lucida Console,Monaco,fixed,monospace;font-size:12px;"&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;ClassCPP::ClassCPP(&lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;const&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; ClassCPP&amp;amp;amp; aClass) {&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;  theString = &lt;/span&gt;&lt;span style="font-weight: bold; color: rgb(0, 0, 0);"&gt;new&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; &lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;char&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;[strlen(aClass.theString)+&lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;1&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;];&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;  strcpy(theString, aClass.theString);  &lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;}&lt;/span&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;I could have used a strdup but strdup uses malloc to allocate memory.&lt;br /&gt;&lt;br /&gt;Having done dynamic allocation of memory the destructor can not be void, we need to release the memory allocated:&lt;br /&gt;&lt;br /&gt;&lt;pre   style="border: 1px dashed rgb(153, 153, 153); padding: 5px; overflow: auto; color: rgb(0, 0, 0); background-color: rgb(238, 238, 238); line-height: 14px; width: 100%;font-family:Andale Mono,Lucida Console,Monaco,fixed,monospace;font-size:12px;"&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;ClassCPP::~ClassCPP() {&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;  &lt;/span&gt;&lt;span style="font-weight: bold; color: rgb(0, 0, 0);"&gt;delete&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; []theString;&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;}&lt;/span&gt;&lt;/pre&gt;&lt;br /&gt;still some problems left, incredible how many errors can be done in a few lines of code!&lt;br /&gt;&lt;br /&gt;&lt;span style="font-style: italic;"&gt;Assignment &lt;/span&gt;operator shall be written as well but in this case we can declare it&lt;br /&gt;private and not implement it:&lt;br /&gt;&lt;br /&gt;&lt;pre   style="border: 1px dashed rgb(153, 153, 153); padding: 5px; overflow: auto; color: rgb(0, 0, 0); background-color: rgb(238, 238, 238); line-height: 14px; width: 100%;font-family:Andale Mono,Lucida Console,Monaco,fixed,monospace;font-size:12px;"&gt;&lt;br /&gt;&lt;span style="font-weight: bold; color: rgb(0, 0, 0);"&gt;class&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; ClassCPP : &lt;/span&gt;&lt;span style="font-weight: bold; color: rgb(0, 0, 0);"&gt;public&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; StructureC {&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;  ...&lt;/span&gt;&lt;br /&gt;&lt;span style="font-weight: bold; color: rgb(0, 0, 0);"&gt;private&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;:&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;  &lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;const&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; ClassCPP &amp;amp;amp; &lt;/span&gt;&lt;span style="font-weight: bold; color: rgb(0, 0, 0);"&gt;operator&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;=(&lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;const&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; ClassCPP&amp;amp;amp;);&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;};&lt;/span&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;in this way we can check if someone needs it (the original implementation didn't&lt;br /&gt;have it implemented so I guess the intention was: "I don't need it") and if necessary&lt;br /&gt;implement it.&lt;br /&gt;&lt;br /&gt;The problems are not over yet, look at the following usage of that code:&lt;br /&gt;&lt;br /&gt;&lt;pre   style="border: 1px dashed rgb(153, 153, 153); padding: 5px; overflow: auto; color: rgb(0, 0, 0); background-color: rgb(238, 238, 238); line-height: 14px; width: 100%;font-family:Andale Mono,Lucida Console,Monaco,fixed,monospace;font-size:12px;"&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;StructureC *a = &lt;/span&gt;&lt;span style="font-weight: bold; color: rgb(0, 0, 0);"&gt;new&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; ClassCPP;&lt;/span&gt;&lt;br /&gt;&lt;span style="font-weight: bold; color: rgb(0, 0, 0);"&gt;delete&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; a;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;as you can see deleting an instance of ClassCPP through a pointer to its base class will not call the ClassCPP destructor. Then we need to declare the destructor of StructureC virtual but given the fact we can not change StructureC then we need avoiding someone being able to build a ClassCPP in the heap memory. This can be done declaring and not implementing the operator new in the private part of class.&lt;br /&gt;&lt;br /&gt;&lt;pre   style="border: 1px dashed rgb(153, 153, 153); padding: 5px; overflow: auto; color: rgb(0, 0, 0); background-color: rgb(238, 238, 238); line-height: 14px; width: 100%;font-family:Andale Mono,Lucida Console,Monaco,fixed,monospace;font-size:12px;"&gt;&lt;br /&gt;&lt;span style="font-weight: bold; color: rgb(0, 0, 0);"&gt;class&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; ClassCPP : &lt;/span&gt;&lt;span style="font-weight: bold; color: rgb(0, 0, 0);"&gt;public&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; StructureC {&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;  ...&lt;/span&gt;&lt;br /&gt;&lt;span style="font-weight: bold; color: rgb(0, 0, 0);"&gt;private&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;:&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;  ...&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;  &lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;void&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; * &lt;/span&gt;&lt;span style="font-weight: bold; color: rgb(0, 0, 0);"&gt;operator&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; &lt;/span&gt;&lt;span style="font-weight: bold; color: rgb(0, 0, 0);"&gt;new&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;(std::size_t);&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;  &lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;void&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; * &lt;/span&gt;&lt;span style="font-weight: bold; color: rgb(0, 0, 0);"&gt;operator&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; &lt;/span&gt;&lt;span style="font-weight: bold; color: rgb(0, 0, 0);"&gt;new&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;(std::size_t, &lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;void&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; *);&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;};&lt;/span&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;as you can see in order to avoid any mistake better to disable the in-place operator&lt;br /&gt;new as well.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/3197434572108789628-1159331920579165593?l=cpp-today.blogspot.com' alt='' /&gt;&lt;/div&gt;
&lt;p&gt;&lt;a href="http://feedads.g.doubleclick.net/~a/BZ48CWI7gpCojLyh2cb4PA_edbw/0/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/BZ48CWI7gpCojLyh2cb4PA_edbw/0/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;br/&gt;
&lt;a href="http://feedads.g.doubleclick.net/~a/BZ48CWI7gpCojLyh2cb4PA_edbw/1/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/BZ48CWI7gpCojLyh2cb4PA_edbw/1/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;/p&gt;&lt;img src="http://feeds.feedburner.com/~r/blogspot/MJbOI/~4/pKo8wtBURG4" height="1" width="1"/&gt;</content><link rel="replies" type="application/atom+xml" href="http://cpp-today.blogspot.com/feeds/1159331920579165593/comments/default" title="Post Comments" /><link rel="replies" type="text/html" href="http://www.blogger.com/comment.g?blogID=3197434572108789628&amp;postID=1159331920579165593" title="0 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/3197434572108789628/posts/default/1159331920579165593?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/3197434572108789628/posts/default/1159331920579165593?v=2" /><link rel="alternate" type="text/html" href="http://feedproxy.google.com/~r/blogspot/MJbOI/~3/pKo8wtBURG4/local-variable-is-local-after-all.html" title="A local variable is &quot;local&quot; after all" /><author><name>Gaetano</name><uri>http://www.blogger.com/profile/03637292910769902384</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="16" height="16" src="http://img2.blogblog.com/img/b16-rounded.gif" /></author><thr:total>0</thr:total><feedburner:origLink>http://cpp-today.blogspot.com/2007/11/local-variable-is-local-after-all.html</feedburner:origLink></entry><entry gd:etag="W/&quot;DEQMR34zfCp7ImA9WxdRFEU.&quot;"><id>tag:blogger.com,1999:blog-3197434572108789628.post-4351844331908855909</id><published>2006-12-15T09:19:00.000-08:00</published><updated>2008-06-03T02:46:26.084-07:00</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2008-06-03T02:46:26.084-07:00</app:edited><category scheme="http://www.blogger.com/atom/ns#" term="c++" /><category scheme="http://www.blogger.com/atom/ns#" term="exception safety" /><title>Exceptions (part 2)</title><content type="html">So we have seen on my last post that throwing exceptions and functions call have some similarities, we have also seen that throw an object always means copy it even if we catch for reference, as all copies in C++ are based on the static type then even in the exceptions environments the objects thrown are base on the static type.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Not always throw an exception assures you to avoid memory leakages even using "automatic objects", consider this example:&lt;br /&gt;&lt;br /&gt;&lt;pre face="Andale Mono,Lucida Console,Monaco,fixed,monospace" size="12px" style="border: 1px dashed rgb(153, 153, 153); padding: 5px; overflow: auto; color: rgb(0, 0, 0); background-color: rgb(238, 238, 238); line-height: 14px; width: 100%;"&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;class Foo {&lt;br /&gt;  public:&lt;br /&gt;    Foo();&lt;br /&gt;    ~Foo()&lt;br /&gt;  private:&lt;br /&gt;    AType* aPointer;&lt;br /&gt;    BType* bPointer;&lt;br /&gt;};&lt;br /&gt;&lt;br /&gt;Foo::Foo()&lt;br /&gt;:aPointer(new AType),&lt;br /&gt; bPointer(new BType)&lt;br /&gt;{ }&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;we know that the initializer list order depends on the order declaration in the definition of class,&lt;br /&gt;so in this case aPointer is initialized first then bPointer. What happens if the "new BType" throws an exception? Well, given the fact aPointer is a plain pointer then we will have memory leakage. So a first thought can be to use not plain pointers but something like "smart pointer".&lt;br /&gt;So a first approach can be the following:&lt;br /&gt;&lt;br /&gt;&lt;pre style="border: 1px dashed rgb(153, 153, 153); padding: 5px; overflow: auto; font-family: Andale Mono,Lucida Console,Monaco,fixed,monospace; color: rgb(0, 0, 0); background-color: rgb(238, 238, 238); font-size: 12px; line-height: 14px; width: 100%;"&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;class Foo {&lt;br /&gt;  public:&lt;br /&gt;    Foo();&lt;br /&gt;    ~Foo()&lt;br /&gt;  private:&lt;br /&gt;    std::auto_ptr aPointer;&lt;br /&gt;    std::auto_ptr&lt;btype&gt; bPointer;&lt;br /&gt;};&lt;br /&gt;&lt;br /&gt;Foo::Foo()&lt;br /&gt;:aPointer(new AType),&lt;br /&gt; bPointer(new BType)&lt;br /&gt;{ }&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;/pre&gt; &lt;br /&gt;well this is still not safe. Let see the constructor execution sequence:&lt;br /&gt;&lt;br /&gt;1) Constructor AType is executed (new AType)&lt;br /&gt;2) Constructor BType is executed (new BType)&lt;br /&gt;3) Constructor std::auto_ptr&lt;atype&gt; is executed ( aPointer( ... ) )&lt;br /&gt;4) Constructor std::auto_ptr&lt;btype&gt; is executed ( bPointer( ... ) )&lt;br /&gt;&lt;br /&gt;do you see know where the problem is? If still "new BType" throws an exception the address of memory allocated by new AType was still not saved anywhere; unfortunately the correct way to solve this problem is the following:&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;pre style="border: 1px dashed rgb(153, 153, 153); padding: 5px; overflow: auto; font-family: Andale Mono,Lucida Console,Monaco,fixed,monospace; color: rgb(0, 0, 0); background-color: rgb(238, 238, 238); font-size: 12px; line-height: 14px; width: 100%;"&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;class Foo {&lt;br /&gt;  public:&lt;br /&gt;    Foo();&lt;br /&gt;    ~Foo()&lt;br /&gt;  private:&lt;br /&gt;    std::auto_ptr&lt;atype&gt; aPointer;&lt;br /&gt;    std::auto_ptr&lt;btype&gt; bPointer;&lt;br /&gt;};&lt;br /&gt;&lt;br /&gt;Foo::Foo()&lt;br /&gt;:aPointer(),&lt;br /&gt; bPointer()&lt;br /&gt;{&lt;br /&gt;  aPointer = std::auto_ptr&lt;atype&gt;(new AType);&lt;br /&gt;  bPointer = std::auto_ptr&lt;btype&gt;(new BType);&lt;br /&gt;}&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;/pre&gt; &lt;br /&gt;&lt;br /&gt;throwing an exception can also leave the object in an inconsistent state, consider the following class (do not consider the fact that the class is useless):&lt;br /&gt;&lt;br /&gt;&lt;pre style="border: 1px dashed rgb(153, 153, 153); padding: 5px; overflow: auto; font-family: Andale Mono,Lucida Console,Monaco,fixed,monospace; color: rgb(0, 0, 0); background-color: rgb(238, 238, 238); font-size: 12px; line-height: 14px; width: 100%;"&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;class Foo {&lt;br /&gt;  public:&lt;br /&gt;    Foo()&lt;br /&gt;    :theStorage()&lt;br /&gt;    { }&lt;br /&gt;&lt;br /&gt;    addInt(int anInteger) {&lt;br /&gt;      theStorage.push_back(anInteger);&lt;br /&gt;    }&lt;br /&gt;&lt;br /&gt;    void sumOne() {&lt;br /&gt;      int i;&lt;br /&gt;      for (i=0; i &lt; theStorage.size(); ++i) {&lt;br /&gt;      theStorage[i] += 1;&lt;br /&gt;      if (i==2) {&lt;br /&gt;         throw std::runtime_error("OPS!");&lt;br /&gt;      }&lt;br /&gt;    }&lt;br /&gt;&lt;br /&gt;  private:&lt;br /&gt;    std::vector&lt;int&gt; theStorage;&lt;br /&gt;};&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;/pre&gt; &lt;br /&gt;&lt;br /&gt;and his usage:&lt;br /&gt;&lt;br /&gt;&lt;pre style="border: 1px dashed rgb(153, 153, 153); padding: 5px; overflow: auto; font-family: Andale Mono,Lucida Console,Monaco,fixed,monospace; color: rgb(0, 0, 0); background-color: rgb(238, 238, 238); font-size: 12px; line-height: 14px; width: 100%;"&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;Foo aFoo;&lt;br /&gt;&lt;br /&gt;aFoo.addInt(0);&lt;br /&gt;aFoo.addInt(1);&lt;br /&gt;aFoo.addInt(2);&lt;br /&gt;aFoo.addInt(3);&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;/pre&gt; &lt;br /&gt;&lt;br /&gt;at this point calling:&lt;br /&gt;&lt;br /&gt;&lt;pre style="border: 1px dashed rgb(153, 153, 153); padding: 5px; overflow: auto; font-family: Andale Mono,Lucida Console,Monaco,fixed,monospace; color: rgb(0, 0, 0); background-color: rgb(238, 238, 238); font-size: 12px; line-height: 14px; width: 100%;"&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;aFoo.sumOne();&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;/pre&gt; &lt;br /&gt;&lt;br /&gt;will throw an exception leaving aFoo with partial updated elements, and from user point of view the aFoo is in an inconsistent state, so the sumOne() function shows here another problem that can break the exception safety of a class. The solution on this kind of problems is to work on a copy of internal class state and then make a swap between the internal state and the modified state.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/3197434572108789628-4351844331908855909?l=cpp-today.blogspot.com' alt='' /&gt;&lt;/div&gt;
&lt;p&gt;&lt;a href="http://feedads.g.doubleclick.net/~a/r4SvcQ_1c8cwObdwzCgBEuqW8p0/0/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/r4SvcQ_1c8cwObdwzCgBEuqW8p0/0/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;br/&gt;
&lt;a href="http://feedads.g.doubleclick.net/~a/r4SvcQ_1c8cwObdwzCgBEuqW8p0/1/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/r4SvcQ_1c8cwObdwzCgBEuqW8p0/1/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;/p&gt;&lt;img src="http://feeds.feedburner.com/~r/blogspot/MJbOI/~4/KPUXgMxPAfE" height="1" width="1"/&gt;</content><link rel="replies" type="application/atom+xml" href="http://cpp-today.blogspot.com/feeds/4351844331908855909/comments/default" title="Post Comments" /><link rel="replies" type="text/html" href="http://www.blogger.com/comment.g?blogID=3197434572108789628&amp;postID=4351844331908855909" title="0 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/3197434572108789628/posts/default/4351844331908855909?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/3197434572108789628/posts/default/4351844331908855909?v=2" /><link rel="alternate" type="text/html" href="http://feedproxy.google.com/~r/blogspot/MJbOI/~3/KPUXgMxPAfE/exceptions-part-2.html" title="Exceptions (part 2)" /><author><name>Gaetano</name><uri>http://www.blogger.com/profile/03637292910769902384</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="16" height="16" src="http://img2.blogblog.com/img/b16-rounded.gif" /></author><thr:total>0</thr:total><feedburner:origLink>http://cpp-today.blogspot.com/2006/12/exceptions-part-2.html</feedburner:origLink></entry><entry gd:etag="W/&quot;D04ARnw9cSp7ImA9WxdRFEU.&quot;"><id>tag:blogger.com,1999:blog-3197434572108789628.post-1469592847868554546</id><published>2006-11-28T03:19:00.000-08:00</published><updated>2008-06-03T02:39:07.269-07:00</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2008-06-03T02:39:07.269-07:00</app:edited><category scheme="http://www.blogger.com/atom/ns#" term="exception safety" /><title>Exceptions (part 1)</title><content type="html">As  you already know a modern way to deal with "errors" in C++ is the exception handling; however you need to be careful on using this mechanism. Let see how it works and some tips as well.&lt;br /&gt;The exception mechanism is based on the &lt;span style="font-style: italic;"&gt;&lt;span style="font-weight: bold;"&gt;try - catch &lt;/span&gt;&lt;/span&gt;blocks:&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;pre face="Andale Mono,Lucida Console,Monaco,fixed,monospace" size="12px" style="border: 1px dashed rgb(153, 153, 153); padding: 5px; overflow: auto; color: rgb(0, 0, 0); background-color: rgb(238, 238, 238); line-height: 14px; width: 100%;"&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;try {&lt;br /&gt;  //some code in here that we &lt;span style="font-style: italic;"&gt;attempt &lt;/span&gt;to execute&lt;br /&gt;}&lt;br /&gt;catch (...) {&lt;br /&gt;  // this block is the error handling, the code in this &lt;br /&gt;  // block is executed if the code in the&lt;br /&gt;  // try block above have thrown an exception&lt;br /&gt;}&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;easy and net.&lt;br /&gt;&lt;br /&gt;Let see how throw an exception so we can see more in depth what this mechanism offers, how use it, what to avoid.&lt;br /&gt;An exception is thrown with a &lt;span style="font-style: italic;"&gt;&lt;span style="font-weight: bold;"&gt;throw:&lt;/span&gt;&lt;/span&gt;&lt;span&gt;&lt;span&gt;&lt;br /&gt;&lt;br /&gt;&lt;pre face="Andale Mono,Lucida Console,Monaco,fixed,monospace" size="12px" style="border: 1px dashed rgb(153, 153, 153); padding: 5px; overflow: auto; color: rgb(0, 0, 0); background-color: rgb(238, 238, 238); line-height: 14px; width: 100%;"&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;throw A;&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;where A is the type of the object thrown, in that case we are throwing an object of type A inizialized with his default constructor, we could have done:&lt;br /&gt;&lt;br /&gt;&lt;pre face="Andale Mono,Lucida Console,Monaco,fixed,monospace" size="12px" style="border: 1px dashed rgb(153, 153, 153); padding: 5px; overflow: auto; color: rgb(0, 0, 0); background-color: rgb(238, 238, 238); line-height: 14px; width: 100%;"&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;throw A(3, "foo");&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;or even:&lt;br /&gt;&lt;br /&gt;&lt;pre face="Andale Mono,Lucida Console,Monaco,fixed,monospace" size="12px" style="border: 1px dashed rgb(153, 153, 153); padding: 5px; overflow: auto; color: rgb(0, 0, 0); background-color: rgb(238, 238, 238); line-height: 14px; width: 100%;"&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;A a(3, "foo");&lt;br /&gt;throw a;&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;The catch(...) { } handler is supposed to handle all kind of exception that code inside the try block throws, in this way we lose the kind of exception thrown so it's a bit reductive because the error handler doesn't have a clue on what is going on; fortunately is possible to specify wich kind of exceptions we want to manage (we as well ignore some).&lt;br /&gt;Specify which kind of exception we want manage is done in this way:&lt;br /&gt;&lt;br /&gt;&lt;pre face="Andale Mono,Lucida Console,Monaco,fixed,monospace" size="12px" style="border: 1px dashed rgb(153, 153, 153); padding: 5px; overflow: auto; color: rgb(0, 0, 0); background-color: rgb(238, 238, 238); line-height: 14px; width: 100%;"&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;catch(Foo f) {&lt;br /&gt;}&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;we can have multiple catch blocks after a try:&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;pre face="Andale Mono,Lucida Console,Monaco,fixed,monospace" size="12px" style="border: 1px dashed rgb(153, 153, 153); padding: 5px; overflow: auto; color: rgb(0, 0, 0); background-color: rgb(238, 238, 238); line-height: 14px; width: 100%;"&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;try {&lt;br /&gt;  //code we are attempting to execute&lt;br /&gt;}&lt;br /&gt;catch(Foo f) {&lt;br /&gt;  // error handler in case a Foo type was thrown&lt;br /&gt;}&lt;br /&gt;catch(Bar b) {&lt;br /&gt;   // error handler in case a Bar type was thrown&lt;br /&gt;}&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;if the code in the try block thrown an exception that is not Foo and neither Bar then is like we are not using the try-catch blocks ( apart the introduction of the try's extra scope ).&lt;br /&gt;&lt;br /&gt;We can obtain the same behaviour (logging for example we were not able to catch any expected exception) in this way:&lt;br /&gt;&lt;br /&gt;&lt;pre face="Andale Mono,Lucida Console,Monaco,fixed,monospace" size="12px" style="border: 1px dashed rgb(153, 153, 153); padding: 5px; overflow: auto; color: rgb(0, 0, 0); background-color: rgb(238, 238, 238); line-height: 14px; width: 100%;"&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;try {&lt;br /&gt;  //code we are attempting to execute&lt;br /&gt;}&lt;br /&gt;catch(Foo f) {&lt;br /&gt;  // error handler in case a Foo type was thrown&lt;br /&gt;}&lt;br /&gt;catch(Bar b) {&lt;br /&gt;  // error handler in case a Bar type was thrown&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;}&lt;br /&gt;catch(...) {&lt;br /&gt;  // log the event in here&lt;br /&gt;  throw;  // this throws again the same exception.&lt;br /&gt;}&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;There is still something behind all this. As you have seen the &lt;span style="font-weight: bold;"&gt;catch &lt;/span&gt;blocks are very similar to a function declaration where the arguments are passed by value.&lt;br /&gt;The &lt;span style="font-weight: bold;"&gt;catch &lt;/span&gt;blocks can have indeed all kind of parameters:&lt;br /&gt;&lt;br /&gt;catch( T )&lt;br /&gt;catch( T &amp; )&lt;br /&gt;catch( T * )&lt;br /&gt;catch( const T )&lt;br /&gt;catch( const T &amp;amp; )&lt;br /&gt;catch( const T * )&lt;br /&gt;&lt;br /&gt;so you can think of throwing an exception have same effect of calling a function, but is not.&lt;br /&gt;Consider this code:&lt;br /&gt;&lt;br /&gt;&lt;pre face="Andale Mono,Lucida Console,Monaco,fixed,monospace" size="12px" style="border: 1px dashed rgb(153, 153, 153); padding: 5px; overflow: auto; color: rgb(0, 0, 0); background-color: rgb(238, 238, 238); line-height: 14px; width: 100%;"&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;void foo()&lt;br /&gt;{&lt;br /&gt;  ...&lt;br /&gt;  A aLocalObject;&lt;br /&gt;  ...&lt;br /&gt;&lt;br /&gt;  throw aLocalObject &lt;br /&gt;}&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;and the call of foo is inside a try block:&lt;br /&gt;&lt;br /&gt;&lt;pre face="Andale Mono,Lucida Console,Monaco,fixed,monospace" size="12px" style="border: 1px dashed rgb(153, 153, 153); padding: 5px; overflow: auto; color: rgb(0, 0, 0); background-color: rgb(238, 238, 238); line-height: 14px; width: 100%;"&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;try {&lt;br /&gt;   foo();&lt;br /&gt;}&lt;br /&gt;catch( A anException) {&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;in this case, as the catch "signature" suggests, the anException is a copy of aLocalObject so&lt;br /&gt;no problem with it, but what if we catch by reference?&lt;br /&gt;&lt;br /&gt;&lt;pre face="Andale Mono,Lucida Console,Monaco,fixed,monospace" size="12px" style="border: 1px dashed rgb(153, 153, 153); padding: 5px; overflow: auto; color: rgb(0, 0, 0); background-color: rgb(238, 238, 238); line-height: 14px; width: 100%;"&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;catch( A &amp; anException ) {&lt;br /&gt;}&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;in this case we can think to have a reference to a destroyed object ( when an exception is thrown is not like a function call and all the local variable on that scope are destroyed ), well this is not the case indeed even if you catch an exception by reference the c++ runtime support will perform a copy of the object thrown, and this happens &lt;span style="font-weight: bold;"&gt;always&lt;/span&gt;, you can not avoid this copy even if the object will not be destroyed going out of scope ( a static variable for example ).&lt;br /&gt;&lt;br /&gt;So in case of:&lt;br /&gt;&lt;br /&gt;catch( A ) { }&lt;br /&gt;&lt;br /&gt;you have 2 copies  performed, in case of:&lt;br /&gt;&lt;br /&gt;catch ( A &amp;amp; )&lt;br /&gt;&lt;br /&gt;you have just one copy. For this very reason is not possible to modify the object thrown, because you have on the catch block a copy of it.&lt;br /&gt;&lt;br /&gt;Next week the second and last part about exceptions.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/3197434572108789628-1469592847868554546?l=cpp-today.blogspot.com' alt='' /&gt;&lt;/div&gt;
&lt;p&gt;&lt;a href="http://feedads.g.doubleclick.net/~a/KyBKgT1vidLwTwBKSXcXgGFg6Q0/0/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/KyBKgT1vidLwTwBKSXcXgGFg6Q0/0/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;br/&gt;
&lt;a href="http://feedads.g.doubleclick.net/~a/KyBKgT1vidLwTwBKSXcXgGFg6Q0/1/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/KyBKgT1vidLwTwBKSXcXgGFg6Q0/1/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;/p&gt;&lt;img src="http://feeds.feedburner.com/~r/blogspot/MJbOI/~4/H54tpY8v6F8" height="1" width="1"/&gt;</content><link rel="replies" type="application/atom+xml" href="http://cpp-today.blogspot.com/feeds/1469592847868554546/comments/default" title="Post Comments" /><link rel="replies" type="text/html" href="http://www.blogger.com/comment.g?blogID=3197434572108789628&amp;postID=1469592847868554546" title="0 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/3197434572108789628/posts/default/1469592847868554546?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/3197434572108789628/posts/default/1469592847868554546?v=2" /><link rel="alternate" type="text/html" href="http://feedproxy.google.com/~r/blogspot/MJbOI/~3/H54tpY8v6F8/exceptions-part-1.html" title="Exceptions (part 1)" /><author><name>Gaetano</name><uri>http://www.blogger.com/profile/03637292910769902384</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="16" height="16" src="http://img2.blogblog.com/img/b16-rounded.gif" /></author><thr:total>0</thr:total><feedburner:origLink>http://cpp-today.blogspot.com/2006/11/exceptions-part-1.html</feedburner:origLink></entry><entry gd:etag="W/&quot;DEQCQ3g4eyp7ImA9WxdRFEU.&quot;"><id>tag:blogger.com,1999:blog-3197434572108789628.post-2721932161108210793</id><published>2006-11-22T05:51:00.000-08:00</published><updated>2008-06-03T02:46:02.633-07:00</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2008-06-03T02:46:02.633-07:00</app:edited><category scheme="http://www.blogger.com/atom/ns#" term="c++" /><category scheme="http://www.blogger.com/atom/ns#" term="new operator" /><category scheme="http://www.blogger.com/atom/ns#" term="operator new" /><title>operator new  - new operator</title><content type="html">At first shot this two entities can appear to be the same; however they are not.&lt;br /&gt;Let see what they are, what you can change and the safe rules to handle them.&lt;br /&gt;&lt;br /&gt;First of all let see what happens when you write something like:  &lt;br /&gt;&lt;br /&gt;&lt;div style="text-align: center;"&gt;      C * pC = new C;&lt;br /&gt;&lt;/div&gt;&lt;ul&gt;&lt;li&gt;Enough memory is allocated to contain the object requested&lt;/li&gt;&lt;li&gt;The constructor of C is called to initialize the object the lays in the memory allocated&lt;br /&gt;&lt;/li&gt;&lt;/ul&gt;This described is the&lt;span style="font-weight: bold;"&gt; new&lt;/span&gt; &lt;span style="font-weight: bold;"&gt;operator &lt;/span&gt;behave and you can not change the way he acts.&lt;br /&gt;&lt;br /&gt;The first point in the sequence above is the only think you can change, the way the memory is allocated, the &lt;span style="font-weight: bold;"&gt;new operator &lt;/span&gt;uses for this task what is called: &lt;span style="font-weight: bold;"&gt;operator new&lt;/span&gt;.&lt;br /&gt;&lt;br /&gt;So the pseudo code for &lt;span style="font-style: italic;"&gt;C * pC = new C;&lt;/span&gt; could be:&lt;br /&gt;&lt;ol&gt;&lt;li&gt;Call &lt;span style="font-weight: bold;"&gt;operator new&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span style="font-weight: bold;"&gt;&lt;span style="font-weight: bold;"&gt;&lt;span style="font-weight: bold;"&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-weight: bold;"&gt;&lt;span style="font-weight: bold;"&gt;&lt;span style="font-weight: bold;"&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;Construct an object of the type request at the location returned from previous step&lt;/li&gt;&lt;/ol&gt;The operator new signature is something like this:&lt;br /&gt;&lt;br /&gt;   void * operator new(size_t);&lt;br /&gt;&lt;br /&gt;so if you want change the way the &lt;span style="font-weight: bold;"&gt;new operator&lt;/span&gt; allocates the memory for your type then you need to rewrite the &lt;span style="font-weight: bold;"&gt;operator new&lt;/span&gt;.&lt;br /&gt;As you already know when you specify a name in a scope ( for example a method name in a class ) this will hide the same name in the scopes that are containing your actual ( the base class scope for example ). So rewriting your &lt;span style="font-weight: bold;"&gt;operator new&lt;/span&gt; what you do is to hide the other forms of &lt;span style="font-weight: bold;"&gt;operator new. &lt;/span&gt;For instance these forms are:&lt;br /&gt;&lt;ol&gt;&lt;li&gt;void * operator new(std::size_t, std::nothrow_t) throw();&lt;/li&gt;&lt;li&gt;void * operator new(std::size_t, void *);&lt;/li&gt;&lt;/ol&gt;the former is the &lt;span style="font-style: italic;"&gt;nothrow new &lt;/span&gt;the latter is the &lt;span style="font-style: italic;"&gt;in place new. &lt;/span&gt;Actualy you can break more than this if you define your own operator new, something like:&lt;br /&gt;&lt;ul&gt;&lt;li&gt;void * operator new(std::size_t, T);&lt;/li&gt;&lt;/ul&gt;remember in this case that first argument of &lt;span style="font-weight: bold;"&gt;operator new &lt;/span&gt;shall be always std::size_t, in this case you will hide not only the "less common" &lt;span style="font-weight: bold;"&gt;operator new&lt;/span&gt; version but also the plain new one. I quoted &lt;span style="font-style: italic;"&gt;less common &lt;/span&gt;because in reality the STL does heavy usage of &lt;span style="font-style: italic;"&gt;in place new.&lt;/span&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/3197434572108789628-2721932161108210793?l=cpp-today.blogspot.com' alt='' /&gt;&lt;/div&gt;
&lt;p&gt;&lt;a href="http://feedads.g.doubleclick.net/~a/d_UCv1dFes1ghYECC0EDuiqbnQ8/0/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/d_UCv1dFes1ghYECC0EDuiqbnQ8/0/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;br/&gt;
&lt;a href="http://feedads.g.doubleclick.net/~a/d_UCv1dFes1ghYECC0EDuiqbnQ8/1/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/d_UCv1dFes1ghYECC0EDuiqbnQ8/1/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;/p&gt;&lt;img src="http://feeds.feedburner.com/~r/blogspot/MJbOI/~4/ubaT1AI_cN4" height="1" width="1"/&gt;</content><link rel="replies" type="application/atom+xml" href="http://cpp-today.blogspot.com/feeds/2721932161108210793/comments/default" title="Post Comments" /><link rel="replies" type="text/html" href="http://www.blogger.com/comment.g?blogID=3197434572108789628&amp;postID=2721932161108210793" title="0 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/3197434572108789628/posts/default/2721932161108210793?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/3197434572108789628/posts/default/2721932161108210793?v=2" /><link rel="alternate" type="text/html" href="http://feedproxy.google.com/~r/blogspot/MJbOI/~3/ubaT1AI_cN4/operator-new-new-operator.html" title="operator new  - new operator" /><author><name>Gaetano</name><uri>http://www.blogger.com/profile/03637292910769902384</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="16" height="16" src="http://img2.blogblog.com/img/b16-rounded.gif" /></author><thr:total>0</thr:total><feedburner:origLink>http://cpp-today.blogspot.com/2006/11/operator-new-new-operator.html</feedburner:origLink></entry></feed>

