<?xml version='1.0' encoding='UTF-8'?><?xml-stylesheet href="http://www.blogger.com/styles/atom.css" type="text/css"?><feed xmlns='http://www.w3.org/2005/Atom' xmlns:openSearch='http://a9.com/-/spec/opensearchrss/1.0/' xmlns:georss='http://www.georss.org/georss' xmlns:gd='http://schemas.google.com/g/2005' xmlns:thr='http://purl.org/syndication/thread/1.0'><id>tag:blogger.com,1999:blog-1456844729588229023</id><updated>2012-02-03T07:07:28.977+05:30</updated><category term='zaman'/><category term='this'/><category term='public'/><category term='multiple public sections'/><category term='C'/><category term='free'/><category term='zaman bakshi'/><category term='this pointer'/><category term='c portability'/><category term='database management system'/><category term='new'/><category term='vptr'/><category term='herb sutter'/><category term='Temporary'/><category term='C++'/><category term='Object Destruction Process'/><category term='object construction process'/><category term='delete'/><category term='query optimization'/><category term='undefined behavior'/><category term='function main()'/><category term='free store'/><category term='bjarne stroustrup'/><category term='plain old data'/><category term='object construction'/><category term='Object Destruction'/><category term='malloc()'/><category term='main()'/><category term='C++ standard library'/><category term='speed'/><category term='return type'/><category term='copy constructor'/><category term='heap'/><category term='mysql'/><category term='stanley lippman'/><category term='NULL'/><category term='main'/><category term='Destructor'/><category term='Bjarne'/><category term='vtbl'/><category term='videos'/><category term='initialization list'/><category term='member initialization list'/><category term='C++ standard'/><category term='default constructor'/><category term='undefined'/><category term='dbms'/><category term='constructor'/><category term='private'/><category term='databases'/><category term='i++ + i++'/><category term='protected'/><category term='constructor augmentation'/><category term='Destruction'/><category term='assignment operator'/><category term='Triviality'/><category term='Indeterminate Value'/><category term='Temporaries'/><category term='access specifier'/><category term='free-store'/><category term='bakshi'/><title type='text'>Get Your Computer Science Queries Answered</title><subtitle type='html'>This blog is dedicated to some of the most intriguing topics in computer programming.</subtitle><link rel='http://schemas.google.com/g/2005#feed' type='application/atom+xml' href='http://zamanbakshifirst.blogspot.com/feeds/posts/default'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/1456844729588229023/posts/default?max-results=100'/><link rel='alternate' type='text/html' href='http://zamanbakshifirst.blogspot.com/'/><link rel='hub' href='http://pubsubhubbub.appspot.com/'/><author><name>Zaman Bakshi</name><uri>http://www.blogger.com/profile/09870326614850200376</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>19</openSearch:totalResults><openSearch:startIndex>1</openSearch:startIndex><openSearch:itemsPerPage>100</openSearch:itemsPerPage><entry><id>tag:blogger.com,1999:blog-1456844729588229023.post-4378465694077779741</id><published>2008-07-04T05:52:00.002+05:30</published><updated>2008-07-04T05:59:12.919+05:30</updated><category scheme='http://www.blogger.com/atom/ns#' term='databases'/><category scheme='http://www.blogger.com/atom/ns#' term='query optimization'/><category scheme='http://www.blogger.com/atom/ns#' term='mysql'/><category scheme='http://www.blogger.com/atom/ns#' term='zaman'/><category scheme='http://www.blogger.com/atom/ns#' term='zaman bakshi'/><category scheme='http://www.blogger.com/atom/ns#' term='dbms'/><category scheme='http://www.blogger.com/atom/ns#' term='database management system'/><title type='text'>DBMS: An Overview of the MySQL Engine and its Latest Features</title><content type='html'>MySQL though a baby DBMS can be a source of knowledge as it's an open source DBMS. It doesn't have a query rewrite and other facilities that serious DBMSs like &lt;a href="http://www.oracle.com/database/index.html"&gt;Oracle&lt;/a&gt; and &lt;a href="http://www.microsoft.com/SQL/default.mspx"&gt;SQL Server &lt;/a&gt;have. Here is a lecture on how MySQL query optimizer works.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;object height="344" width="425"&gt;&lt;param name="movie" value="http://www.youtube.com/v/_nfUCpywMJc&amp;amp;hl=en&amp;amp;fs=1"&gt;&lt;param name="allowFullScreen" value="true"&gt;&lt;embed src="http://www.youtube.com/v/_nfUCpywMJc&amp;hl=en&amp;fs=1" type="application/x-shockwave-flash" allowfullscreen="true" width="425" height="344"&gt;&lt;/embed&gt;&lt;/object&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/1456844729588229023-4378465694077779741?l=zamanbakshifirst.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://zamanbakshifirst.blogspot.com/feeds/4378465694077779741/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=1456844729588229023&amp;postID=4378465694077779741' title='36 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/1456844729588229023/posts/default/4378465694077779741'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/1456844729588229023/posts/default/4378465694077779741'/><link rel='alternate' type='text/html' href='http://zamanbakshifirst.blogspot.com/2008/07/dbms-overview-of-mysql-engine-and-its.html' title='DBMS: An Overview of the MySQL Engine and its Latest Features'/><author><name>Zaman Bakshi</name><uri>http://www.blogger.com/profile/09870326614850200376</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>36</thr:total></entry><entry><id>tag:blogger.com,1999:blog-1456844729588229023.post-7372911073350797311</id><published>2008-05-11T04:02:00.011+05:30</published><updated>2008-06-03T17:56:43.818+05:30</updated><category scheme='http://www.blogger.com/atom/ns#' term='videos'/><category scheme='http://www.blogger.com/atom/ns#' term='zaman'/><category scheme='http://www.blogger.com/atom/ns#' term='zaman bakshi'/><category scheme='http://www.blogger.com/atom/ns#' term='bjarne stroustrup'/><title type='text'>C++: Bjarne Stroustrup Explaining C++0x Initialization Lists</title><content type='html'>&lt;strong&gt;C++0x Initialization Lists&lt;/strong&gt;&lt;br /&gt;&lt;object width="425" height="355"&gt;&lt;param name="movie" value="http://www.youtube.com/v/uEqknlxXJfg&amp;hl=en"&gt;&lt;/param&gt;&lt;param name="wmode" value="transparent"&gt;&lt;/param&gt;&lt;embed src="http://www.youtube.com/v/uEqknlxXJfg&amp;hl=en" type="application/x-shockwave-flash" wmode="transparent" width="425" height="355"&gt;&lt;/embed&gt;&lt;/object&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/1456844729588229023-7372911073350797311?l=zamanbakshifirst.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://zamanbakshifirst.blogspot.com/feeds/7372911073350797311/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=1456844729588229023&amp;postID=7372911073350797311' title='1 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/1456844729588229023/posts/default/7372911073350797311'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/1456844729588229023/posts/default/7372911073350797311'/><link rel='alternate' type='text/html' href='http://zamanbakshifirst.blogspot.com/2008/05/bjarne-videos.html' title='C++: Bjarne Stroustrup Explaining C++0x Initialization Lists'/><author><name>Zaman Bakshi</name><uri>http://www.blogger.com/profile/09870326614850200376</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></entry><entry><id>tag:blogger.com,1999:blog-1456844729588229023.post-7740252738496998486</id><published>2007-06-05T15:38:00.000+05:30</published><updated>2007-06-05T16:23:10.019+05:30</updated><category scheme='http://www.blogger.com/atom/ns#' term='C++ standard'/><category scheme='http://www.blogger.com/atom/ns#' term='plain old data'/><category scheme='http://www.blogger.com/atom/ns#' term='protected'/><category scheme='http://www.blogger.com/atom/ns#' term='zaman bakshi'/><category scheme='http://www.blogger.com/atom/ns#' term='multiple public sections'/><category scheme='http://www.blogger.com/atom/ns#' term='access specifier'/><category scheme='http://www.blogger.com/atom/ns#' term='Bjarne'/><category scheme='http://www.blogger.com/atom/ns#' term='undefined behavior'/><category scheme='http://www.blogger.com/atom/ns#' term='C++'/><category scheme='http://www.blogger.com/atom/ns#' term='public'/><category scheme='http://www.blogger.com/atom/ns#' term='private'/><category scheme='http://www.blogger.com/atom/ns#' term='bjarne stroustrup'/><title type='text'>C++ : Multiple Access Specifiers in a Class</title><content type='html'>On May 27th, &lt;a href="http://www.orkut.com/Profile.aspx?uid=7852952172124826284"&gt;Ramshankar&lt;/a&gt; in one of the &lt;a href="http://www.orkut.com/Community.aspx?cmm=38750"&gt;C++ communities&lt;/a&gt; at &lt;a href="http://www.orkut.com/"&gt;Orkut&lt;/a&gt;, asked what seemed like a pretty innocuous question:&lt;br /&gt;&lt;br /&gt;&lt;span style="font-weight: bold; font-style: italic; color: rgb(255, 0, 0);"&gt;   class&lt;/span&gt;&lt;span style="font-style: italic; color: rgb(255, 0, 0);"&gt; TUid&lt;/span&gt;&lt;br /&gt;&lt;span style="font-style: italic; color: rgb(255, 0, 0);"&gt; {&lt;/span&gt;&lt;br /&gt;&lt;span style="font-weight: bold; font-style: italic; color: rgb(255, 0, 0);"&gt;public&lt;/span&gt;&lt;span style="font-style: italic; color: rgb(255, 0, 0);"&gt;:&lt;/span&gt;&lt;br /&gt;&lt;span style="font-style: italic; color: rgb(255, 0, 0);"&gt; IMPORT_C TInt &lt;/span&gt;&lt;span style="font-weight: bold; font-style: italic; color: rgb(255, 0, 0);"&gt;operator&lt;/span&gt;&lt;span style="font-style: italic; color: rgb(255, 0, 0);"&gt;==(&lt;/span&gt;&lt;span style="font-weight: bold; font-style: italic; color: rgb(255, 0, 0);"&gt;const&lt;/span&gt;&lt;span style="font-style: italic; color: rgb(255, 0, 0);"&gt; TUid&amp; aUid) &lt;/span&gt;&lt;span style="font-weight: bold; font-style: italic; color: rgb(255, 0, 0);"&gt;const&lt;/span&gt;&lt;span style="font-style: italic; color: rgb(255, 0, 0);"&gt;;&lt;/span&gt;&lt;br /&gt;&lt;span style="font-weight: bold; font-style: italic; color: rgb(255, 0, 0);"&gt; static&lt;/span&gt;&lt;span style="font-style: italic; color: rgb(255, 0, 0);"&gt; &lt;/span&gt;&lt;span style="font-weight: bold; font-style: italic; color: rgb(255, 0, 0);"&gt;inline&lt;/span&gt;&lt;span style="font-style: italic; color: rgb(255, 0, 0);"&gt; TUid Null();&lt;/span&gt;&lt;br /&gt;&lt;span style="font-weight: bold; font-style: italic; color: rgb(255, 0, 0);"&gt;public&lt;/span&gt;&lt;span style="font-style: italic; color: rgb(255, 0, 0);"&gt;:&lt;/span&gt;&lt;br /&gt;&lt;span style="font-style: italic; color: rgb(255, 0, 0);"&gt; TInt32 iUid;&lt;/span&gt;&lt;br /&gt;&lt;span style="font-style: italic; color: rgb(255, 0, 0);"&gt; };&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="font-style: italic; color: rgb(255, 0, 0);"&gt;What is the purpose of defining "public" section again? Is it for &lt;/span&gt;&lt;span style="font-style: italic; color: rgb(255, 0, 0);"&gt;allowing:&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="font-style: italic; color: rgb(255, 0, 0);"&gt;TUid myType = { 0x01232423 };&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;Multiple public/protected/private sections are very much allowed in C++. In fact, they are seen in &lt;a href="http://en.wikipedia.org/wiki/Microsoft_Foundation_Class_Library"&gt;MFC&lt;/a&gt; wizard generated code. But, the real problem lay in not whether it was allowed, but why has this been allowed? As the &lt;a href="http://www.esnips.com/doc/b5bda452-32e1-4aa2-b1ae-83fb771bcc5d/C++-Standard-INCITS+ISO+IEC+14882-2003"&gt;C++ standard&lt;/a&gt; states that:&lt;br /&gt;&lt;br /&gt;&lt;i&gt;&lt;span style="color: rgb(255, 102, 0);"&gt;12. Nonstatic data members of a (non-union) class declared without an intervening access-specifier are allocated so that later members have higher addresses within a class object. The order of allocation of nonstatic data members separated by an access-specifier is unspecified (_class.access.spec_). &lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;/i&gt;So, to find accurate (reliable) answer I had to email &lt;a href="http://www.research.att.com/%7Ebs/"&gt;Bjarne&lt;/a&gt;, and this was his reply:&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 102, 0);"&gt;Consider&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 102, 0);"&gt;    &lt;/span&gt;&lt;span style="font-weight: bold; color: rgb(0, 153, 0);"&gt;struct&lt;/span&gt;&lt;span style="color: rgb(0, 153, 0);"&gt; S {&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 153, 0);"&gt;    &lt;/span&gt;&lt;span style="font-weight: bold; color: rgb(0, 153, 0);"&gt;public&lt;/span&gt;&lt;span style="color: rgb(0, 153, 0);"&gt;:&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 153, 0);"&gt;       &lt;/span&gt;&lt;span style="font-weight: bold; color: rgb(0, 153, 0);"&gt;int&lt;/span&gt;&lt;span style="color: rgb(0, 153, 0);"&gt; a;&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 153, 0);"&gt;    &lt;/span&gt;&lt;span style="font-weight: bold; color: rgb(0, 153, 0);"&gt;private&lt;/span&gt;&lt;span style="color: rgb(0, 153, 0);"&gt;:&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 153, 0);"&gt;       &lt;/span&gt;&lt;span style="font-weight: bold; color: rgb(0, 153, 0);"&gt;int&lt;/span&gt;&lt;span style="color: rgb(0, 153, 0);"&gt; b;&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 153, 0);"&gt;    &lt;/span&gt;&lt;span style="font-weight: bold; color: rgb(0, 153, 0);"&gt;public&lt;/span&gt;&lt;span style="color: rgb(0, 153, 0);"&gt;:&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 153, 0);"&gt;       &lt;/span&gt;&lt;span style="font-weight: bold; color: rgb(0, 153, 0);"&gt;int&lt;/span&gt;&lt;span style="color: rgb(0, 153, 0);"&gt; c;&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 153, 0);"&gt;    &lt;/span&gt;&lt;span style="font-weight: bold; color: rgb(0, 153, 0);"&gt;private&lt;/span&gt;&lt;span style="color: rgb(0, 153, 0);"&gt;:&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 153, 0);"&gt;         &lt;/span&gt;&lt;span style="font-weight: bold; color: rgb(0, 153, 0);"&gt;int &lt;/span&gt;&lt;span style="color: rgb(0, 153, 0);"&gt;d;&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 153, 0);"&gt;    };&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 102, 0);"&gt;Is the compiler allowed to allocate the private members next to each other? (the answer is yes). &lt;blockquote&gt;The reason for the rule was early ideas of separating private data from public data for some implementations to be able to alleviate code evolution problems when the data layout changed.&lt;/blockquote&gt;For example if you allocated public before private, then adding a private member could be done without affecting the public intercase (after creation). As far as I know, no compiler has ever done that.&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 102, 0);"&gt;However, some compilers do use rearrangement to create more compact layouts. For example:&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 102, 0);"&gt;    &lt;/span&gt;&lt;span style="font-weight: bold; color: rgb(0, 153, 0);"&gt;struct&lt;/span&gt;&lt;span style="color: rgb(0, 153, 0);"&gt; SS {&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 153, 0);"&gt;       &lt;/span&gt;&lt;span style="font-weight: bold; color: rgb(0, 153, 0);"&gt;char&lt;/span&gt;&lt;span style="color: rgb(0, 153, 0);"&gt; a;&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 153, 0);"&gt;    &lt;/span&gt;&lt;span style="font-weight: bold; color: rgb(0, 153, 0);"&gt;public&lt;/span&gt;&lt;span style="color: rgb(0, 153, 0);"&gt;:&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 153, 0);"&gt;       &lt;/span&gt;&lt;span style="font-weight: bold; color: rgb(0, 153, 0);"&gt;int&lt;/span&gt;&lt;span style="color: rgb(0, 153, 0);"&gt; b;&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 153, 0);"&gt;    &lt;/span&gt;&lt;span style="font-weight: bold; color: rgb(0, 153, 0);"&gt;public&lt;/span&gt;&lt;span style="color: rgb(0, 153, 0);"&gt;:&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 153, 0);"&gt;       &lt;/span&gt;&lt;span style="font-weight: bold; color: rgb(0, 153, 0);"&gt;char&lt;/span&gt;&lt;span style="color: rgb(0, 153, 0);"&gt; c;&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 153, 0);"&gt;    &lt;/span&gt;&lt;span style="font-weight: bold; color: rgb(0, 153, 0);"&gt;public&lt;/span&gt;&lt;span style="color: rgb(0, 153, 0);"&gt;:&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 153, 0);"&gt;       &lt;/span&gt;&lt;span style="font-weight: bold; color: rgb(0, 153, 0);"&gt;int&lt;/span&gt;&lt;span style="color: rgb(0, 153, 0);"&gt; d;&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 153, 0);"&gt;    &lt;/span&gt;&lt;span style="font-weight: bold; color: rgb(0, 153, 0);"&gt;public&lt;/span&gt;&lt;span style="color: rgb(0, 153, 0);"&gt;:&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 153, 0);"&gt;       &lt;/span&gt;&lt;span style="font-weight: bold; color: rgb(0, 153, 0);"&gt;char&lt;/span&gt;&lt;span style="color: rgb(0, 153, 0);"&gt; e;&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 153, 0);"&gt;    };&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 102, 0);"&gt;If members are allocated in declaration order, the size will be 5 words, but you can (legally) reorder to get 3 words (assuming a 4-byte word).&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 102, 0);"&gt;Personally, I have never found this useful.&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;I'll leave as an exercise how to reorder to get 3 &lt;a href="http://en.wikipedia.org/wiki/Word_%28computing%29"&gt;words&lt;/a&gt; . &lt;span style="color: rgb(255, 0, 0);"&gt;&lt;(^_^)&gt;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;But, a mystery still remains as &lt;a href="http://www.orkut.com/Profile.aspx?uid=15418884273347729852"&gt;André&lt;/a&gt; asked:&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(51, 153, 153);"&gt;Can you write any piece of strictly conforming code for which 9p12 (the above stated standard snippet) makes ANY difference for a non-POD &lt;span style="color: rgb(0, 0, 0);"&gt;(&lt;a href="http://en.wikipedia.org/wiki/Plain_Old_Data_Structures"&gt;Plain Old Data&lt;/a&gt;)&lt;/span&gt; type?&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(51, 153, 153);"&gt;Put another way (this is a different formulation of the same question):&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(51, 153, 153);"&gt;Can you write any piece of code that uses 9p12 (the fact that the order is specified) for a non-POD type, without invoking &lt;a href="http://zamanbakshifirst.blogspot.com/2006/11/c-whats-undefined-behavior-in-c.html"&gt;undefined behaviour&lt;/a&gt;?&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;Can you?!&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/1456844729588229023-7740252738496998486?l=zamanbakshifirst.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://zamanbakshifirst.blogspot.com/feeds/7740252738496998486/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=1456844729588229023&amp;postID=7740252738496998486' title='14 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/1456844729588229023/posts/default/7740252738496998486'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/1456844729588229023/posts/default/7740252738496998486'/><link rel='alternate' type='text/html' href='http://zamanbakshifirst.blogspot.com/2007/06/c-multiple-access-specifiers-in-class.html' title='C++ : Multiple Access Specifiers in a Class'/><author><name>Zaman Bakshi</name><uri>http://www.blogger.com/profile/09870326614850200376</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>14</thr:total></entry><entry><id>tag:blogger.com,1999:blog-1456844729588229023.post-6606571236363102172</id><published>2007-03-27T04:15:00.000+05:30</published><updated>2007-03-27T04:34:54.884+05:30</updated><category scheme='http://www.blogger.com/atom/ns#' term='zaman bakshi'/><category scheme='http://www.blogger.com/atom/ns#' term='stanley lippman'/><category scheme='http://www.blogger.com/atom/ns#' term='C++'/><category scheme='http://www.blogger.com/atom/ns#' term='this pointer'/><category scheme='http://www.blogger.com/atom/ns#' term='this'/><category scheme='http://www.blogger.com/atom/ns#' term='bjarne stroustrup'/><title type='text'>C++ : 'this' pointer representation</title><content type='html'>How is the '&lt;span style="font-style: italic; color: rgb(51, 204, 0);"&gt;this&lt;/span&gt;' pointer represented? &lt;a href="http://www.esnips.com/doc/b5bda452-32e1-4aa2-b1ae-83fb771bcc5d/C++-Standard-INCITS+ISO+IEC+14882-2003"&gt;The C++ standard&lt;/a&gt; states:&lt;br /&gt;&lt;br /&gt;&lt;span style="font-style: italic; color: rgb(255, 0, 0);"&gt;9.3.2/1&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="font-style: italic; color: rgb(255, 0, 0);"&gt;In the body of a nonstatic (9.3) member function, the keyword this is a non-lvalue expression whose value is the address of the object for which the function is called.&lt;/span&gt;&lt;b style="font-style: italic; color: rgb(255, 0, 0);"&gt; The type of this in a member function of a class X is X*. If the member function is declared const, the type of this is const X*&lt;/b&gt;&lt;span style="font-style: italic;"&gt;&lt;span style="color: rgb(255, 0, 0);"&gt;, if the member function is declared volatile, the type of this is volatile X*, and if the member function is declared const volatile, the type of this is const volatile X*.&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;/span&gt;But, in many texts I have read that the '&lt;span style="font-style: italic;"&gt;this&lt;/span&gt;' pointer is considered a constant. So, I did what I usually do when faced with extreme difficulty, shoot a mail to Mr &lt;a href="http://www.research.att.com/%7Ebs/homepage.html"&gt;Stroustrup&lt;/a&gt;. Here's a part of conversation that followed:&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 153, 0);"&gt;Dear Mr Stroustrup,&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 153, 0);"&gt;I am sorry if I am disturbing you.&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 153, 0);"&gt;In the C++ standard following is stated for the 'this' pointer.    &lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 153, 0);"&gt;-----------&lt;/span&gt;&lt;br /&gt;&lt;span style="font-style: italic; color: rgb(0, 153, 0);"&gt;9.3.2/1&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="font-style: italic; color: rgb(0, 153, 0);"&gt;In the body of a nonstatic (9.3) member function, the keyword this is a non-lvalue expression whose value is the address of the object for which the function is called.&lt;/span&gt;&lt;b style="font-style: italic; color: rgb(0, 153, 0);"&gt; The type of this in a member function of a class X is X*. If the member function is declared const, the type of this is const X*&lt;/b&gt;&lt;span style="font-style: italic; color: rgb(0, 153, 0);"&gt;, if the member function is declared volatile, the type of this is volatile X*, and if the member function is declared const volatile, the type of this is const volatile X*.&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 153, 0);"&gt;----------&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 153, 0);"&gt;While reading '&lt;a href="http://www.esnips.com/web/MyComputerBooks"&gt;Inside the C++ Object model&lt;/a&gt;', by Mr &lt;a href="http://staff.develop.com/slip/"&gt;Stanley Lippman&lt;/a&gt;, I came through 'this' being a constant pointer: myclass *const this, and const myclass *const this. And he writes that this technique was used in 'cfront'. &lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 153, 0);"&gt;My point is, I always considered 'this' to being a constant pointer so that we can validate it being a non-LValue as the standard requires it to be. But if we stick to the standard then what Mr Lippman writes turns out to be non-standard. What should I assume? In the C++ community at Orkut, I replied with the following &lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="font-style: italic; color: rgb(0, 153, 0);"&gt;   and it must be&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="font-style: italic; color: rgb(0, 153, 0);"&gt;&lt;span style="color: rgb(255, 0, 0);"&gt;void&lt;/span&gt; zaman::shout (&lt;span style="color: rgb(255, 0, 0);"&gt;const&lt;/span&gt; zaman *&lt;span style="color: rgb(255, 0, 0);"&gt;const&lt;/span&gt; &lt;span style="color: rgb(255, 0, 0);"&gt;this&lt;/span&gt;, .....) &lt;span style="color: rgb(255, 0, 0);"&gt;const&lt;/span&gt; {}&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="font-style: italic; color: rgb(0, 153, 0);"&gt;and &lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="font-style: italic; color: rgb(0, 153, 0);"&gt;&lt;span style="color: rgb(255, 0, 0);"&gt;void&lt;/span&gt; zaman::shout(zaman *&lt;span style="color: rgb(255, 0, 0);"&gt;const&lt;/span&gt; &lt;span style="color: rgb(255, 0, 0);"&gt;this&lt;/span&gt;, .....) {}&lt;br /&gt;&lt;span style="font-style: italic;"&gt;&lt;br /&gt;&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="color: rgb(0, 153, 0);"&gt;Waiting anxiously for reply&lt;br /&gt;&lt;br /&gt;----------------------------&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(102, 0, 0);"&gt;His reply:&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="color: rgb(102, 0, 0);"&gt;The committee decided (to simplify overloading rules, I think) to express the fact that you cannot change the value of "this" as it being a non-lvalue. I originally expressed its immutability by saying that it was a *const. Undoubtedly, you can find some example where that difference matter, but you'll have to look hard and the likelihood of such an example appearing in real come must be minimal.&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(102, 0, 0);"&gt;-------------------------------&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;So, it's not a constant pointer. -- Answer&lt;/span&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/1456844729588229023-6606571236363102172?l=zamanbakshifirst.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://zamanbakshifirst.blogspot.com/feeds/6606571236363102172/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=1456844729588229023&amp;postID=6606571236363102172' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/1456844729588229023/posts/default/6606571236363102172'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/1456844729588229023/posts/default/6606571236363102172'/><link rel='alternate' type='text/html' href='http://zamanbakshifirst.blogspot.com/2007/03/c-this-pointer-representation.html' title='C++ : &apos;this&apos; pointer representation'/><author><name>Zaman Bakshi</name><uri>http://www.blogger.com/profile/09870326614850200376</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></entry><entry><id>tag:blogger.com,1999:blog-1456844729588229023.post-6977889677393227410</id><published>2007-03-04T22:51:00.000+05:30</published><updated>2007-03-04T23:05:33.413+05:30</updated><category scheme='http://www.blogger.com/atom/ns#' term='C++ standard library'/><category scheme='http://www.blogger.com/atom/ns#' term='zaman'/><category scheme='http://www.blogger.com/atom/ns#' term='zaman bakshi'/><category scheme='http://www.blogger.com/atom/ns#' term='NULL'/><category scheme='http://www.blogger.com/atom/ns#' term='Bjarne'/><category scheme='http://www.blogger.com/atom/ns#' term='bakshi'/><category scheme='http://www.blogger.com/atom/ns#' term='C++'/><category scheme='http://www.blogger.com/atom/ns#' term='bjarne stroustrup'/><title type='text'>C++ : Are NULL and C++ standard library part of the C++ language?</title><content type='html'>NO, &lt;a href="http://www.esnips.com/doc/b5bda452-32e1-4aa2-b1ae-83fb771bcc5d/C++-Standard-INCITS+ISO+IEC+14882-2003"&gt;C++ standard&lt;/a&gt; library is not a part of the C++ language but provides support.&lt;br /&gt;&lt;br /&gt;The question was raised in an &lt;a href="http://www.orkut.com/"&gt;Orkut&lt;/a&gt; forum, and who better to ask than &lt;a href="http://www.research.att.com/%7Ebs/homepage.html"&gt;Bjarne&lt;/a&gt;. This is what his reply to my email was:&lt;br /&gt;&lt;br /&gt;&lt;div style="direction: ltr; color: rgb(255, 0, 0);"&gt;&lt;span class="q"&gt;Zaman Bakshi wrote:&lt;br /&gt;&gt; Dear Mr Stroustrup,&lt;br /&gt;&gt;&lt;br /&gt;&gt; I hope this email finds you in good health. Sir, can we say that the&lt;br /&gt;&gt; standard C and C++ libraries are a part of the C++ 'language'? Or&lt;br /&gt;&gt; should they be considered as a support for the language and not a part&lt;br /&gt;&gt; of it? This point was raised in the C/C++ programmers' community (that&lt;br /&gt;&gt; I am moderating) with reference to NULL. I cited your TC++PL and wrote&lt;br /&gt;&gt; const int NULL = 0; to be the correct implementation of NULL in C++,&lt;br /&gt;&gt; if it has to be defined. We know that 0 should be used instead of&lt;br /&gt;&gt; NULL, but what if NULL has to be defined. A null pointer is defined in&lt;br /&gt;&gt; the 'C++ standard' but NULL is used with reference to the C libraries.&lt;br /&gt;&gt; My answer was that NULL is not a part of the language but part of the&lt;br /&gt;&gt; C standard libraries referred in the C++ standard.&lt;br /&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;div style="direction: ltr; color: rgb(255, 0, 0);"&gt;&lt;span style="color: rgb(0, 153, 0);"&gt;I distinguish between the C++ language and the C++ standard library.&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 153, 0);"&gt;They are both part of the C++ standard, though, and shipped with every&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 153, 0);"&gt;implementation. Some people (slightly incorrect I think, but&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 153, 0);"&gt;understandably) refer to all that is in "The C++ language standard" as&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 153, 0);"&gt;"the C++ language".&lt;/span&gt;&lt;br /&gt;&lt;/div&gt;&lt;div style="direction: ltr; color: rgb(255, 0, 0);"&gt;&lt;span class="q"&gt;&lt;br /&gt;&gt;&lt;br /&gt;&gt; So, are NULL and the libraries part of the 'C++ language'?&lt;br /&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;span style="color: rgb(0, 153, 0);"&gt;I would say no. Even though you can use NULL after #including that&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 153, 0);"&gt;appropriate standard library header, you don't have to.&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 153, 0);"&gt;In C++0x, we'll get nullptr as a keyword indicating the null pointer.&lt;/span&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/1456844729588229023-6977889677393227410?l=zamanbakshifirst.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://zamanbakshifirst.blogspot.com/feeds/6977889677393227410/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=1456844729588229023&amp;postID=6977889677393227410' title='1 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/1456844729588229023/posts/default/6977889677393227410'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/1456844729588229023/posts/default/6977889677393227410'/><link rel='alternate' type='text/html' href='http://zamanbakshifirst.blogspot.com/2007/03/c-is-null-and-c-standard-library-parts.html' title='C++ : Are NULL and C++ standard library part of the C++ language?'/><author><name>Zaman Bakshi</name><uri>http://www.blogger.com/profile/09870326614850200376</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></entry><entry><id>tag:blogger.com,1999:blog-1456844729588229023.post-1830821752848526780</id><published>2007-02-23T02:54:00.000+05:30</published><updated>2007-02-23T03:25:22.336+05:30</updated><category scheme='http://www.blogger.com/atom/ns#' term='free'/><category scheme='http://www.blogger.com/atom/ns#' term='zaman bakshi'/><category scheme='http://www.blogger.com/atom/ns#' term='free store'/><category scheme='http://www.blogger.com/atom/ns#' term='malloc()'/><category scheme='http://www.blogger.com/atom/ns#' term='free-store'/><category scheme='http://www.blogger.com/atom/ns#' term='heap'/><category scheme='http://www.blogger.com/atom/ns#' term='delete'/><category scheme='http://www.blogger.com/atom/ns#' term='zaman'/><category scheme='http://www.blogger.com/atom/ns#' term='new'/><category scheme='http://www.blogger.com/atom/ns#' term='Bjarne'/><category scheme='http://www.blogger.com/atom/ns#' term='C++'/><category scheme='http://www.blogger.com/atom/ns#' term='herb sutter'/><category scheme='http://www.blogger.com/atom/ns#' term='bjarne stroustrup'/><title type='text'>C++ : Free-store versus Heap</title><content type='html'>What's the difference between the heap and the free-store? The C++ Programming Language keeps on referring them interchangeably. There was as huge cry over this issue in C/C++ programmer's community in Orkut. I had to shoot a mail to &lt;a href="http://www.research.att.com/%7Ebs/homepage.html"&gt;Dr. Bjarne Stroustrup&lt;/a&gt;. Here's our conversation:&lt;br /&gt;&lt;br /&gt;&lt;span style="font-weight: bold;"&gt;My Mail:&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="font-style: italic; color: rgb(255, 0, 0);"&gt;Dear Mr Stroustrup,&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="font-style: italic; color: rgb(255, 0, 0);"&gt;Sorry to disturb you again. You have mentioned several times in the TC++PL that 'new' allocates memory from the 'free store (or heap)'. There has been a huge cry on the C++ community at Orkut (that I am moderating) as to whether free-store is the same as heap. The argument given against is that &lt;a href="http://en.wikipedia.org/wiki/Herb_Sutter"&gt;Mr Herb Sutter&lt;/a&gt; has mentioned that the free-store is different from the heap: &lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;a style="font-style: italic; color: rgb(255, 0, 0);" href="http://www.gotw.ca/gotw/009.htm" target="_blank" onclick="return top.js.OpenExtLink(window,event,this)"&gt;http://www.gotw.ca/gotw/009.htm&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="font-style: italic; color: rgb(255, 0, 0);"&gt;and that global 'new' has nothing to do with the heap. &lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="font-style: italic; color: rgb(255, 0, 0);"&gt;So, if so, why has TC++PL used 'free store (or heap)' instead of mentioning the use of 'heap' separately.&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="font-style: italic; color: rgb(255, 0, 0);"&gt;Waiting anxiously for the response.&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="font-style: italic; color: rgb(255, 0, 0);"&gt;Regards,&lt;/span&gt;&lt;br /&gt;&lt;span class="sg"&gt;&lt;span style="font-style: italic; color: rgb(255, 0, 0);"&gt;Zaman Bakshi&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="font-weight: bold;"&gt;His Reply:&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;/span&gt;&lt;div style="direction: ltr; color: rgb(0, 153, 0); font-style: italic;"&gt;Note that Herb says: "Note about Heap vs. Free Store: We distinguish between "heap" and "free store" because the draft deliberately leaves unspecified the question of whether these two areas are related. For example, when memory is deallocated via operator delete, &lt;a onclick="return top.js.OpenExtLink(window,event,this)" href="http://18.4.1.1/" target="_blank"&gt;18.4.1.1&lt;/a&gt; states:"&lt;br /&gt;&lt;br /&gt;In other word, the "free store" vs "heap" distinction is Herb's attempt to distinguish malloc() allocation from new allocation.&lt;br /&gt;&lt;/div&gt;&lt;div style="direction: ltr; color: rgb(0, 153, 0); font-style: italic;"&gt;&lt;span class="q"&gt;&lt;br /&gt;&gt;&lt;br /&gt;&gt; So, if so, why has TC++PL used 'free store (or heap)' instead of&lt;br /&gt;&gt; mentioning the use of 'heap' separately.&lt;br /&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;!-- D(["mb","&lt;div&gt;Because even though it is undefined from where new and malloc() get&lt;br /&gt;their memory, they typically get them from exactly the same place. It is&lt;br /&gt;common for new and malloc() to allocate and free storage from the same&lt;br /&gt;part of the computer\'s memory. In that case, &amp;quot;free store&amp;quot; and &amp;quot;heap&amp;quot; are&lt;br /&gt;synonyms. I consistently use &amp;quot;free store&amp;quot; and &amp;quot;heap&amp;quot; is not a defined&lt;br /&gt;term in the C++ standard (outside the heap standard library algorithms,&lt;br /&gt;which are unrelated to new and malloc()). In relation to new, &amp;quot;heap&amp;quot; is&lt;br /&gt;simply a word someone uses (typically as a synonym to &amp;quot;free store&amp;quot;) -&lt;br /&gt;usually because they come from a different language background.&lt;br /&gt;&lt;/div&gt;",1] ); D(["mb","&lt;div&gt;&lt;div&gt;&lt;span&gt;- Show quoted text -&lt;/span&gt;&lt;/div&gt;&lt;span&gt;&lt;br /&gt;&gt;&lt;br /&gt;&gt; Waiting anxiously for the response.&lt;br /&gt;&gt;&lt;br /&gt;&gt; Regards,&lt;br /&gt;&gt; Zaman Bakshi&lt;br /&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;",0] );  //--&gt;&lt;span style="color: rgb(0, 153, 0); font-style: italic;"&gt;Because even though it is undefined from where new and malloc() get their memory, they typically get them from exactly the same place. It is common for new and malloc() to allocate and free storage from the same part of the computer's memory. In that case, "free store" and "heap" are synonyms. I consistently use "free store" and "heap" is not a defined term in the C++ standard (outside the heap standard library algorithms, which are unrelated to new and malloc()). In relation to new, "heap" is simply a word someone uses (typically as a synonym to "free store") - usually because they come from a different language background.&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="font-weight: bold;"&gt;My Reply:&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="font-style: italic; color: rgb(255, 0, 0);"&gt;Thank you Mr. Stroustrup, I had inferred the same thing (about using free store as general -- or better, synonym -- term) and had explained the community. But, I had been requested to reconfirm.&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="font-style: italic; color: rgb(255, 0, 0);"&gt;With warm regards,&lt;/span&gt;&lt;br /&gt;&lt;span style="font-style: italic; color: rgb(255, 0, 0);" class="sg"&gt; Zaman Bakshi&lt;/span&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/1456844729588229023-1830821752848526780?l=zamanbakshifirst.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://zamanbakshifirst.blogspot.com/feeds/1830821752848526780/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=1456844729588229023&amp;postID=1830821752848526780' title='3 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/1456844729588229023/posts/default/1830821752848526780'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/1456844729588229023/posts/default/1830821752848526780'/><link rel='alternate' type='text/html' href='http://zamanbakshifirst.blogspot.com/2007/02/c-free-store-versus-heap.html' title='C++ : Free-store versus Heap'/><author><name>Zaman Bakshi</name><uri>http://www.blogger.com/profile/09870326614850200376</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>3</thr:total></entry><entry><id>tag:blogger.com,1999:blog-1456844729588229023.post-8032089034739819129</id><published>2007-02-18T04:49:00.000+05:30</published><updated>2007-02-18T05:01:42.628+05:30</updated><category scheme='http://www.blogger.com/atom/ns#' term='Indeterminate Value'/><category scheme='http://www.blogger.com/atom/ns#' term='C'/><category scheme='http://www.blogger.com/atom/ns#' term='zaman'/><category scheme='http://www.blogger.com/atom/ns#' term='zaman bakshi'/><category scheme='http://www.blogger.com/atom/ns#' term='Bjarne'/><category scheme='http://www.blogger.com/atom/ns#' term='C++'/><category scheme='http://www.blogger.com/atom/ns#' term='bjarne stroustrup'/><title type='text'>C++ : Indeterminate Value</title><content type='html'>&lt;a href="http://www.esnips.com/doc/f43f92e5-5057-4045-b0ca-62650019c0bd/ISO-Standard-for-the-C-Language"&gt;The C language standard&lt;/a&gt; clearly 'defines' what an indeterminate value is in C. But &lt;a href="http://www.esnips.com/doc/b5bda452-32e1-4aa2-b1ae-83fb771bcc5d/C++-Standard-INCITS+ISO+IEC+14882-2003"&gt;the C++ standard&lt;/a&gt; is missing this definition. Naturally, we can't adopt the definition in C standard to C++. I wanted to know, and who could have been more reliable than &lt;a href="http://www.research.att.com/%7Ebs/"&gt;Mr. Bjarne Stroustrup&lt;/a&gt; to clear this cloud of uncertainty. So, I dashed an email to him. Here's the conversation that followed:&lt;br /&gt;&lt;br /&gt;&lt;span class="sg"&gt;----------------------------------&lt;br /&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="font-style: italic; color: rgb(153, 0, 0);"&gt;Dear Mr Stroustrup,&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="font-style: italic; color: rgb(153, 0, 0);"&gt;I am reading D&amp;E, and let me congratulate you for writing such a great book. It has been of a lot of help. Mr. Stroustrup, I am moderating a C++ community on Orkut and there has been a very big issue over what 'indeterminate value' means for the C++ standard. The C standard clearly states what 'indeterminate value' means, but the C++ standard though using (indeterminate value) many times doesn't specify its definition. Should we regard 'indeterminate value' in C++ as being undefined, or should we stick to the C standard's definition (for 'indeterminate value')? &lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="font-style: italic; color: rgb(153, 0, 0);"&gt;Anxiously waiting for the response.&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="font-style: italic; color: rgb(153, 0, 0);"&gt;Regards,&lt;/span&gt;&lt;br /&gt;&lt;span class="sg"&gt;&lt;span style="font-style: italic; color: rgb(153, 0, 0);"&gt;Zaman Bakshi.&lt;br /&gt;&lt;br /&gt;&lt;/span&gt;----------------------------------&lt;br /&gt;&lt;br /&gt;&lt;/span&gt;&lt;div style="direction: ltr;"&gt;&lt;span style="font-style: italic; color: rgb(0, 153, 0);"&gt;Thanks.&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="font-style: italic; color: rgb(0, 153, 0);"&gt;I never used "indeterminate value" and hadn't noticed that it had "snug&lt;/span&gt;&lt;br /&gt;&lt;span style="font-style: italic; color: rgb(0, 153, 0);"&gt;into" the C++ standard. I have raised an issue and "indeterminate value"&lt;/span&gt;&lt;br /&gt;&lt;span style="font-style: italic; color: rgb(0, 153, 0);"&gt;will be defined in C++0x. You can't "stick to C's definition" because&lt;/span&gt;&lt;br /&gt;&lt;span style="font-style: italic; color: rgb(0, 153, 0);"&gt;that definition has never been approved for C++ (was introduced into C&lt;/span&gt;&lt;br /&gt;&lt;span style="font-style: italic; color: rgb(0, 153, 0);"&gt;relatively lately, I believe). "indeterminate" simple means that you&lt;/span&gt;&lt;br /&gt;&lt;span style="font-style: italic; color: rgb(0, 153, 0);"&gt;don't know what that value is (if could be absolutely any bit pattern&lt;/span&gt;&lt;br /&gt;&lt;span style="font-style: italic; color: rgb(0, 153, 0);"&gt;that fits in the object). I believe the C++ standard is specific about&lt;/span&gt;&lt;br /&gt;&lt;span style="font-style: italic; color: rgb(0, 153, 0);"&gt;which operations requires a properly initialized object.&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="font-style: italic; color: rgb(0, 153, 0);"&gt;Does this address the issues raised in your discussion? If not, please&lt;/span&gt;&lt;br /&gt;&lt;span style="font-style: italic; color: rgb(0, 153, 0);"&gt;ask again.&lt;br /&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;span class="sg"&gt;----------------------------------&lt;br /&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="color: rgb(153, 0, 0); font-style: italic;"&gt;Dear Mr Stroustrup,&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(153, 0, 0); font-style: italic;"&gt;Thank you for replying to my e-mail. Yes, it does answer my issue. This is exactly what I had inferred, but as you know, developers like me can't argue over the standard, so had to clear the doubt. I, like others, are anxiously waiting for C++0x to be out with the standard. Good luck with it. And I thank you again for your response. &lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(153, 0, 0); font-style: italic;"&gt;Regards,&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(153, 0, 0); font-style: italic;" class="sg"&gt;Zaman Bakshi&lt;br /&gt;&lt;br /&gt;&lt;/span&gt;&lt;span class="sg"&gt;----------------------------------&lt;br /&gt;&lt;br /&gt;&lt;/span&gt;&lt;div style="direction: ltr;"&gt;&lt;span style="color: rgb(0, 153, 0);"&gt;Thanks. I'm working hard for C++0x to become C++09. Doing that requires&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 153, 0);"&gt;a complete feature freeze and complete WP by the end of 2007.&lt;/span&gt;&lt;br /&gt;&lt;/div&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/1456844729588229023-8032089034739819129?l=zamanbakshifirst.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://zamanbakshifirst.blogspot.com/feeds/8032089034739819129/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=1456844729588229023&amp;postID=8032089034739819129' title='1 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/1456844729588229023/posts/default/8032089034739819129'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/1456844729588229023/posts/default/8032089034739819129'/><link rel='alternate' type='text/html' href='http://zamanbakshifirst.blogspot.com/2007/02/c-indeterminate-value.html' title='C++ : Indeterminate Value'/><author><name>Zaman Bakshi</name><uri>http://www.blogger.com/profile/09870326614850200376</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></entry><entry><id>tag:blogger.com,1999:blog-1456844729588229023.post-4537477579484341934</id><published>2006-12-03T02:07:00.000+05:30</published><updated>2006-12-03T02:33:09.908+05:30</updated><category scheme='http://www.blogger.com/atom/ns#' term='C'/><category scheme='http://www.blogger.com/atom/ns#' term='speed'/><category scheme='http://www.blogger.com/atom/ns#' term='zaman bakshi'/><category scheme='http://www.blogger.com/atom/ns#' term='Bjarne'/><category scheme='http://www.blogger.com/atom/ns#' term='C++'/><category scheme='http://www.blogger.com/atom/ns#' term='bjarne stroustrup'/><title type='text'>C/C++ : Speed Variation</title><content type='html'>People believe that C code has faster execution speed than of C++. I argue otherwise. Many C++ gurus have spent their valuable time explaining that there's no difference. Now, I may lack credibility but the writer of C++, &lt;a href="http://www.research.att.com/%7Ebs/"&gt;Bjarne Stroustrup&lt;/a&gt;, certainly doesn't. So, would you believe his words? While browsing the Internet, I found myself laying hands on one of the emails sent to Bajrne to explain his views on this contentious topic. Here's the email (in full):&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="font-style: italic;"&gt;TITLE: Speed and size of C versus C++&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="font-style: italic;"&gt;PROBLEM: ???&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="font-style: italic;"&gt;I also heard the size of C++ program is generally bigger than C. I am a C programmer trying to learn C++.  So, don't blame me if I have created any misconception about C++.&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="font-style: italic;"&gt;RESPONSE: ???&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="font-style: italic;"&gt;For one minor detail: including printf/scanf can include more code than is actually used. An intelligent C++ linker will only get that parts of the stream library REALLY needed.&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="font-style: italic;"&gt;RESPONSE: cshaver@informix.com (Craig Shaver @ Informix Software, Inc.)&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="font-style: italic;"&gt;Who implements an intelligent C++ linker???  I was under the impression that you get all the functionality in a class when you link, whether you use it or not.  &lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="font-style: italic;"&gt;RESPONSE: bs@alice.att.com (Bjarne Stroustrup), 12 Jan 93&lt;/span&gt;&lt;br /&gt;&lt;span style="font-style: italic;"&gt;      AT&amp;T Bell Laboratories, Murray Hill NJ&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="font-style: italic;"&gt;Let me try to clear up one or two points. Consider first a somewhat minimal C and C++ program x1.c:&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="font-style: italic;"&gt;    main()&lt;/span&gt;&lt;br /&gt;&lt;span style="font-style: italic;"&gt;    {&lt;/span&gt;&lt;br /&gt;&lt;span style="font-style: italic;"&gt;        &lt;span style="color: rgb(255, 0, 0);"&gt;int&lt;/span&gt; i;&lt;/span&gt;&lt;br /&gt;&lt;span style="font-style: italic;"&gt;        &lt;span style="color: rgb(255, 0, 0);"&gt;for&lt;/span&gt; (i = 0; i &lt; 1000000; i++ ) printf("Hi,mom!\n") ;&lt;br /&gt;&lt;span style="font-style: italic;"&gt;    }&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="font-style: italic;"&gt;and its more C++ looking cousin x2.c:&lt;/span&gt;&lt;span style="font-style: italic;"&gt;&lt;iostream.h&gt;&lt;/iostream.h&gt;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="font-style: italic;"&gt;    main()&lt;/span&gt;&lt;br /&gt;&lt;span style="font-style: italic;"&gt;    {&lt;/span&gt;&lt;br /&gt;&lt;span style="font-style: italic;"&gt;        &lt;span style="color: rgb(255, 0, 0);"&gt;int&lt;/span&gt; i;&lt;/span&gt;&lt;br /&gt;&lt;span style="font-style: italic;"&gt;       &lt;span style="color: rgb(255, 0, 0);"&gt; for &lt;/span&gt;(i = 0; i&lt;1000000; i++ ) cout &lt;&lt; "Hi,mon!\n" ;&lt;br /&gt;&lt;span style="font-style: italic;"&gt;    }&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="font-style: italic;"&gt;I compiled and ran x1.c and x2.c:&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="font-style: italic;"&gt;c: cc x1.c&lt;/span&gt;&lt;br /&gt;&lt;span style="font-style: italic;"&gt;c: size a.out&lt;/span&gt;&lt;br /&gt;&lt;span style="font-style: italic;"&gt;text    | data  |   bss   | dec      |  hex&lt;/span&gt;&lt;br /&gt;&lt;span style="font-style: italic;"&gt;12288|  6144|   7608| 26040|  65b8&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="font-style: italic;"&gt;c: time a.out &gt; /dev/null&lt;/span&gt;&lt;br /&gt;&lt;span style="font-style: italic;"&gt;25.5u 0.5s 37r      a.out&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="font-style: italic;"&gt;c: PTCC x1.c&lt;/span&gt;&lt;br /&gt;&lt;span style="font-style: italic;"&gt;c: size a.out&lt;/span&gt;&lt;br /&gt;&lt;span style="font-style: italic;"&gt;text     |data   | bss     | dec       | hex&lt;/span&gt;&lt;br /&gt;&lt;span style="font-style: italic;"&gt;12288 | 6144 | 7620  | 26052 |   65c4&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="font-style: italic;"&gt;c:  time a.out &gt; /dev/null&lt;/span&gt;&lt;br /&gt;&lt;span style="font-style: italic;"&gt;25.7u 0.4s 33r      a.out&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="font-style: italic;"&gt;PTCC is the driver for my standard off the-shelf Cfront 3.0 (i.e. I'm not using any technology you couldn't buy half a year ago). Note that the size of the generated code is essentially the same. So is the speed. Running these examples a few times to eliminate random error in the timing mechanism shows that the run-time isn't biased one way or the other.&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="font-style: italic;"&gt;This is what you should expect for a program in the common sub-set of C and C++. There is fundamental reasons for that. You should expect identical code from two C and C++ compilers using the same technology. The only possibly SYSTEMATIC difference I can think of is that a C++ compiler can use better function call sequences than a C compiler that doesn't apply global optimization because in many cases a C compiler must guard against possible calls with differing numbers of arguments where a C++ compiler doesn't need to because of C++'s stronger type checking. In most C and C++ compilers, this difference is theoretical, but I'm told that in Zortech C++ it is real (i.e. C++ programs are ever so slightly faster than their C equivalents). However, this is all noise, I doubt the difference between C and C++ in this kind of comparison matters to any real programmers. The difference is far smaller than differences between different C compilers - but surprisingly, it is in C++'s favor.&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="font-style: italic;"&gt;    Programs in the common subset of C and C++ results in&lt;/span&gt;&lt;br /&gt;&lt;span style="font-style: italic;"&gt;    equal sized code that execute at equal speed.&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="font-style: italic;"&gt;If that conclusion doesn't appear to hold, check if your C and C++ compilers are of similar quality. If your C++ compiler appears to loose badly you have the option of using a Cfront variant to get the benefits of your C compiler's code generation facilities. If your C compiler loose badly, switch to C++ even if you aren't ready to use the ``++ features.''&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="font-style: italic;"&gt;Now, a common argument is ``OK, so C++ can match C for a C programs but as soon as you use the REAL C++ features your programs get bigger and slower.'' Clearly you can write big and slow programs in any language (even C), but you don't necessarily take a performance hit when you start using C++. Consider x2.c. It uses the C++ stream I/O library that is certainly bigger than C's stdio and is unlikely to be tuned to the same degree as stdio. It is also a library that&lt;/span&gt;&lt;br /&gt;&lt;span style="font-style: italic;"&gt;uses a very large sub-set of C++'s features in its interface and implementation (operator overloading, multiple inheritance, virtual functions, etc.):&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="font-style: italic;"&gt;c: PTCC x2.c&lt;/span&gt;&lt;br /&gt;&lt;span style="font-style: italic;"&gt;c: size a.out&lt;/span&gt;&lt;br /&gt;&lt;span style="font-style: italic;"&gt;c: text  |  data  |  bss  |  dec   |  hex&lt;/span&gt;&lt;br /&gt;&lt;span style="font-style: italic;"&gt;17408  | 2048  |  0     |19456|  4c00&lt;/span&gt;&lt;br /&gt;&lt;span style="font-style: italic;"&gt;c: time a.out &gt; /dev/null&lt;/span&gt;&lt;br /&gt;&lt;span style="font-style: italic;"&gt;32.8u 1.0s 43r      a.out&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="font-style: italic;"&gt;Surprisingly enough, the code generated for x2.c is noticeably smaller than the code generated for x1.c (75% of x1.o) though - as expected it runs a bit slower (29% user cpu time, 16% better elapse time).&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="font-style: italic;"&gt;I claim, but cannot prove, that the run-time overhead is primarily a difference in tuning. Other programs that rely heavily on C++ features show improvements over their C counterparts - and others again show overhead. The differences does not appear systematic to me; that is, they are differences in design and effort, rather than inherent overhead in C or C++.&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="font-style: italic;"&gt;The space advantage of the C++ program is an advantage of the same kind; that is, it is there because a little extra care and thought was spent. Other implementations of stream I/O will show different space and time usage, as will different implementations of stdio. To do simple things only the essential parts of the stream I/O library is brought in. You don't actually need a very ``intelligent'' linker, the dumb old Unix ld will do: Just manually split your implementation into several .c files. A simple example:&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="font-style: italic;"&gt;X.h:&lt;/span&gt;&lt;br /&gt;&lt;span style="font-style: italic;"&gt;    &lt;span style="color: rgb(255, 0, 0);"&gt;class&lt;/span&gt; X {&lt;/span&gt;&lt;br /&gt;&lt;span style="font-style: italic;"&gt;        // details&lt;/span&gt;&lt;br /&gt;&lt;span style="font-style: italic;"&gt;    &lt;span style="color: rgb(255, 0, 0);"&gt;public&lt;/span&gt;:&lt;/span&gt;&lt;br /&gt;&lt;span style="font-style: italic;"&gt;        &lt;span style="color: rgb(255, 0, 0);"&gt;void &lt;/span&gt;f();    // common function&lt;/span&gt;&lt;br /&gt;&lt;span style="font-style: italic;"&gt;        &lt;span style="color: rgb(255, 0, 0);"&gt;void &lt;/span&gt;g();    // uncommon function&lt;/span&gt;&lt;br /&gt;&lt;span style="font-style: italic;"&gt;        // more functions&lt;/span&gt;&lt;br /&gt;&lt;span style="font-style: italic;"&gt;    };&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="font-style: italic;"&gt;    &lt;/span&gt;&lt;br /&gt;&lt;span style="font-style: italic;"&gt;X1.c:&lt;/span&gt;&lt;br /&gt;&lt;span style="font-style: italic;"&gt;    // common functions:&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="font-style: italic;"&gt;    &lt;span style="color: rgb(255, 0, 0);"&gt;void &lt;/span&gt;X::f() { ... }&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="font-style: italic;"&gt;X2.c:&lt;/span&gt;&lt;br /&gt;&lt;span style="font-style: italic;"&gt;    // uncommon functions:&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="font-style: italic;"&gt;    &lt;span style="color: rgb(255, 0, 0);"&gt;void&lt;/span&gt; X::g() { ... }&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="font-style: italic;"&gt;Now, any half-way decent archive program can bring in the object code for X1.c (only) for programs that use the common functions (only) and leave the expense of bringing in the object code for X2.c for the programs that actually use functions defined in X2.c.&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="font-style: italic;"&gt;There exist linkers that can do that without human help (mostly in the PC world), I just happen not to have one. I think it is important to note that this technique and the tools that supports it carried over from C to C++. We wasn't at the mercy of some ``smart'' and possibly espensive or unavailable technology. We don't have to forget or loose all of our effective techniques in moving from C to C++. We should - as ever - use them with a suitable amount of judgement.&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="font-style: italic;"&gt;C++ was designed not to leave room "below'' for a lower level language, except assembler for machine specific operations.&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;I found this email on this following link:&lt;br /&gt;&lt;a href="http://nkari.uw.hu/Tutorials/CPPTips/split_impl"&gt;http://nkari.uw.hu/Tutorials/CPPTips/split_impl&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;You can click on it to check for any inconsistencies. So, I take his words on this issue, do you?&lt;/span&gt;&lt;/span&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/1456844729588229023-4537477579484341934?l=zamanbakshifirst.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://zamanbakshifirst.blogspot.com/feeds/4537477579484341934/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=1456844729588229023&amp;postID=4537477579484341934' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/1456844729588229023/posts/default/4537477579484341934'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/1456844729588229023/posts/default/4537477579484341934'/><link rel='alternate' type='text/html' href='http://zamanbakshifirst.blogspot.com/2006/12/cc-speed-variation.html' title='C/C++ : Speed Variation'/><author><name>Zaman Bakshi</name><uri>http://www.blogger.com/profile/09870326614850200376</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></entry><entry><id>tag:blogger.com,1999:blog-1456844729588229023.post-3167535062240867744</id><published>2006-11-18T09:24:00.000+05:30</published><updated>2006-11-18T10:16:46.147+05:30</updated><category scheme='http://www.blogger.com/atom/ns#' term='zaman bakshi'/><category scheme='http://www.blogger.com/atom/ns#' term='Temporary'/><category scheme='http://www.blogger.com/atom/ns#' term='Temporaries'/><category scheme='http://www.blogger.com/atom/ns#' term='C++'/><title type='text'>C++ : All About Temporaries</title><content type='html'>&lt;span style="font-style: italic;"&gt;Even the most trivial statements, like A = B, in a computer language may produce temporaries. Moreover, the generation of these temporaries has to be standardized to maintain a language's efficacy. The C++ language is no exception to that rule.&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="font-style: italic;"&gt;Following is an embellished version of &lt;/span&gt;&lt;a style="font-style: italic;" href="http://www.esnips.com/web/MyComputerPapers"&gt;'The C++ Standard'&lt;/a&gt;&lt;span style="font-style: italic;"&gt;.&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="font-weight: bold;"&gt;Temporary Objects&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="font-weight: bold;"&gt;1&lt;/span&gt; Temporaries of class type are created in various contexts: binding an rvalue to a reference, returning an rvalue, a conversion that creates an rvalue, throwing an exception, entering a handler, and in some initializations. Even when the creation of the temporary object is avoided, all the semantic restrictions must be respected as if the temporary object was created. [Example: even if the copy constructor is not called, all the semantic restrictions, such as accessibility, shall be satisfied. ]&lt;br /&gt;&lt;br /&gt;&lt;span style="font-weight: bold;"&gt;2&lt;/span&gt; [Example:&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 0, 0);font-family:courier new;" &gt;class&lt;/span&gt;&lt;span style="font-family:courier new;"&gt; X {&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:courier new;"&gt;// ...&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 0, 0);font-family:courier new;" &gt;public&lt;/span&gt;&lt;span style="font-family:courier new;"&gt;:&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:courier new;"&gt;// ...&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:courier new;"&gt;X(&lt;/span&gt;&lt;span style="color: rgb(255, 0, 0);font-family:courier new;" &gt;int&lt;/span&gt;&lt;span style="font-family:courier new;"&gt;);&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:courier new;"&gt;X(&lt;/span&gt;&lt;span style="color: rgb(255, 0, 0);font-family:courier new;" &gt;const&lt;/span&gt;&lt;span style="font-family:courier new;"&gt; X&amp;);&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:courier new;"&gt;˜X();&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:courier new;"&gt;};&lt;br /&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="font-family:courier new;"&gt;X f(X);&lt;br /&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="color: rgb(255, 0, 0);font-family:courier new;" &gt;void&lt;/span&gt;&lt;span style="font-family:courier new;"&gt; g()&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:courier new;"&gt;{&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:courier new;"&gt;X a(1);&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:courier new;"&gt;X b = f(X(2));&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:courier new;"&gt;a = f(a);&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:courier new;"&gt;}&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;Here, an implementation might use a temporary in which to construct &lt;span style="font-style: italic;"&gt;X(2)&lt;/span&gt; before passing it to &lt;span style="font-style: italic;"&gt;f() &lt;/span&gt;using &lt;span style="font-style: italic;"&gt;X&lt;/span&gt;’s copy-constructor; alternatively, &lt;span style="font-style: italic;"&gt;X(2)&lt;/span&gt; might be constructed in the space used to hold the argument. Also, a temporary might be used to hold the result of &lt;span style="font-style: italic;"&gt;f(X(2))&lt;/span&gt; before copying it to &lt;span style="font-style: italic;"&gt;b&lt;/span&gt; using &lt;span style="font-style: italic;"&gt;X&lt;/span&gt;’s copy-constructor; alternatively, &lt;span style="font-style: italic;"&gt;f()&lt;/span&gt;’s result might be constructed (directly) in &lt;span style="font-style: italic;"&gt;b&lt;/span&gt;. On the other hand, the expression &lt;span style="font-style: italic;"&gt;a=f(a)&lt;/span&gt; requires a temporary for either the argument &lt;span style="font-style: italic;"&gt;a &lt;/span&gt;or the result of &lt;span style="font-style: italic;"&gt;f(a)&lt;/span&gt; to avoid undesired aliasing of &lt;span style="font-style: italic;"&gt;a&lt;/span&gt;. ]&lt;br /&gt;&lt;br /&gt;&lt;span style="font-weight: bold;"&gt;3&lt;/span&gt; When an implementation introduces a temporary object of a class that has a &lt;a href="http://zamanbakshifirst.blogspot.com/2006/11/c-i-have-heard-that-if-i-dont-write-one.html"&gt;non-trivial constructor&lt;/a&gt;, it shall ensure that a constructor is called for the temporary object. Similarly, the destructor shall be called for a temporary with a non-trivial destructor. Temporary objects are destroyed as the last step in evaluating the full-expression &lt;span style="color: rgb(204, 51, 204);"&gt;(a full-expression is an expression that is not a subexpression of another expression)&lt;/span&gt;  that (lexically) contains the point where they were created. &lt;span style="color: rgb(204, 51, 204);"&gt;This is true even if that evaluation ends in throwing an exception&lt;/span&gt;.&lt;br /&gt;&lt;br /&gt;&lt;span style="font-weight: bold;"&gt;4&lt;/span&gt; There are two contexts in which temporaries are destroyed at a different point than the end of the full-expression . The first context is when an expression appears as an initializer for a declarator defining an object. In that context, the temporary that holds the result of the expression shall persist until the object’s initialization is complete. The object is initialized from a copy of the temporary; during this copying, an implementation can call the copy constructor many times; the temporary is destroyed after it has been copied, before or when the initialization completes. If many temporaries are created by the evaluation of the initializer, the temporaries are destroyed in reverse order of the completion of their construction.&lt;br /&gt;&lt;br /&gt;&lt;span style="font-weight: bold;"&gt;5&lt;/span&gt; The second context is when a reference is bound to a temporary. The temporary to which the reference is bound or the temporary that is the complete object to a subobject of which the temporary is bound persists for the lifetime of the reference except as specified below. A temporary bound to a reference member in a constructor’s ctor-initializer persists until the constructor exits. A temporary bound to a reference parameter in a function call persists until the completion of the full expression containing the call. A temporary bound to the returned value in a function return statement persists until the function exits. In all these cases, the temporaries created during the evaluation of the expression initializing the reference, except the temporary to which the reference is bound, are destroyed at the end of the full-expression in which they are created and in the reverse order of the completion of their construction. If the lifetime of two or more temporaries to which references are bound ends at the same point, these temporaries are destroyed at that point in the reverse order of the completion of their construction. In addition, the destruction of temporaries bound to references shall take into account the ordering of destruction of objects with static or automatic storage duration; that is, if &lt;span style="font-style: italic;"&gt;obj1&lt;/span&gt; is an object with static or automatic storage duration created before the temporary is created, the temporary shall be destroyed before obj1 is destroyed; if &lt;span style="font-style: italic;"&gt;obj2&lt;/span&gt; is an object with static or automatic storage duration created after the temporary is created, the temporary shall be destroyed after obj2 is destroyed. [Example:&lt;br /&gt;&lt;br /&gt;&lt;span style="font-family:courier new;"&gt;&lt;span style="color: rgb(255, 0, 0);"&gt;class&lt;/span&gt; C {&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:courier new;"&gt;// ...&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:courier new;"&gt;&lt;span style="color: rgb(255, 0, 0);"&gt;public&lt;/span&gt;:&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:courier new;"&gt;C();&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:courier new;"&gt;C(&lt;span style="color: rgb(255, 0, 0);"&gt;int&lt;/span&gt;);&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:courier new;"&gt;&lt;span style="color: rgb(255, 0, 0);"&gt;friend&lt;/span&gt; C &lt;span style="color: rgb(255, 0, 0);"&gt;operator&lt;/span&gt;+( &lt;span style="color: rgb(255, 0, 0);"&gt;const&lt;/span&gt; C&amp;, &lt;span style="color: rgb(255, 0, 0);"&gt;const&lt;/span&gt; C&amp; );&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:courier new;"&gt;˜C();&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:courier new;"&gt;};&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="font-family:courier new;"&gt;C obj1;&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:courier new;"&gt;&lt;span style="color: rgb(255, 0, 0);"&gt;const&lt;/span&gt; C&amp; cr = C(16)+C(23);&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:courier new;"&gt;C obj2;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;the expression &lt;span style="font-style: italic;"&gt;C(16)+C(23)&lt;/span&gt; creates three temporaries. A first temporary &lt;span style="font-style: italic;"&gt;T1&lt;/span&gt; to hold the result of the expression &lt;span style="font-style: italic;"&gt;C(16)&lt;/span&gt;, a second temporary &lt;span style="font-style: italic;"&gt;T2&lt;/span&gt; to hold the result of the expression &lt;span style="font-style: italic;"&gt;C(23)&lt;/span&gt;, and a third temporary &lt;span style="font-style: italic;"&gt;T3&lt;/span&gt; to hold the result of the addition of these two expressions. The temporary &lt;span style="font-style: italic;"&gt;T3&lt;/span&gt; is then bound to the reference &lt;span style="font-style: italic;"&gt;cr&lt;/span&gt;. It is unspecified whether &lt;span style="font-style: italic;"&gt;T1&lt;/span&gt; or &lt;span style="font-style: italic;"&gt;T2&lt;/span&gt; is created first. On an implementation where &lt;span style="font-style: italic;"&gt;T1&lt;/span&gt; is created before &lt;span style="font-style: italic;"&gt;T2&lt;/span&gt;, it is guaranteed that &lt;span style="font-style: italic;"&gt;T2&lt;/span&gt; is destroyed before &lt;span style="font-style: italic;"&gt;T1&lt;/span&gt;. The temporaries &lt;span style="font-style: italic;"&gt;T1&lt;/span&gt; and &lt;span style="font-style: italic;"&gt;T2&lt;/span&gt; are bound to the reference parameters of &lt;span style="font-style: italic;"&gt;operator+&lt;/span&gt;; these temporaries are destroyed at the end of the full expression containing the call to &lt;span style="font-style: italic;"&gt;operator+&lt;/span&gt;. The temporary &lt;span style="font-style: italic;"&gt;T3&lt;/span&gt; bound to the reference &lt;span style="font-style: italic;"&gt;cr&lt;/span&gt; is destroyed at the end of &lt;span style="font-style: italic;"&gt;cr&lt;/span&gt;’s lifetime, that is, at the end of the program. In addition, the order in which &lt;span style="font-style: italic;"&gt;T3&lt;/span&gt; is destroyed takes into account the destruction order of other objects with static storage duration. That is, because &lt;span style="font-style: italic;"&gt;obj1&lt;/span&gt; is constructed before &lt;span style="font-style: italic;"&gt;T3&lt;/span&gt;, and &lt;span style="font-style: italic;"&gt;T3&lt;/span&gt; is constructed before &lt;span style="font-style: italic;"&gt;obj2&lt;/span&gt;, it is guaranteed that &lt;span style="font-style: italic;"&gt;obj2&lt;/span&gt; is destroyed before &lt;span style="font-style: italic;"&gt;T3&lt;/span&gt;, and that &lt;span style="font-style: italic;"&gt;T3&lt;/span&gt; is destroyed before &lt;span style="font-style: italic;"&gt;obj1&lt;/span&gt;. ]&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/1456844729588229023-3167535062240867744?l=zamanbakshifirst.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://zamanbakshifirst.blogspot.com/feeds/3167535062240867744/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=1456844729588229023&amp;postID=3167535062240867744' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/1456844729588229023/posts/default/3167535062240867744'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/1456844729588229023/posts/default/3167535062240867744'/><link rel='alternate' type='text/html' href='http://zamanbakshifirst.blogspot.com/2006/11/c-all-about-temporaries.html' title='C++ : All About Temporaries'/><author><name>Zaman Bakshi</name><uri>http://www.blogger.com/profile/09870326614850200376</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></entry><entry><id>tag:blogger.com,1999:blog-1456844729588229023.post-3638030364651640443</id><published>2006-11-17T01:05:00.000+05:30</published><updated>2006-11-17T01:14:15.400+05:30</updated><category scheme='http://www.blogger.com/atom/ns#' term='Object Destruction'/><category scheme='http://www.blogger.com/atom/ns#' term='Destruction'/><category scheme='http://www.blogger.com/atom/ns#' term='Object Destruction Process'/><category scheme='http://www.blogger.com/atom/ns#' term='zaman bakshi'/><category scheme='http://www.blogger.com/atom/ns#' term='Destructor'/><category scheme='http://www.blogger.com/atom/ns#' term='C++'/><title type='text'>C++ : The Object Destruction Process</title><content type='html'>&lt;p class="MsoNormal" style="line-height: 130%; text-align: left;" align="left"&gt;&lt;span style="font-size: 9pt; color: rgb(51, 51, 51); line-height: 130%; font-family: Verdana;" lang="EN-US"&gt;A user-defined destructor is augmented in much &lt;a href="http://zamanbakshifirst.blogspot.com/2006/11/c-object-construction-process.html"&gt;the same way as are the  constructors&lt;/a&gt;, except in reverse order:&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt; &lt;p class="MsoNormal" style="margin-left: 36pt; text-indent: -18pt; line-height: 130%; text-align: left;" align="left"&gt;&lt;span style="font-size: 9pt; color: rgb(51, 51, 51); line-height: 130%; font-family: Verdana;" lang="EN-US"&gt;&lt;span style=""&gt;1.&lt;span style="font-family: 'Times New Roman'; font-style: normal; font-variant: normal; font-weight: normal; font-size: 7pt; line-height: normal; font-size-adjust: none; font-stretch: normal;"&gt;       &lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size: 9pt; color: rgb(51, 51, 51); line-height: 130%; font-family: Verdana;" lang="EN-US"&gt;If  the object contains a &lt;a href="http://zamanbakshifirst.blogspot.com/2006/11/what-are-vptr-and-vtbl-in-c.html"&gt;&lt;span class="SpellE"&gt;vptr&lt;/span&gt;&lt;/a&gt;, it is reset to the &lt;a href="http://zamanbakshifirst.blogspot.com/2006/11/what-are-vptr-and-vtbl-in-c.html"&gt;virtual  table&lt;/a&gt; associated with the class.&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt; &lt;p class="MsoNormal" style="margin-left: 36pt; text-indent: -18pt; line-height: 130%; text-align: left;" align="left"&gt;&lt;span style="font-size: 9pt; color: rgb(51, 51, 51); line-height: 130%; font-family: Verdana;" lang="EN-US"&gt;&lt;span style=""&gt;2.&lt;span style="font-family: 'Times New Roman'; font-style: normal; font-variant: normal; font-weight: normal; font-size: 7pt; line-height: normal; font-size-adjust: none; font-stretch: normal;"&gt;       &lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size: 9pt; color: rgb(51, 51, 51); line-height: 130%; font-family: Verdana;" lang="EN-US"&gt;The  body of the destructor is then executed; that is, the &lt;span class="SpellE"&gt;vptr&lt;/span&gt; is reset prior to evaluating the user-supplied  code.&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt; &lt;p class="MsoNormal" style="margin-left: 36pt; text-indent: -18pt; line-height: 130%; text-align: left;" align="left"&gt;&lt;span style="font-size: 9pt; color: rgb(51, 51, 51); line-height: 130%; font-family: Verdana;" lang="EN-US"&gt;&lt;span style=""&gt;3.&lt;span style="font-family: 'Times New Roman'; font-style: normal; font-variant: normal; font-weight: normal; font-size: 7pt; line-height: normal; font-size-adjust: none; font-stretch: normal;"&gt;       &lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size: 9pt; color: rgb(51, 51, 51); line-height: 130%; font-family: Verdana;" lang="EN-US"&gt;If  the class has member class objects with destructors, these are invoked in the  reverse order of their declaration.&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt; &lt;p class="MsoNormal" style="margin-left: 36pt; text-indent: -18pt; line-height: 130%; text-align: left;" align="left"&gt;&lt;span style="font-size: 9pt; color: rgb(51, 51, 51); line-height: 130%; font-family: Verdana;" lang="EN-US"&gt;&lt;span style=""&gt;4.&lt;span style="font-family: 'Times New Roman'; font-style: normal; font-variant: normal; font-weight: normal; font-size: 7pt; line-height: normal; font-size-adjust: none; font-stretch: normal;"&gt;       &lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size: 9pt; color: rgb(51, 51, 51); line-height: 130%; font-family: Verdana;" lang="EN-US"&gt;If  there are any immediate &lt;span class="SpellE"&gt;non-virtual&lt;/span&gt; base classes with  destructors, these are invoked in the reverse order of their  declaration.&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt; &lt;p class="MsoNormal" style="margin-left: 36pt; text-indent: -18pt; line-height: 130%; text-align: left;" align="left"&gt;&lt;span style="font-size: 9pt; color: rgb(51, 51, 51); line-height: 130%; font-family: Verdana;" lang="EN-US"&gt;&lt;span style=""&gt;5.&lt;span style="font-family: 'Times New Roman'; font-style: normal; font-variant: normal; font-weight: normal; font-size: 7pt; line-height: normal; font-size-adjust: none; font-stretch: normal;"&gt;       &lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size: 9pt; color: rgb(51, 51, 51); line-height: 130%; font-family: Verdana;" lang="EN-US"&gt;If  there are any virtual base classes with destructors and this class represents  the most-derived class, these are invoked in the reverse order of their original  construction.&lt;/span&gt;&lt;/p&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/1456844729588229023-3638030364651640443?l=zamanbakshifirst.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://zamanbakshifirst.blogspot.com/feeds/3638030364651640443/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=1456844729588229023&amp;postID=3638030364651640443' title='4 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/1456844729588229023/posts/default/3638030364651640443'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/1456844729588229023/posts/default/3638030364651640443'/><link rel='alternate' type='text/html' href='http://zamanbakshifirst.blogspot.com/2006/11/c-object-destruction-process.html' title='C++ : The Object Destruction Process'/><author><name>Zaman Bakshi</name><uri>http://www.blogger.com/profile/09870326614850200376</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>4</thr:total></entry><entry><id>tag:blogger.com,1999:blog-1456844729588229023.post-9217610856071756625</id><published>2006-11-13T09:07:00.000+05:30</published><updated>2006-11-13T09:21:24.499+05:30</updated><category scheme='http://www.blogger.com/atom/ns#' term='constructor'/><category scheme='http://www.blogger.com/atom/ns#' term='zaman bakshi'/><category scheme='http://www.blogger.com/atom/ns#' term='object construction process'/><category scheme='http://www.blogger.com/atom/ns#' term='object construction'/><category scheme='http://www.blogger.com/atom/ns#' term='constructor augmentation'/><category scheme='http://www.blogger.com/atom/ns#' term='C++'/><title type='text'>C++ : The Object Construction Process</title><content type='html'>&lt;p class="MsoNormal" style="line-height: 130%; text-align: left; font-family: georgia;" align="left"&gt;&lt;span style="font-size:100%;"&gt;&lt;span style="font-size: 9pt; color: rgb(51, 51, 51); line-height: 130%;" lang="EN-US"&gt;&lt;span style="font-size:100%;"&gt;When  we define an object, such as&lt;/span&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt; &lt;p class="MsoNormal" style="text-align: left; font-family: georgia;" align="left"&gt;&lt;span style="font-size:100%;"&gt;&lt;span style="font-size: 9.5pt; color: rgb(121, 0, 41);" lang="EN-US"&gt;&lt;span style="font-family: courier new;font-size:100%;" &gt;T  object;&lt;/span&gt; &lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt; &lt;p class="MsoNormal" style="line-height: 130%; text-align: left; font-family: georgia;" align="left"&gt;&lt;span style="font-size:100%;"&gt;&lt;span style="font-size:130%;"&gt;&lt;span style="font-size: 9pt; color: rgb(51, 51, 51); line-height: 130%;" lang="EN-US"&gt;&lt;span style="font-size:100%;"&gt;exactly  what happens? If there is a constructor associated &lt;/span&gt;&lt;span style="font-size:100%;"&gt;with&lt;/span&gt; &lt;/span&gt;&lt;span style="font-size: 10pt; color: rgb(121, 0, 41); line-height: 130%;" lang="EN-US"&gt;T&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size: 9pt; color: rgb(51, 51, 51); line-height: 130%;" lang="EN-US"&gt;&lt;span style="font-size:100%;"&gt;  (either user supplied or synthesized by the compiler), it is invoked. That's  obvious. What is sometimes less obvious is what the invocation of a constructor  actually entails.&lt;/span&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt; &lt;p class="MsoNormal" style="line-height: 130%; text-align: left; font-family: georgia;" align="left"&gt;&lt;span style="font-size:100%;"&gt;&lt;span style="font-size:100%;"&gt;&lt;span style="font-size:180%;"&gt;&lt;span style="font-size: 9pt; color: rgb(51, 51, 51); line-height: 130%;" lang="EN-US"&gt;&lt;span style="font-size:100%;"&gt;Constructors  can contain a great deal of hidden program code because the compiler augments  every constructor to a greater or lesser extent depending on the com&lt;/span&gt;&lt;span style="font-size:100%;"&gt;plexity of&lt;/span&gt;  &lt;/span&gt;&lt;/span&gt;&lt;span style="font-size: 10pt; color: rgb(121, 0, 41); line-height: 130%;" lang="EN-US"&gt;T&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size: 9pt; color: rgb(51, 51, 51); line-height: 130%;" lang="EN-US"&gt;&lt;span style="font-size:100%;"&gt;&lt;span style="font-size:100%;"&gt;'s&lt;/span&gt;  class hierarchy. The general sequence of compiler augmentations is as  follows:&lt;/span&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt; &lt;p class="MsoNormal" style="margin-left: 36pt; text-indent: -18pt; line-height: 130%; text-align: left; font-family: georgia;" align="left"&gt;&lt;span style="font-size:100%;"&gt;&lt;span style="font-size: 9pt; color: rgb(51, 51, 51); line-height: 130%;" lang="EN-US"&gt;&lt;span style=""&gt;1.&lt;span style="font-style: normal; font-variant: normal; font-weight: normal; font-size: 7pt; line-height: normal; font-size-adjust: none; font-stretch: normal;"&gt;       &lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size: 9pt; color: rgb(51, 51, 51); line-height: 130%;" lang="EN-US"&gt;&lt;span style="font-size:100%;"&gt;The  data members initialized in the member initialization list have to be entered  within the body of the constructor in the order of member  declaration.&lt;/span&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt; &lt;p class="MsoNormal" style="margin-left: 36pt; text-indent: -18pt; line-height: 130%; text-align: left; font-family: georgia;" align="left"&gt;&lt;span style="font-size:100%;"&gt;&lt;span style="font-size: 9pt; color: rgb(51, 51, 51); line-height: 130%;" lang="EN-US"&gt;&lt;span style=""&gt;2.&lt;span style="font-style: normal; font-variant: normal; font-weight: normal; font-size: 7pt; line-height: normal; font-size-adjust: none; font-stretch: normal;"&gt;       &lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size: 9pt; color: rgb(51, 51, 51); line-height: 130%;" lang="EN-US"&gt;&lt;span style="font-size:100%;"&gt;If  a member class object is not present in the member initialization list but has  an associated default constructor, that default constructor must be  invoked.&lt;/span&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt; &lt;p class="MsoNormal" style="margin-left: 36pt; text-indent: -18pt; line-height: 130%; text-align: left; font-family: georgia;" align="left"&gt;&lt;span style="font-size:100%;"&gt;&lt;span style="font-size: 9pt; color: rgb(51, 51, 51); line-height: 130%;" lang="EN-US"&gt;&lt;span style=""&gt;3.&lt;span style="font-style: normal; font-variant: normal; font-weight: normal; font-size: 7pt; line-height: normal; font-size-adjust: none; font-stretch: normal;"&gt;     &lt;span style="font-size:100%;"&gt;  &lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size: 9pt; color: rgb(51, 51, 51); line-height: 130%;" lang="EN-US"&gt;&lt;span style="font-size:100%;"&gt;Prior  to that, if there is a virtual table pointer (or pointers) contained within the  class object, it (they) must be initialized with the address of the appropriate  virtual table(s).&lt;/span&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt; &lt;p class="MsoNormal" style="margin-left: 36pt; text-indent: -18pt; line-height: 130%; text-align: left; font-family: georgia;" align="left"&gt;&lt;span style="font-size:100%;"&gt;&lt;span style="font-size: 9pt; color: rgb(51, 51, 51); line-height: 130%;" lang="EN-US"&gt;&lt;span style=""&gt;4.&lt;span style="font-style: normal; font-variant: normal; font-weight: normal; font-size: 7pt; line-height: normal; font-size-adjust: none; font-stretch: normal;"&gt;       &lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size: 9pt; color: rgb(51, 51, 51); line-height: 130%;" lang="EN-US"&gt;&lt;span style="font-size:100%;"&gt;Prior  to that, all immediate base class constructors must be invoked in the order of  base class declaration (the order within the member initialization list is not  relevant).&lt;/span&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt; &lt;p class="MsoNormal" style="margin-left: 72pt; text-indent: -18pt; line-height: 130%; text-align: left; font-family: georgia;" align="left"&gt;&lt;span style="font-size:100%;"&gt;&lt;span style="font-size: 10pt; color: rgb(51, 51, 51); line-height: 130%;" lang="EN-US"&gt;&lt;span style=""&gt;o&lt;span style="font-style: normal; font-variant: normal; font-weight: normal; font-size: 7pt; line-height: normal; font-size-adjust: none; font-stretch: normal;"&gt;       &lt;span style="font-size:100%;"&gt;  &lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size: 9pt; color: rgb(51, 51, 51); line-height: 130%;" lang="EN-US"&gt;&lt;span style="font-size:100%;"&gt;If  the base class is listed within the member initialization list, the explicit  arguments, if any, must be passed.&lt;/span&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt; &lt;p class="MsoNormal" style="margin-left: 72pt; text-indent: -18pt; line-height: 130%; text-align: left; font-family: georgia;" align="left"&gt;&lt;span style="font-size:100%;"&gt;&lt;span style="font-size: 10pt; color: rgb(51, 51, 51); line-height: 130%;" lang="EN-US"&gt;&lt;span style=""&gt;o&lt;span style="font-style: normal; font-variant: normal; font-weight: normal; font-size: 7pt; line-height: normal; font-size-adjust: none; font-stretch: normal;"&gt;         &lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size: 9pt; color: rgb(51, 51, 51); line-height: 130%;" lang="EN-US"&gt;&lt;span style="font-size:100%;"&gt;If  the base class is not listed within the member initialization list, the default  constructor (or default memberwise copy constructor -- bitwise copy) must be invoked, if  present.&lt;/span&gt;&lt;span style="font-size:100%;"&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt; &lt;p class="MsoNormal" style="margin-left: 72pt; text-indent: -18pt; line-height: 130%; text-align: left; font-family: georgia;" align="left"&gt;&lt;span style="font-size:100%;"&gt;&lt;span style="font-size:100%;"&gt;&lt;span style="font-size: 10pt; color: rgb(51, 51, 51); line-height: 130%;" lang="EN-US"&gt;&lt;span style=""&gt;o&lt;span style="font-style: normal; font-variant: normal; font-weight: normal; font-size: 7pt; line-height: normal; font-size-adjust: none; font-stretch: normal;"&gt;         &lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:100%;"&gt;&lt;span style="font-size: 9pt; color: rgb(51, 51, 51); line-height: 130%;" lang="EN-US"&gt;&lt;span style="font-size:100%;"&gt;If  the base class is a second or subsequent base class, the&lt;/span&gt; &lt;/span&gt;&lt;b&gt;&lt;span style="font-size: 9pt; color: rgb(51, 51, 51); line-height: 130%;" lang="EN-US"&gt;this&lt;/span&gt;&lt;/b&gt;&lt;/span&gt;&lt;span style="font-size: 9pt; color: rgb(51, 51, 51); line-height: 130%;" lang="EN-US"&gt;&lt;span style="font-size:100%;"&gt;  pointer must be adjusted.&lt;/span&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt; &lt;p class="MsoNormal" style="margin-left: 36pt; text-indent: -18pt; line-height: 130%; text-align: left; font-family: georgia;" align="left"&gt;&lt;span style="font-size:100%;"&gt;&lt;span style="font-size: 9pt; color: rgb(51, 51, 51); line-height: 130%;" lang="EN-US"&gt;&lt;span style=""&gt;5.&lt;span style="font-style: normal; font-variant: normal; font-weight: normal; font-size: 7pt; line-height: normal; font-size-adjust: none; font-stretch: normal;"&gt;       &lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size: 9pt; color: rgb(51, 51, 51); line-height: 130%;" lang="EN-US"&gt;&lt;span style="font-size:100%;"&gt;Prior  to that, all virtual base class constructors must be invoked in a left-to-right,  depth-first search of the inheritance hierarchy defined by the derived  class.&lt;/span&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt; &lt;p class="MsoNormal" style="margin-left: 72pt; text-indent: -18pt; line-height: 130%; text-align: left; font-family: georgia;" align="left"&gt;&lt;span style="font-size:100%;"&gt;&lt;span style="font-size: 10pt; color: rgb(51, 51, 51); line-height: 130%;" lang="EN-US"&gt;&lt;span style=""&gt;o&lt;span style="font-style: normal; font-variant: normal; font-weight: normal; font-size: 7pt; line-height: normal; font-size-adjust: none; font-stretch: normal;"&gt;       &lt;span style="font-size:100%;"&gt;  &lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size: 9pt; color: rgb(51, 51, 51); line-height: 130%;" lang="EN-US"&gt;&lt;span style="font-size:100%;"&gt;If  the class is listed within the member initialization list, the explicit  arguments, if any, must be passed. Otherwise, if there is a default constructor  associated with the class, it must be invoked.&lt;/span&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt; &lt;p class="MsoNormal" style="margin-left: 72pt; text-indent: -18pt; line-height: 130%; text-align: left; font-family: georgia;" align="left"&gt;&lt;span style="font-size:100%;"&gt;&lt;span style="font-size: 10pt; color: rgb(51, 51, 51); line-height: 130%;" lang="EN-US"&gt;&lt;span style=""&gt;o&lt;span style="font-style: normal; font-variant: normal; font-weight: normal; font-size: 7pt; line-height: normal; font-size-adjust: none; font-stretch: normal;"&gt;         &lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size: 9pt; color: rgb(51, 51, 51); line-height: 130%;" lang="EN-US"&gt;&lt;span style="font-size:100%;"&gt;In  addition, the offset of each virtual base class subobject within the class must  somehow be made accessible at runtime.&lt;/span&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt; &lt;p class="MsoNormal" style="margin-left: 72pt; text-indent: -18pt; line-height: 130%; text-align: left;" align="left"&gt;&lt;span style="font-size:100%;"&gt;&lt;span style="font-size: 10pt; color: rgb(51, 51, 51); line-height: 130%; font-family: georgia;" lang="EN-US"&gt;&lt;span style=""&gt;o&lt;span style="font-style: normal; font-variant: normal; font-weight: normal; font-size: 7pt; line-height: normal; font-size-adjust: none; font-stretch: normal;"&gt;         &lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size: 9pt; color: rgb(51, 51, 51); line-height: 130%; font-family: Verdana;" lang="EN-US"&gt;&lt;span style="font-size:100%;"&gt;&lt;span style="font-family: georgia;"&gt;These  constructors, however, may be invoked if, and only if, the class object  represents the "most-derived class." Some mechanism supporting this must be put  into place.&lt;/span&gt;&lt;/span&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt;&lt;br /&gt;&lt;span style="font-weight: bold;"&gt;Please click on the ads to your right&lt;/span&gt; :p&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/1456844729588229023-9217610856071756625?l=zamanbakshifirst.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://zamanbakshifirst.blogspot.com/feeds/9217610856071756625/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=1456844729588229023&amp;postID=9217610856071756625' title='5 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/1456844729588229023/posts/default/9217610856071756625'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/1456844729588229023/posts/default/9217610856071756625'/><link rel='alternate' type='text/html' href='http://zamanbakshifirst.blogspot.com/2006/11/c-object-construction-process.html' title='C++ : The Object Construction Process'/><author><name>Zaman Bakshi</name><uri>http://www.blogger.com/profile/09870326614850200376</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>5</thr:total></entry><entry><id>tag:blogger.com,1999:blog-1456844729588229023.post-1370716251003722346</id><published>2006-11-13T08:27:00.000+05:30</published><updated>2006-11-13T09:03:24.321+05:30</updated><category scheme='http://www.blogger.com/atom/ns#' term='member initialization list'/><category scheme='http://www.blogger.com/atom/ns#' term='initialization list'/><category scheme='http://www.blogger.com/atom/ns#' term='zaman bakshi'/><category scheme='http://www.blogger.com/atom/ns#' term='C++'/><title type='text'>C++ : Is Member Initialization List Efficient ?</title><content type='html'>Yes it is, and should be made a habit. Consider the following example:&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 0, 0);font-family:courier new;" &gt;class&lt;/span&gt;&lt;span style="font-family:courier new;"&gt; Anjelina {&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 0, 0);font-family:courier new;" &gt;public&lt;/span&gt;&lt;span style="font-family:courier new;"&gt;:&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:courier new;"&gt;// very naive ....&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:courier new;"&gt;Anjelina( ) {&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:courier new;"&gt;_name = 0; //_name is NULL now&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:courier new;"&gt;_blemish = 0;&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:courier new;"&gt;}&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 0, 0);font-family:courier new;" &gt;private&lt;/span&gt;&lt;span style="font-family:courier new;"&gt;:&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:courier new;"&gt;String _name;&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 0, 0);font-family:courier new;" &gt;int&lt;/span&gt;&lt;span style="font-family:courier new;"&gt; _blemish;&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:courier new;"&gt;};&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 102, 0);"&gt;What happens before the start of constructor call?&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;An object is fully constructed. What !!! Yes, it is. Before the constructor call, the '&lt;span style="color: rgb(255, 0, 0);"&gt;this&lt;/span&gt;' pointer is initialized and all the members are constructed (their default constructors called). The member constructor is called if it is &lt;a href="http://zamanbakshifirst.blogspot.com/search/label/copy%20constructor"&gt;non-trivial&lt;/a&gt;. So, what's wrong in the above constructor? Nothing, it's just naive. The reason is, that the constructor of _name is already called and String already initialized to 0 (default behavior for String object). The code transformation that takes place is:&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(51, 204, 0);font-family:courier new;" &gt;// Pseudo C++ Code&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:courier new;"&gt;Angelina::Angelina (&lt;span style="color: rgb(51, 204, 0);"&gt;/* 'this' pointer goes here */&lt;/span&gt;) {&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(51, 204, 0);font-family:courier new;" &gt;// invoke default String constructor&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:courier new;"&gt;_name.String::String ( ) ;&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(51, 204, 0);font-family:courier new;" &gt;// generate temporary&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:courier new;"&gt;String temp = String ( 0 ) ;&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(51, 204, 0);font-family:courier new;" &gt;// memberwise copy _name&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:courier new;"&gt;_name.String::operator= ( temp ) ;&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(51, 204, 0);font-family:courier new;" &gt;// destroy temporary&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:courier new;"&gt;_name.String::~String( );&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="font-family:courier new;"&gt;_blemish = 0 ;&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:courier new;"&gt;}&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;So the proper code for constructor should be:&lt;br /&gt;&lt;br /&gt;&lt;span style="font-family:courier new;"&gt;Anjelina( ):_name(0) {&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:courier new;"&gt;_blemish = 0;&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:courier new;"&gt;}&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;or even better (non-confusing code) ...&lt;br /&gt;&lt;br /&gt;&lt;span style="font-family:courier new;"&gt;Anjelina( ):_name(0), _blemish(0) { }&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;This will call the constructor with 0 as an argument. If you want String's default constructor to be called, you can write _name( ).&lt;br /&gt;&lt;br /&gt;&lt;span style="font-weight: bold;"&gt;Please click on the ads to your right&lt;/span&gt; :p&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/1456844729588229023-1370716251003722346?l=zamanbakshifirst.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://zamanbakshifirst.blogspot.com/feeds/1370716251003722346/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=1456844729588229023&amp;postID=1370716251003722346' title='5 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/1456844729588229023/posts/default/1370716251003722346'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/1456844729588229023/posts/default/1370716251003722346'/><link rel='alternate' type='text/html' href='http://zamanbakshifirst.blogspot.com/2006/11/c-is-member-initialization-list.html' title='C++ : Is Member Initialization List Efficient ?'/><author><name>Zaman Bakshi</name><uri>http://www.blogger.com/profile/09870326614850200376</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>5</thr:total></entry><entry><id>tag:blogger.com,1999:blog-1456844729588229023.post-5471567946799953048</id><published>2006-11-13T08:23:00.001+05:30</published><updated>2009-11-24T16:49:39.123+05:30</updated><category scheme='http://www.blogger.com/atom/ns#' term='member initialization list'/><category scheme='http://www.blogger.com/atom/ns#' term='initialization list'/><category scheme='http://www.blogger.com/atom/ns#' term='zaman bakshi'/><category scheme='http://www.blogger.com/atom/ns#' term='C++'/><title type='text'>C++ : Is Member Initialization List a Must ?</title><content type='html'>No. But is a &lt;span style="font-weight: bold;"&gt;must&lt;/span&gt; in the following cases:&lt;br /&gt;&lt;br /&gt;&lt;ol style="font-style: italic;"&gt;&lt;li&gt;When initializing a &lt;span style="font-weight: bold;"&gt;reference&lt;/span&gt; member.&lt;/li&gt;&lt;li&gt;When initializing a &lt;span style="font-weight: bold; color: rgb(255, 0, 0);"&gt;'const'&lt;/span&gt; member.&lt;/li&gt;&lt;li&gt;When invoking a base or member class constructor with a set of arguments.&lt;/li&gt;&lt;/ol&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/1456844729588229023-5471567946799953048?l=zamanbakshifirst.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://zamanbakshifirst.blogspot.com/feeds/5471567946799953048/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=1456844729588229023&amp;postID=5471567946799953048' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/1456844729588229023/posts/default/5471567946799953048'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/1456844729588229023/posts/default/5471567946799953048'/><link rel='alternate' type='text/html' href='http://zamanbakshifirst.blogspot.com/2006/11/c-is-member-initialization-list-must.html' title='C++ : Is Member Initialization List a Must ?'/><author><name>Zaman Bakshi</name><uri>http://www.blogger.com/profile/09870326614850200376</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></entry><entry><id>tag:blogger.com,1999:blog-1456844729588229023.post-4676333904684352394</id><published>2006-11-13T01:17:00.001+05:30</published><updated>2009-11-24T16:48:30.502+05:30</updated><category scheme='http://www.blogger.com/atom/ns#' term='zaman bakshi'/><category scheme='http://www.blogger.com/atom/ns#' term='Bjarne'/><category scheme='http://www.blogger.com/atom/ns#' term='undefined behavior'/><category scheme='http://www.blogger.com/atom/ns#' term='C++'/><category scheme='http://www.blogger.com/atom/ns#' term='undefined'/><category scheme='http://www.blogger.com/atom/ns#' term='i++ + i++'/><title type='text'>C++ : What's the value of i++ + i++?</title><content type='html'>It's &lt;a href="http://zamanbakshifirst.blogspot.com/2006/11/c-whats-undefined-behavior-in-c.html"&gt;undefined&lt;/a&gt; !! Basically, in C and C++, if you read a variable twice in an expression where you also write it, the result is undefined. Don't do that. Another example is: &lt;pre&gt; v[i] = i++;&lt;br /&gt;&lt;/pre&gt; Related example: &lt;pre&gt; f(v[i],i++);&lt;br /&gt;&lt;/pre&gt; Here, the result is undefined because the order of evaluation of function arguments are undefined.&lt;br /&gt;&lt;br /&gt;You can even check it from &lt;a href="http://www.research.att.com/%7Ebs/bs_faq2.html"&gt;Bjarne's own blog on  C++ FAQs&lt;/a&gt;.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/1456844729588229023-4676333904684352394?l=zamanbakshifirst.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://zamanbakshifirst.blogspot.com/feeds/4676333904684352394/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=1456844729588229023&amp;postID=4676333904684352394' title='3 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/1456844729588229023/posts/default/4676333904684352394'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/1456844729588229023/posts/default/4676333904684352394'/><link rel='alternate' type='text/html' href='http://zamanbakshifirst.blogspot.com/2006/11/c-whats-value-of-i-i.html' title='C++ : What&apos;s the value of i++ + i++?'/><author><name>Zaman Bakshi</name><uri>http://www.blogger.com/profile/09870326614850200376</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>3</thr:total></entry><entry><id>tag:blogger.com,1999:blog-1456844729588229023.post-2314331053740638627</id><published>2006-11-13T00:17:00.002+05:30</published><updated>2009-11-24T16:47:30.562+05:30</updated><category scheme='http://www.blogger.com/atom/ns#' term='zaman bakshi'/><category scheme='http://www.blogger.com/atom/ns#' term='undefined behavior'/><category scheme='http://www.blogger.com/atom/ns#' term='C++'/><category scheme='http://www.blogger.com/atom/ns#' term='undefined'/><title type='text'>C++ : What's Undefined behavior in C++ ?</title><content type='html'>Let me quote here &lt;a href="http://www.esnips.com/web/MyComputerPapers"&gt;the 2003 standard for C++&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="font-style: italic;"&gt;...&lt;/span&gt;&lt;br /&gt;&lt;span style="font-style: italic;"&gt;1.3.12 undefined behavior&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="font-style: italic;"&gt;behavior, such as might arise upon use of an erroneous program construct or erroneous data, for which this International Standard imposes no requirements. Undefined behavior may also be expected when this International Standard omits the description of any explicit definition of behavior. [Note: permissible undefined behavior ranges from ignoring the situation completely with unpredictable results, to behaving during translation or program execution in a documented manner characteristic of the environment (with or without the issuance of a diagnostic message), to terminating a translation or execution (with the issuance of a diagnostic message). Many erroneous program constructs do not engender undefined behavior; they are required to be diagnosed. ]&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="font-style: italic;"&gt;1.3.13 unspecified behavior&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="font-style: italic;"&gt;behavior, for a well-formed program construct and correct data, that depends on the implementation. The implementation is not required to document which behavior occurs. [Note: usually, the range of possible behaviors is delineated by this International Standard. ]&lt;/span&gt;&lt;br /&gt;&lt;span style="font-style: italic;"&gt;...&lt;br /&gt;&lt;br /&gt;&lt;/span&gt;One of the frequently asked questions to &lt;a href="http://www.research.att.com/%7Ebs/bs_faq2.html"&gt;Bjarne&lt;/a&gt; is:&lt;br /&gt;&lt;br /&gt;&lt;span style="font-weight: bold;"&gt;Why are some things left undefined in C++?&lt;/span&gt;&lt;br /&gt;&lt;span style="font-style: italic;"&gt;&lt;/span&gt;&lt;h2&gt;&lt;a name="undefined"&gt;&lt;/a&gt;&lt;/h2&gt;&lt;h2&gt;&lt;span style="font-size:100%;"&gt;&lt;a name="undefined"&gt; &lt;/a&gt;&lt;/span&gt;&lt;/h2&gt; &lt;p&gt;And here's his answer....&lt;br /&gt;&lt;/p&gt;&lt;p style="font-style: italic;"&gt; Because machines differ and because C left many things undefined. For details, including definitions of the terms "undefined", "unspecified", "implementation defined", and "well-formed"; see the &lt;a href="http://www.esnips.com/web/MyComputerPapers"&gt;ISO C++ standard&lt;/a&gt;. Note that the meaning of those terms differ from their definition of the &lt;a href="http://www.esnips.com/web/MyComputerPapers"&gt;ISO C standard&lt;/a&gt; and from some common usage. You can get wonderfully confused discussions when people don't realize that not everybody share definitions. &lt;/p&gt;&lt;p style="font-style: italic;"&gt; This is a correct, if unsatisfactory, answer. Like C, C++ is meant to exploit hardware directly and efficiently. This implies that C++ must deal with hardware entities such as bits, bytes, words, addresses, integer computations, and floating-point computations they way they are on a given machine, rather than how we might like them to be. &lt;span style="color: rgb(255, 102, 102);"&gt;Note that many "things" that people refer to as "undefined" are in fact "implementation defined"&lt;/span&gt;, so that we can write perfectly specified code as long as we know which machine we are running on. Sizes of integers and the rounding behavior of floating-point computations fall into that category. &lt;/p&gt;&lt;p style="font-style: italic;"&gt; Consider what is probably the the best known and most infamous example of undefined behavior: &lt;/p&gt;&lt;pre style="font-style: italic;"&gt;&lt;span style="color: rgb(255, 0, 0);"&gt;int&lt;/span&gt; a[10];&lt;br /&gt;a[100] = 0; &lt;span style="color: rgb(51, 255, 51);"&gt;// range error&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 0, 0);"&gt;int&lt;/span&gt;* p = a;&lt;br /&gt;&lt;span style="color: rgb(51, 255, 51);"&gt;// ...&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(51, 255, 51);"&gt;// range error (unless we gave p a better &lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(51, 255, 51);"&gt;// value before that assignment)&lt;/span&gt;&lt;br /&gt;p[100] = 0;&lt;br /&gt;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="font-style: italic;"&gt; The C++ (and C) notion of array and pointer are direct representations of a machine's notion of memory and addresses, provided with no overhead. The primitive operations on pointers map directly onto machine instructions. In particular, no range checking is done. Doing range checking would impose a cost in terms of run time and code size. C was designed to outcompete assembly code for operating systems tasks, so that was a necessary decision. Also, C -- unlike C++ -- has no reasonable way of reporting a violation had a compiler decided to generate code to detect it: There are no exceptions in C. C++ followed C for reasons of compatibility and because it too compete directly with assembler (in OS, embedded systems, and some numeric computation areas). &lt;span style="color: rgb(255, 102, 102);"&gt;If you want range checking, use a suitable checked class (vector, smart pointer, string, etc.)&lt;/span&gt;. A good compiler could catch the range error for a[100] at compile time, catching the one for p[100] is far more difficult, and in general it is impossible to catch every range error at compile time. &lt;/span&gt;&lt;p style="font-style: italic;"&gt; Other examples of undefined behavior stems from the compilation model. A compiler cannot detect an inconsistent definition of an object or a function in separately-compiled translation units. For example: &lt;/p&gt;&lt;pre style="font-style: italic;"&gt;&lt;span style="color: rgb(51, 255, 51);"&gt;// file1.c:&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 0, 0);"&gt;struct&lt;/span&gt; S { &lt;span style="color: rgb(255, 0, 0);"&gt;int&lt;/span&gt; x,y; };&lt;br /&gt;&lt;span style="color: rgb(255, 0, 0);"&gt;int&lt;/span&gt; f(&lt;span style="color: rgb(255, 0, 0);"&gt;struct&lt;/span&gt; S* p) { &lt;span style="color: rgb(255, 0, 0);"&gt;return&lt;/span&gt; p-&gt;x; }&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(51, 255, 51);"&gt;// file2.c:&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 0, 0);"&gt;struct&lt;/span&gt; S { &lt;span style="color: rgb(255, 0, 0);"&gt;int&lt;/span&gt; y,x; };&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 0, 0);"&gt;int&lt;/span&gt; main(){&lt;br /&gt;&lt;span style="color: rgb(255, 0, 0);"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;struct&lt;/span&gt; S s;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;s.x = 1;&lt;br /&gt;&lt;span style="color: rgb(255, 0, 0);"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;int&lt;/span&gt; x = f(&amp;s); // x!=s.x !!&lt;br /&gt;&lt;span style="color: rgb(255, 0, 0);"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;return&lt;/span&gt; 2;&lt;br /&gt;}&lt;br /&gt;&lt;/pre&gt;&lt;span style="font-style: italic;"&gt; Compiling file1.c and file2.c and linking the results into the same program is illegal in both C and C++. A linker could catch the inconsistent definition of S, but is not obliged to do so (and most don't). In many cases, it can be quite difficult to catch inconsistencies between separately compiled translation units. Consistent use of header files helps minimize such problems and there are some signs that linkers are improving. Note that C++ linkers do catch almost errors related to inconsistently declared functions. &lt;/span&gt;&lt;p style="font-style: italic;"&gt; Finally, we have the apparently unnecessary and rather annoying undefined behavior of individual expressions. For example: &lt;/p&gt;&lt;pre style="font-style: italic;"&gt;&lt;span style="color: rgb(255, 0, 0);"&gt;void&lt;/span&gt; out1 { cout &lt;&lt; 1 ; }&lt;br /&gt;&lt;span style="color: rgb(255, 0, 0);"&gt;void&lt;/span&gt; out2 { cout &lt;&lt; 2 ; }&lt;br /&gt;&lt;span style="color: rgb(255, 0, 0);"&gt;int&lt;/span&gt; main(){&lt;br /&gt;&lt;span style="color: rgb(255, 0, 0);"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;int&lt;/span&gt; i = 10;&lt;br /&gt;&lt;span style="color: rgb(255, 0, 0);"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;int&lt;/span&gt; j = ++i + i++; &lt;span&gt;&lt;span style="color: rgb(51, 255, 51);"&gt;// value of j unspecified&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;f(out1(),out2());  &lt;span style="color: rgb(51, 255, 51);"&gt;// prints 12 or 21&lt;/span&gt;&lt;br /&gt;}&lt;br /&gt;&lt;/pre&gt;&lt;span style="font-style: italic;"&gt; The value of j is unspecified to allow compilers to produce optimal code. It is claimed that the difference between what can be produced giving the compiler this freedom and requiring "ordinary left-to-right evaluation" can be significant. I'm unconvinced, but with innumerable compilers "out there" taking advantage of the freedom and some people passionately defending that freedom, a change would be difficult and could take decades to penetrate to the distant corners of the C and C++ worlds. I am disappointed that not all compilers warn against code such as ++i+i++. &lt;span style="color: rgb(255, 102, 102);"&gt;Similarly, the order of evaluation of arguments is unspecified.&lt;/span&gt; &lt;/span&gt;&lt;p&gt;&lt;span style="font-style: italic;"&gt; IMO far too many "things" are left undefined, unspecified, implementation-defined, etc. However, that's easy to say and even to give examples of, but hard to fix. It should also be noted that it is not all that difficult to avoid most of the problems and produce portable code.&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span&gt;&lt;br /&gt;&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span&gt;This is &lt;a href="http://www.research.att.com/%7Ebs/bs_faq2.html"&gt;Bjarne&lt;/a&gt; at his best.&lt;/span&gt;&lt;/p&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/1456844729588229023-2314331053740638627?l=zamanbakshifirst.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://zamanbakshifirst.blogspot.com/feeds/2314331053740638627/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=1456844729588229023&amp;postID=2314331053740638627' title='3 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/1456844729588229023/posts/default/2314331053740638627'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/1456844729588229023/posts/default/2314331053740638627'/><link rel='alternate' type='text/html' href='http://zamanbakshifirst.blogspot.com/2006/11/c-whats-undefined-behavior-in-c.html' title='C++ : What&apos;s Undefined behavior in C++ ?'/><author><name>Zaman Bakshi</name><uri>http://www.blogger.com/profile/09870326614850200376</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>3</thr:total></entry><entry><id>tag:blogger.com,1999:blog-1456844729588229023.post-5328962175640986720</id><published>2006-11-08T19:45:00.001+05:30</published><updated>2009-11-24T16:39:27.061+05:30</updated><category scheme='http://www.blogger.com/atom/ns#' term='C'/><category scheme='http://www.blogger.com/atom/ns#' term='zaman bakshi'/><category scheme='http://www.blogger.com/atom/ns#' term='main()'/><category scheme='http://www.blogger.com/atom/ns#' term='main'/><category scheme='http://www.blogger.com/atom/ns#' term='C++'/><category scheme='http://www.blogger.com/atom/ns#' term='return type'/><category scheme='http://www.blogger.com/atom/ns#' term='function main()'/><title type='text'>C++ / C: Should main ( ) return void?</title><content type='html'>&lt;span style="font-weight: bold;"&gt;The answer is 'NO'&lt;/span&gt;. 'void' has never been the return type of main() in C++ and nor C. You should return 'int'. Example,&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 0, 0); font-style: italic;"&gt;int&lt;/span&gt;&lt;span style="font-style: italic;"&gt; main ( ) {&lt;/span&gt;&lt;br /&gt;&lt;span style="font-style: italic;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;std::cout&lt;&lt; "Hello Mama";&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 0, 0); font-style: italic;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;return&lt;/span&gt;&lt;span style="font-style: italic;"&gt; 0;&lt;/span&gt;&lt;br /&gt;&lt;span style="font-style: italic;"&gt;}&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 153, 0);font-family:arial;" &gt;What does 'The C++ Standard' say?&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Let me quote &lt;a href="http://www.esnips.com/web/MyComputerPapers"&gt;'The C++ Standard'&lt;/a&gt; here&lt;br /&gt;&lt;br /&gt;&lt;span style="font-weight: bold;"&gt;3.6.1 Main function&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="font-style: italic;"&gt;....&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="font-weight: bold; font-style: italic;"&gt;2&lt;/span&gt;&lt;span style="font-style: italic;"&gt; An implementation shall not predefine the main function. This function shall not be overloaded. It shall have a return type of type int, but otherwise its type is implementation-defined. All implementations shall allow both of the following definitions of main:&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 0, 0); font-style: italic;"&gt;int&lt;/span&gt;&lt;span style="font-style: italic;"&gt; main() { /* ... */ }&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="font-style: italic;"&gt;and&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 0, 0); font-style: italic;"&gt;int&lt;/span&gt;&lt;span style="font-style: italic;"&gt; main(&lt;/span&gt;&lt;span style="color: rgb(255, 0, 0); font-style: italic;"&gt;int&lt;/span&gt;&lt;span style="font-style: italic;"&gt; argc, &lt;/span&gt;&lt;span style="color: rgb(255, 0, 0); font-style: italic;"&gt;char&lt;/span&gt;&lt;span style="font-style: italic;"&gt;* argv[]) { /* ... */ }&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;.... and it continues to add ...&lt;br /&gt;&lt;br /&gt;&lt;span style="font-weight: bold; font-style: italic;"&gt;5&lt;/span&gt;&lt;span style="font-style: italic;"&gt; A return statement in main has the effect of leaving the main function (destroying any objects with automatic storage duration) and calling exit with the return value as the argument. If control reaches the end of main without encountering a return statement, the effect is that of executing &lt;/span&gt;&lt;span style="color: rgb(255, 0, 0); font-style: italic;"&gt;return&lt;/span&gt;&lt;span style="font-style: italic;"&gt; 0;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;So, now you know what the standard states. Shun the books that don't understand the standard properly.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/1456844729588229023-5328962175640986720?l=zamanbakshifirst.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://zamanbakshifirst.blogspot.com/feeds/5328962175640986720/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=1456844729588229023&amp;postID=5328962175640986720' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/1456844729588229023/posts/default/5328962175640986720'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/1456844729588229023/posts/default/5328962175640986720'/><link rel='alternate' type='text/html' href='http://zamanbakshifirst.blogspot.com/2006/11/c-c-main-should-return-void.html' title='C++ / C: Should main ( ) return void?'/><author><name>Zaman Bakshi</name><uri>http://www.blogger.com/profile/09870326614850200376</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></entry><entry><id>tag:blogger.com,1999:blog-1456844729588229023.post-5296137349854191729</id><published>2006-11-08T00:31:00.001+05:30</published><updated>2009-11-24T16:19:18.488+05:30</updated><category scheme='http://www.blogger.com/atom/ns#' term='zaman bakshi'/><category scheme='http://www.blogger.com/atom/ns#' term='c portability'/><category scheme='http://www.blogger.com/atom/ns#' term='vptr'/><category scheme='http://www.blogger.com/atom/ns#' term='C++'/><title type='text'>C++: I am sure that vptr is stored as a first member of the object !</title><content type='html'>No, you're wrong. You need to read on...&lt;br /&gt;&lt;br /&gt;&lt;span style="font-weight: bold;"&gt;Eligibility:&lt;/span&gt; &lt;span style="font-style: italic;"&gt;You should know what &lt;a href="http://zamanbakshifirst.blogspot.com/2006/11/what-are-vptr-and-vtbl-in-c.html"&gt;vptr&lt;/a&gt; stands for and what are its functions, and you should have basic familiarity with the C++ language.&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 153, 0);font-family:arial;" &gt;What does 'The C++ Standard' say?&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;a href="http://www.esnips.com/web/MyComputerPapers"&gt;The C++ Standard&lt;/a&gt; is noncommittal about it. Because the standard describes C++ Language and not it's implementation.&lt;br /&gt;&lt;br /&gt;The first implementation of C++ (by &lt;a href="http://www.research.att.com/%7Ebs/"&gt;Bjarne Stroustrup&lt;/a&gt;) has vptr as the last member of the object/sub-object. But the Microsoft Compiler as some others have it as the first member. What are the consequences of it's positioning within the object? Lets see...&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 153, 0);font-family:arial;" &gt;The C language portability&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;One current topic of debate within the C++ community concerns where best to locate the vptr within the class object. In the original &lt;span style="font-weight: bold;"&gt;cfront&lt;/span&gt; implementation, it was placed at the end of the class object in order to support the following inheritance pattern, shown in the figure below:&lt;br /&gt;&lt;br /&gt;&lt;a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="http://photos1.blogger.com/blogger2/927/177837491404813/1600/obj2.jpg"&gt;&lt;img style="margin: 0pt 10px 10px 0pt; float: left; cursor: pointer;" src="http://photos1.blogger.com/blogger2/927/177837491404813/400/obj2.jpg" alt="" border="0" /&gt;&lt;/a&gt;&lt;span style="color: rgb(255, 0, 0); font-style: italic;"&gt;struct&lt;/span&gt;&lt;span style="font-style: italic;"&gt; no_virts {&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 0, 0); font-style: italic;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;int&lt;/span&gt;&lt;span style="font-style: italic;"&gt; d1, d2;&lt;/span&gt;&lt;br /&gt;&lt;span style="font-style: italic;"&gt;};&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 0, 0); font-style: italic;"&gt;class&lt;/span&gt;&lt;span style="font-style: italic;"&gt; has_virts: &lt;/span&gt;&lt;span style="color: rgb(255, 0, 0); font-style: italic;"&gt;public&lt;/span&gt;&lt;span style="font-style: italic;"&gt; no_virts {&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 0, 0); font-style: italic;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;public&lt;/span&gt;&lt;span style="font-style: italic;"&gt;:&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 0, 0); font-style: italic;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;virtual&lt;/span&gt;&lt;span style="font-style: italic;"&gt; &lt;/span&gt;&lt;span style="color: rgb(255, 0, 0); font-style: italic;"&gt;void&lt;/span&gt;&lt;span style="font-style: italic;"&gt; foo();&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(51, 204, 0); font-style: italic;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;// ...&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 0, 0); font-style: italic;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;private&lt;/span&gt;&lt;span style="font-style: italic;"&gt;:&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 0, 0); font-style: italic;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;int&lt;/span&gt;&lt;span style="font-style: italic;"&gt; d3;&lt;/span&gt;&lt;br /&gt;&lt;span style="font-style: italic;"&gt;};&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="font-style: italic;"&gt;no_virts *p = &lt;/span&gt;&lt;span style="color: rgb(255, 0, 0); font-style: italic;"&gt;new&lt;/span&gt;&lt;span style="font-style: italic;"&gt; has_virts;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;Placing the vptr at the end of the class object preserves the object layout of the base class C struct, thus permitting it's use within C code. This inheritance idiom is believed by many to have been more common when C++ was first introduced than currently.&lt;br /&gt;&lt;br /&gt;Subsequent to release 2.0, with its addition of support for multiple inheritance and abstract base classes, and the general rise in popularity of the OO paradigm, some implementations began placing the vptr at the start of the class object.&lt;br /&gt;&lt;br /&gt;Placing the vptr at the start of the class is more efficient in supporting some virtual function invocations through pointer to class members under multiple inheritance. Otherwise, not only must the offset to the start of the class be made available at runtime, but also the offset to the location of the vptr of that class must be made available. The trade-off, however, is a loss in C language interoperability. How significant a loss? What percentage of programs derive polymorphic class from a C-language struct? There are currently no empirical numbers to support either position.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/1456844729588229023-5296137349854191729?l=zamanbakshifirst.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://zamanbakshifirst.blogspot.com/feeds/5296137349854191729/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=1456844729588229023&amp;postID=5296137349854191729' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/1456844729588229023/posts/default/5296137349854191729'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/1456844729588229023/posts/default/5296137349854191729'/><link rel='alternate' type='text/html' href='http://zamanbakshifirst.blogspot.com/2006/11/c-i-am-sure-that-vptr-is-stored-as.html' title='C++: I am sure that vptr is stored as a first member of the object !'/><author><name>Zaman Bakshi</name><uri>http://www.blogger.com/profile/09870326614850200376</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></entry><entry><id>tag:blogger.com,1999:blog-1456844729588229023.post-1496056445395213096</id><published>2006-11-07T05:16:00.002+05:30</published><updated>2009-11-24T16:37:36.766+05:30</updated><category scheme='http://www.blogger.com/atom/ns#' term='Triviality'/><category scheme='http://www.blogger.com/atom/ns#' term='zaman bakshi'/><category scheme='http://www.blogger.com/atom/ns#' term='assignment operator'/><category scheme='http://www.blogger.com/atom/ns#' term='default constructor'/><category scheme='http://www.blogger.com/atom/ns#' term='copy constructor'/><category scheme='http://www.blogger.com/atom/ns#' term='C++'/><title type='text'>C++: I have heard that if I don't write one or more of default constructor, copy constructor, and copy assignment operator the compiler creates them.</title><content type='html'>Ha! They have taken you half-way through your voyage. This is incomplete information. The answer is &lt;span style="FONT-WEIGHT: bold"&gt;'NOT ALWAYS'!&lt;/span&gt; Surprised? Well then read on...&lt;br /&gt;&lt;br /&gt;&lt;span style="FONT-WEIGHT: bold"&gt;Eligibility:&lt;/span&gt; &lt;span style="FONT-STYLE: italic"&gt;You should be familiar with basic formulation of the C++ language. You can check for &lt;a href="http://www.esnips.com/web/MyComputerPapers"&gt;'&lt;/a&gt;&lt;a href="http://www.esnips.com/web/MyComputerPapers"&gt;The C++ Standard' here&lt;/a&gt;.&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="font-size:100%;"&gt;&lt;span style="COLOR: rgb(255,153,0);font-family:arial;" &gt;Triviality...&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;The C++ Standard states that the compiler should create the default .ctor, copy .ctor, and copy assignment operator if they are non-trivial. Now we need to consider the conditions in which the three are non-trivial. Let's consider them one by one.&lt;br /&gt;&lt;br /&gt;&lt;span style="COLOR: rgb(255,153,0);font-family:arial;" &gt;The Default Constructor&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;You should remember that if you provide other argument constructors but not the default constructor, then the default constructor is not prepared for you. The compiler assumes that this is the exact behavior you needed. The standard states that the default constructor is &lt;span style="FONT-WEIGHT: bold"&gt;non-trivial&lt;/span&gt; if:&lt;br /&gt;&lt;br /&gt;&lt;ol&gt;&lt;li&gt;Any of the member class objects has a default constructor.&lt;/li&gt;&lt;li&gt;Base class has a default constructor.&lt;/li&gt;&lt;li&gt;Class has virtual functions.&lt;/li&gt;&lt;li&gt;Class has virtual base class.&lt;/li&gt;&lt;/ol&gt;If any of these conditions is not met, then the compiler doesn't create a constructor for you. So, if we consider the following code&lt;br /&gt;&lt;br /&gt;&lt;span style="FONT-STYLE: italic; COLOR: rgb(255,0,0)"&gt;class&lt;/span&gt;&lt;span style="FONT-STYLE: italic"&gt; MyClass{&lt;br /&gt;&lt;/span&gt;&lt;span style="FONT-STYLE: italic; COLOR: rgb(255,0,0)"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;public&lt;/span&gt;&lt;span style="FONT-STYLE: italic"&gt;:&lt;/span&gt;&lt;br /&gt;&lt;span style="FONT-STYLE: italic; COLOR: rgb(255,0,0)"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;void&lt;/span&gt;&lt;span style="FONT-STYLE: italic"&gt;&amp;nbsp;SomeFunction( ) {//...}&lt;/span&gt;&lt;br /&gt;&lt;span style="FONT-STYLE: italic; COLOR: rgb(255,0,0)"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;private&lt;/span&gt;&lt;span style="FONT-STYLE: italic"&gt;:&lt;/span&gt;&lt;br /&gt;&lt;span style="FONT-STYLE: italic; COLOR: rgb(255,0,0)"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;int&lt;/span&gt;&lt;span style="FONT-STYLE: italic"&gt;* somedata;&lt;/span&gt;&lt;br /&gt;&lt;span style="FONT-STYLE: italic; COLOR: rgb(255,0,0)"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;int&lt;/span&gt;&lt;span style="FONT-STYLE: italic"&gt; _x, _y;&lt;br /&gt;&lt;/span&gt;&lt;span style="FONT-STYLE: italic"&gt;};&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;the default constructor is not generated and the members are left uninitialized. This may result in unwanted behavior. Here,&lt;span style="FONT-STYLE: italic"&gt; somedata&lt;/span&gt; may point to restricted memory and it's access may result in a program crash. So, it's best to define a default constructor, and should be made a habit.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="COLOR: rgb(255,153,0);font-family:arial;" &gt;The Copy Constructor&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;The Standard states that the copy constructor if not defined should be generated only if it will not be trivial. It goes on to add that the condition of being trivial is to exhibit bitwise copy semantics. Bitwise copy semantics mean bit-by-bit copy of an object. The standard states that a class &lt;span style="FONT-WEIGHT: bold"&gt;does not&lt;/span&gt; exhibit bitwise copy semantics, if:&lt;br /&gt;&lt;br /&gt;&lt;ol&gt;&lt;li&gt;When the class contains a member object of a class for which a copy constructor exists (either explicitly declared by the class designer, or synthesized by the compiler).&lt;/li&gt;&lt;li&gt;When the class is derived from a base class for which a copy constructor exists (again, either explicitly declared or synthesized).&lt;/li&gt;&lt;li&gt;When a class declares one or more virtual functions. &lt;span style="FONT-STYLE: italic"&gt;This is essential for proper initialization of &lt;/span&gt;&lt;a style="FONT-STYLE: italic" href="http://zamanbakshifirst.blogspot.com/2006/11/what-are-vptr-and-vtbl-in-c.html"&gt;vptr&lt;/a&gt;&lt;span style="FONT-STYLE: italic"&gt; if the parent object is initialized with an object of derived class.&lt;/span&gt;&lt;br /&gt;&lt;/li&gt;&lt;li&gt;When the class is derived from an inheritance chain in which one or more base classes are virtual. &lt;span style="FONT-STYLE: italic"&gt;This is essential to properly initialize the virtual class subobject if the parent object is initialized with an object of derived class. Failing to which would result in improper initialization of virtual base class pointer/offset within the &lt;a href="http://zamanbakshifirst.blogspot.com/2006/11/what-are-vptr-and-vtbl-in-c.html"&gt;vtbl&lt;/a&gt;.&lt;/span&gt;&lt;/li&gt;&lt;/ol&gt;So, the following class doesn't create a copy constructor for you. That is, there is no production of a function (and it's call). Rather, a code is inlined wherever needed.&lt;br /&gt;&lt;br /&gt;&lt;span style="FONT-STYLE: italic; COLOR: rgb(255,0,0)"&gt;class&lt;/span&gt;&lt;span style="FONT-STYLE: italic"&gt; MyClass {&lt;br /&gt;&lt;/span&gt;&lt;span style="FONT-STYLE: italic; COLOR: rgb(255,0,0)"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;public&lt;/span&gt;&lt;span style="FONT-STYLE: italic"&gt;:&lt;/span&gt;&lt;br /&gt;&lt;span style="FONT-STYLE: italic"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;MyClass( ):name(0), x(0){ }&lt;/span&gt;&lt;br /&gt;&lt;span style="FONT-STYLE: italic"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;MyClass (&lt;/span&gt;&lt;span style="FONT-STYLE: italic; COLOR: rgb(255,0,0)"&gt;const&lt;/span&gt;&lt;span style="FONT-STYLE: italic"&gt; char* );&lt;br /&gt;&lt;/span&gt;&lt;span style="FONT-STYLE: italic"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;~MyClass( ) {&lt;/span&gt;&lt;br /&gt;&lt;span style="FONT-STYLE: italic; COLOR: rgb(255,0,0)"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;delete&lt;/span&gt;&lt;span style="FONT-STYLE: italic"&gt;[] name ; &lt;/span&gt;&lt;br /&gt;&lt;span style="FONT-STYLE: italic"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;}&lt;/span&gt;&lt;br /&gt;&lt;span style="FONT-STYLE: italic; COLOR: rgb(255,0,0)"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;void&lt;/span&gt;&lt;span style="FONT-STYLE: italic"&gt; ShowMe( ) { //... }&lt;br /&gt;&lt;/span&gt;&lt;span style="FONT-STYLE: italic; COLOR: rgb(255,0,0)"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;private&lt;/span&gt;&lt;span style="FONT-STYLE: italic"&gt;:&lt;br /&gt;&lt;/span&gt;&lt;span style="FONT-STYLE: italic; COLOR: rgb(255,0,0)"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;char&lt;/span&gt;&lt;span style="FONT-STYLE: italic"&gt; *name;&lt;/span&gt;&lt;br /&gt;&lt;span style="FONT-STYLE: italic; COLOR: rgb(255,0,0)"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;int&lt;/span&gt;&lt;span style="FONT-STYLE: italic"&gt; x;&lt;/span&gt;&lt;br /&gt;&lt;span style="FONT-STYLE: italic"&gt;};&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;Now if we write&lt;br /&gt;&lt;br /&gt;&lt;span style="FONT-STYLE: italic"&gt;MyClass M;&lt;br /&gt;&lt;/span&gt;&lt;span style="FONT-STYLE: italic"&gt;MyClass M2 =M; //invoke inlining&lt;br /&gt;&lt;/span&gt;&lt;span style="FONT-STYLE: italic"&gt;someFunction(M2); //invoke inlining&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;or&lt;br /&gt;&lt;br /&gt;&lt;span style="FONT-STYLE: italic; COLOR: rgb(255,0,0)"&gt;void&lt;/span&gt;&lt;span style="FONT-STYLE: italic"&gt; someOtherFunction() {&lt;/span&gt;&lt;br /&gt;&lt;span style="FONT-STYLE: italic"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;MyClass M;&lt;/span&gt;&lt;br /&gt;&lt;span style="FONT-STYLE: italic; COLOR: rgb(255,0,0)"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;return&lt;/span&gt;&lt;span style="FONT-STYLE: italic"&gt; M;//invoke inlining&lt;/span&gt;&lt;br /&gt;&lt;span style="FONT-STYLE: italic"&gt;}&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;There is no functional call, but, the code for bit to bit copy of members is inlined. That is,&lt;br /&gt;&lt;br /&gt;&lt;span style="FONT-STYLE: italic"&gt;M2.name = M.name;&lt;/span&gt;&lt;br /&gt;&lt;span style="FONT-STYLE: italic"&gt;M2.x=M.x;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;This will lead to both the &lt;span style="FONT-STYLE: italic; FONT-WEIGHT: bold"&gt;name&lt;/span&gt;s pointing to the same location. So, if any object changes or deletes (while exiting the scope) it's &lt;span style="FONT-WEIGHT: bold"&gt;name&lt;/span&gt;, it will result in a dangling pointer in other object as the memory pointer but it's &lt;span style="FONT-WEIGHT: bold"&gt;name&lt;/span&gt; would have been invalidated.&lt;br /&gt;&lt;br /&gt;So, it is advised to always declare a copy constructor. But if you are smart and know that it won't lead to bugs, you can give it a miss. This will increase program speed as there won't be any functional jumps.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="COLOR: rgb(255,153,0);font-family:arial;" &gt;Copy Assignment Operator&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;The condition for synthesizing copy assignment operator is the same as that of the copy constructor -- that it is should not exhibit the bitwise copy semantics, i.e should be non-trivial. The conditions that a copy assignment operator &lt;span style="FONT-WEIGHT: bold"&gt;does not&lt;/span&gt; show bitwise copy semantics are:&lt;br /&gt;&lt;br /&gt;&lt;ol&gt;&lt;li&gt;When the class contains a member object of a class for which a copy assignment operator exists (either explicitly declared by the class designer, or synthesized by the compiler).&lt;/li&gt;&lt;li&gt;When the class is derived from a base class for which a copy assignment operator exists (again, either explicitly declared or synthesized).&lt;/li&gt;&lt;li&gt;When a class declares one or more virtual functions. &lt;span style="FONT-STYLE: italic"&gt;This is essential for proper initialization of &lt;/span&gt;&lt;a style="FONT-STYLE: italic" href="http://zamanbakshifirst.blogspot.com/2006/11/what-are-vptr-and-vtbl-in-c.html"&gt;vptr&lt;/a&gt;&lt;span style="FONT-STYLE: italic"&gt; if the parent object is initialized with an object of derived class.&lt;/span&gt;&lt;br /&gt;&lt;/li&gt;&lt;li&gt;When the class is derived from an inheritance chain in which one or more base classes are virtual. &lt;span style="FONT-STYLE: italic"&gt;This is essential to properly initialize the virtual class subobject if the parent object is initialized with an object of derived class. Which would result in improper initialization of virtual base class pointer/offset within the &lt;a href="http://zamanbakshifirst.blogspot.com/2006/11/what-are-vptr-and-vtbl-in-c.html"&gt;vtbl&lt;/a&gt;.&lt;/span&gt;&lt;/li&gt;&lt;/ol&gt;&lt;br /&gt;So, if the operator is trivial, it results in inlining of code, rather than resulting in function calls. This increases the speed. And as the assignment operator is used widely in a program, it is best advised not to declare one unless is necessary, like when the bitwise copy can result in dangling pointers. If you are not sure about the behavior then you should go ahead and declare one anyhow.&lt;br /&gt;&lt;br /&gt;Now you know the full information. So &lt;span class="me"&gt;bon vo‧yage o reader.&lt;br /&gt;&lt;/span&gt;&lt;span class="me"&gt;&lt;/span&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/1456844729588229023-1496056445395213096?l=zamanbakshifirst.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://zamanbakshifirst.blogspot.com/feeds/1496056445395213096/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=1456844729588229023&amp;postID=1496056445395213096' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/1456844729588229023/posts/default/1496056445395213096'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/1456844729588229023/posts/default/1496056445395213096'/><link rel='alternate' type='text/html' href='http://zamanbakshifirst.blogspot.com/2006/11/c-i-have-heard-that-if-i-dont-write-one.html' title='C++: I have heard that if I don&apos;t write one or more of default constructor, copy constructor, and copy assignment operator the compiler creates them.'/><author><name>Zaman Bakshi</name><uri>http://www.blogger.com/profile/09870326614850200376</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></entry><entry><id>tag:blogger.com,1999:blog-1456844729588229023.post-103332627905492106</id><published>2006-11-07T02:24:00.001+05:30</published><updated>2009-11-24T16:35:34.742+05:30</updated><category scheme='http://www.blogger.com/atom/ns#' term='vtbl'/><category scheme='http://www.blogger.com/atom/ns#' term='zaman'/><category scheme='http://www.blogger.com/atom/ns#' term='vptr'/><category scheme='http://www.blogger.com/atom/ns#' term='C++'/><title type='text'>C++: What are vptr and vtbl in C++ ?</title><content type='html'>&lt;div style="text-align: justify;"&gt;Well well, now this is one of the most asked questions of all times in C++. Let me be clear first. These two data structures are not mentioned in '&lt;a href="http://www.esnips.com/web/MyComputerPapers"&gt;The C++ Standard'&lt;/a&gt;. Rather, are implementation details of C++. So, are not part of the standard C++.&lt;br /&gt;&lt;br /&gt;&lt;span style="font-weight: bold;"&gt;Eligibility&lt;/span&gt;: &lt;span style="font-style: italic;"&gt;you should have a clear understanding of polymorphism with C++.&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 153, 0);font-size:100%;" &gt;&lt;span style="font-family:arial;"&gt;Why do we need them?&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;The question that arises is 'Why do we need them?' The answer is simple. To support polymorphism and RTTI. Let's look at a piece of code in C++.&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 0, 0); font-style: italic;"&gt;class&lt;/span&gt;&lt;span style="font-style: italic;"&gt; Parent {&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 0, 0); font-style: italic;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;public&lt;/span&gt;&lt;span style="font-style: italic;"&gt;:&lt;/span&gt;&lt;br /&gt;&lt;span style="font-style: italic;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;Parent():data(0) { }&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 0, 0); font-style: italic;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;void&lt;/span&gt;&lt;span style="font-style: italic;"&gt; doSomeThing ( ) {&lt;/span&gt;&lt;br /&gt;&lt;span style="font-style: italic;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;//.......&lt;/span&gt;&lt;br /&gt;&lt;span style="font-style: italic;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;showData( ) ; //equivalent to &lt;span style="color: rgb(255, 0, 0);"&gt;this&lt;/span&gt;-&gt;showData( );&lt;/span&gt;&lt;br /&gt;&lt;span style="font-style: italic;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;}&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 0, 0); font-style: italic;"&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;&lt;span style="color: rgb(255, 0, 0);"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;virtual&lt;/span&gt; &lt;span style="color: rgb(255, 0, 0);"&gt;void&lt;/span&gt; someFun&lt;/span&gt;&lt;/span&gt;&lt;span style="color: rgb(255, 0, 0); font-style: italic;"&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;ction( ) {&lt;/span&gt; &lt;span style="color: rgb(0, 0, 0);"&gt;//......&lt;/span&gt; &lt;span style="color: rgb(0, 0, 0);"&gt;}&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 0, 0); font-style: italic;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;private&lt;/span&gt;&lt;span style="font-style: italic;"&gt;:&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 0, 0); font-style: italic;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;int&lt;/span&gt;&lt;span style="font-style: italic;"&gt; data;&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 0, 0); font-style: italic;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;virtual&lt;/span&gt;&lt;span style="font-style: italic;"&gt; &lt;/span&gt;&lt;span style="color: rgb(255, 0, 0); font-style: italic;"&gt;void&lt;/span&gt;&lt;span style="font-style: italic;"&gt; showData ( ) {&lt;/span&gt;&lt;br /&gt;&lt;span style="font-style: italic;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;cout &lt;&lt;  data ;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;}&lt;br /&gt;&lt;span style="font-style: italic;"&gt;};  &lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="font-style: italic;"&gt;&lt;span style="color: rgb(255, 0, 0); font-style: italic;"&gt;class&lt;/span&gt;&lt;span style="font-style: italic;"&gt; Child : &lt;/span&gt;&lt;span style="color: rgb(255, 0, 0); font-style: italic;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;public &lt;/span&gt;&lt;span style="font-style: italic;"&gt;Parent {&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="font-style: italic;"&gt;   &lt;span style="color: rgb(255, 0, 0); font-style: italic;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;public&lt;/span&gt;&lt;span style="font-style: italic;"&gt;:&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="font-style: italic;"&gt;&lt;span style="font-style: italic;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;Child():data(10) { }&lt;/span&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="font-style: italic;"&gt;   &lt;span style="color: rgb(255, 0, 0); font-style: italic;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;private&lt;/span&gt;&lt;span style="font-style: italic;"&gt;:&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="font-style: italic;"&gt;   &lt;span style="color: rgb(255, 0, 0); font-style: italic;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;int&lt;/span&gt;&lt;span style="font-style: italic;"&gt; data;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="font-style: italic;"&gt;   &lt;span style="color: rgb(255, 0, 0); font-style: italic;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;void&lt;/span&gt;&lt;span style="font-style: italic;"&gt; showData ( ) {&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="font-style: italic;"&gt;&lt;span style="font-style: italic;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;cout &lt;&lt;  data  ;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;}&lt;br /&gt;&lt;/span&gt;&lt;span style="font-style: italic;"&gt;&lt;span style="font-style: italic;"&gt;&lt;span style="font-style: italic;"&gt;};     &lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="font-style: italic;"&gt;&lt;span style="font-style: italic;"&gt;&lt;span style="color: rgb(255, 0, 0); font-style: italic;"&gt;int&lt;/span&gt;&lt;span style="font-style: italic;"&gt; main ( ) {&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="font-style: italic;"&gt;&lt;span style="font-style: italic;"&gt;&lt;span style="font-style: italic;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;Parent *p = 0;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="font-style: italic;"&gt;&lt;span style="font-style: italic;"&gt;&lt;span style="font-style: italic;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;p = &lt;/span&gt;&lt;span style="color: rgb(255, 0, 0); font-style: italic;"&gt;new&lt;/span&gt;&lt;span style="font-style: italic;"&gt; Child ( ); // p points to Child Now&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="font-style: italic;"&gt;&lt;span style="font-style: italic;"&gt;&lt;span style="font-style: italic;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;p -&gt; doSomeThing ( ); //gives 10&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="font-style: italic;"&gt;&lt;span style="font-style: italic;"&gt;   &lt;span style="color: rgb(255, 0, 0); font-style: italic;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;return&lt;/span&gt;&lt;span style="font-style: italic;"&gt; 0;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="font-style: italic;"&gt;&lt;span style="font-style: italic;"&gt;&lt;span style="font-style: italic;"&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="font-size:100%;"&gt;&lt;span style="font-family:arial;"&gt;&lt;span style="color: rgb(255, 153, 0);"&gt;How to produced intermediate-code capable of handling polymorphic behavior?&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;span&gt;&lt;span&gt;The crux of the problem for compiler, is that, just like main ( ) as shown here, many other functions in a large program would call doSomeThing( ) thr&lt;/span&gt;&lt;/span&gt;&lt;span&gt;&lt;span&gt;ough parent class object. This object may actually (as in this case) point to the derived class object. Now, the compiler has to produce an intermediate code that could decide which showData( ) to call whenever doSomeThing( ) is called through a Parent class pointer. If the pointer points to Parent object, cout should output 0, else if the pointer points to the Child object it should output 10.&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span&gt;&lt;span&gt;To make the decision, it employs the technique of vptr (virtual pointer) and vtbl (virtual table). Every class object that exhibits polymorphic behavior (has virtual function) embeds a pointer. This pointer is vptr.&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="font-size:100%;"&gt;&lt;span style="font-family:arial;"&gt;&lt;span style="color: rgb(255, 153, 0);"&gt;What does vptr point to?&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;span&gt;&lt;span&gt;Now the question arises, 'what does this pointer (vptr) point to?'. To answer this question we need to understand where the member functions are stored. Every member function of a class is stored statically with a mangled name. Say, doSomeThing( ) would be stored as __Parent__d001( ), as would be the two virtual functions __Parent__showData001( ) and __Child_showData001( ). So, the code like&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="font-style: italic;"&gt;&lt;span style="font-style: italic;"&gt;&lt;span style="font-style: italic;"&gt;Parent p = &lt;span style="color: rgb(255, 0, 0);"&gt;new&lt;/span&gt; Parent ( );&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="font-style: italic;"&gt;&lt;span style="font-style: italic;"&gt;&lt;span style="font-style: italic;"&gt;p.doSomeThing( );&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span&gt;&lt;span&gt;is changed to&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="font-style: italic;"&gt;&lt;span style="font-style: italic;"&gt;//...&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="font-style: italic;"&gt;&lt;span style="font-style: italic;"&gt;&lt;span style="font-style: italic;"&gt;__Parent__d001(p);&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span&gt;&lt;span&gt;which means, call __Parent__d001( ) through p. Now, let's see what are the effects of the keyword 'virtual' before a function declaration. A derived class declaration can:&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;ol style="text-align: justify;"&gt;&lt;li&gt;Override the virtual function.&lt;/li&gt;&lt;li&gt;Not override the virtual function.&lt;/li&gt;&lt;/ol&gt;To incorporate both the situations, a compiler creates a data structure called vtbl (for virtual table). Every class has one or more vptrs and vtbls, but are in equal numbers. In simple scenario (as with above code) we will have one vptr and one vtbl, and would consider it for explanation. The vtbl can be considered having first member as the 'type_info' of the class, and the rest members (variable numbers -- but at least one) are pointers to virtual functions of the class object. The structure o&lt;a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="http://photos1.blogger.com/blogger2/927/177837491404813/1600/vtbl.jpg"&gt;&lt;img style="margin: 0pt 10px 10px 0pt; float: left; cursor: pointer;" src="http://photos1.blogger.com/blogger2/927/177837491404813/400/vtbl.jpg" alt="" border="0" /&gt;&lt;/a&gt;f vtbl is given below:&lt;br /&gt;&lt;div style="text-align: justify;"&gt;&lt;/div&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;div style="text-align: left;"&gt;When a derived class decides not to override the virtual function the address of Parent's virtual function is added to the vtbl. But if, the derived class decides to override the function, the address of the overriding function replaces the old address. Thus, yielding the figure shown on the left-hand side. The positions of the functions remains fixed within the hierarchy of vtbls. Here, &amp;__XXX__showData001( ) will always be the first entry within the vtbls.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="font-size:100%;"&gt;&lt;span style="font-family:arial;"&gt;&lt;span style="color: rgb(255, 153, 0);"&gt;Where in object is vptr stored?&lt;br /&gt;&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;Now, the second question to be answered in our journey to find out what does vptr point to, is, 'Where in the object structure is the vptr stored?'. The answer is anywhere you would like to. Most standard compilers choose to embed vptr as the last member (to have C language compatibility), whereas others like Microsoft compilers make it as the first member of the object structure. So, considering the former case, we will reach to the following object layout:&lt;br /&gt;&lt;br /&gt;&lt;a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="http://photos1.blogger.com/blogger2/927/177837491404813/1600/objects.jpg"&gt;&lt;img style="margin: 0pt 10px 10px 0pt; float: left; cursor: pointer;" src="http://photos1.blogger.com/blogger2/927/177837491404813/400/objects.jpg" alt="" border="0" /&gt;&lt;/a&gt;Now, we are in a position to answer the question. The vptr of the object points to the respective vtbl.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="font-size:100%;"&gt;&lt;span style="font-family:arial;"&gt;&lt;span style="color: rgb(255, 153, 0);"&gt;&lt;br /&gt;Rewriting the intermediate code...&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;So the polymorphic code:&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 0, 0); font-style: italic;"&gt;    void&lt;/span&gt;&lt;span style="font-style: italic;"&gt; doSomeThing ( ) {&lt;/span&gt;&lt;br /&gt;&lt;span style="font-style: italic;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;//.......&lt;/span&gt;&lt;br /&gt;&lt;span style="font-style: italic;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;showData( ) ;&lt;/span&gt;&lt;br /&gt;&lt;span style="font-style: italic;"&gt;    }&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;can be written as&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 0, 0); font-style: italic;"&gt;&lt;br /&gt;void&lt;/span&gt;&lt;span style="font-style: italic;"&gt; doSomeThing ( ) {&lt;/span&gt;&lt;br /&gt;&lt;span style="font-style: italic;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;//.......&lt;br /&gt;&lt;span style="color: rgb(255, 0, 0);"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;this&lt;/span&gt;-&gt;vptr[1](&lt;span style="color: rgb(255, 0, 0);"&gt;this&lt;/span&gt;);&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;//[1] is the second entry of vtbl, which is showData( )&lt;/span&gt;&lt;br /&gt;&lt;span style="font-style: italic;"&gt;    }&lt;br /&gt;&lt;br /&gt;&lt;/span&gt;&lt;span&gt;Now depending upon the type of 'this', i.e. the type of object that called doSomeThing( ), it's respective virtual function would be called.&lt;br /&gt;&lt;br /&gt;Hurrah! That solves the problem of embellishing polymorphic intermediate code properly. In the same way the type_info for every object can be checked to see if it can be properly cast into any other type. Thus enabling Runtime Type Information (RTTI).&lt;/span&gt;&lt;/div&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/1456844729588229023-103332627905492106?l=zamanbakshifirst.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://zamanbakshifirst.blogspot.com/feeds/103332627905492106/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=1456844729588229023&amp;postID=103332627905492106' title='3 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/1456844729588229023/posts/default/103332627905492106'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/1456844729588229023/posts/default/103332627905492106'/><link rel='alternate' type='text/html' href='http://zamanbakshifirst.blogspot.com/2006/11/what-are-vptr-and-vtbl-in-c.html' title='C++: What are vptr and vtbl in C++ ?'/><author><name>Zaman Bakshi</name><uri>http://www.blogger.com/profile/09870326614850200376</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>3</thr:total></entry></feed>
