<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
		>
<channel>
	<title>Comments for ScapeCode</title>
	<atom:link href="http://www.scapecode.com/comments/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.scapecode.com</link>
	<description>Anime, tentacles, and software development</description>
	<lastBuildDate>Thu, 19 May 2011 05:38:31 +0000</lastBuildDate>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
	<item>
		<title>Comment on C++ Quiz #2 by Washu</title>
		<link>http://www.scapecode.com/2011/05/c-quiz-2/#comment-5</link>
		<dc:creator>Washu</dc:creator>
		<pubDate>Thu, 19 May 2011 05:38:31 +0000</pubDate>
		<guid isPermaLink="false">https://scapecode.com/?p=332#comment-5</guid>
		<description>[spoiler]Ok, so first up is question 1.
1.1) typeid(*a); is well formed. It will return the typeid of SubBase2.
1.2) dynamic_cast&lt;SubBase2*&gt;(a); is well formed as well, as a is of type Base* and SubBase2 is derived from Base. Results in a SubBase2*.
1.3) typeid(*b); is malformed and results in undefined behavior. Since we are in the constructor of SubBase2, type SubBase1 is not a base of SubType2.
1.4) dynamic_cast&lt;SubBase2*&gt;(b); is malformed and results in undefined behavior. Since we are in the constructor of SubBase2, type SubBase1 is not a base of SubType2
1.5) a-&gt;Arr(); Well formed, calls SubBase2::Arr().
1.6) b-&gt;Arr(); Malformed, undefined behavior. SubBase1 is not a base of SubBase2.

This example was almost an exact copy of two similar examples from the standard, in section 12.7. When typeid or dynamic_cast is used in a constructor or in a destructor, or in a function called from either, and if the operator of typeid/dynamic_cast refers to the object under construction or destruction, and the static type of the operand is neither the constructor nor the destructors class, nor one of its bases, then the behavior is undefined.

Question 2:
2.1) X&lt;T&gt;* p; Is well formed, pointers to incomplete types are allowed.
2.2) X&lt;T*&gt; a; Is malformed, this results in infinite recursion and hence the result is undefined behavior.

This one was an exact copy of the standard, from section: 14.7.1 -14.[/spoiler] </description>
		<content:encoded><![CDATA[<div class='easySpoilerWrapper' style=''>
<table class='easySpoilerTable' border='0' style='text-align:center;' align='center' bgcolor='FFFFFF' >
<tr style='white-space:normal;'>
<th class='easySpoilerTitleA'  style='white-space:normal;font-weight:normal;text-align:left;vertical-align:middle;font-size:120%;color:#000000;'>Spoiler Inside</th>
<th class='easySpoilerTitleB'  style='text-align:right;vertical-align:middle;font-size:100%; white-space:nowrap;'>
<a href='' onclick='wpSpoilerSelect("spoilerDiv28a58001"); return false;' class='easySpoilerButtonOther' style='font-size:100%;color:#000000;background-color:#fcfcfc;background-image:none;border: 1px inset;border-style:solid;border-color:#cccccc;  margin: 3px 0px 3px; padding: 4px; ' align='right'>Select</a><a href='' onclick='wpSpoilerToggle("spoilerDiv28a58001",true,"Show","Hide","fast",false); return false;' id='spoilerDiv28a58001_action' class='easySpoilerButton' value="Show" align='right' style='font-size:100%;color:#000000;background-color:#fcfcfc;background-image:none;border: 1px inset;border-style:solid;border-color:#cccccc; margin: 3px 0px 3px 5px; padding: 4px;"'>Show</a></th>
</tr>
<tr>
<td class='easySpoilerRow' colspan='2' style=''>
<div id='spoilerDiv28a58001' class='easySpoilerSpoils'  style='display:none; white-space:wrap; overflow:auto; vertical-align:middle;'>
Ok, so first up is question 1.<br />
1.1) typeid(*a); is well formed. It will return the typeid of SubBase2.<br />
1.2) dynamic_cast<subbase2 *>(a); is well formed as well, as a is of type Base* and SubBase2 is derived from Base. Results in a SubBase2*.<br />
1.3) typeid(*b); is malformed and results in undefined behavior. Since we are in the constructor of SubBase2, type SubBase1 is not a base of SubType2.<br />
1.4) dynamic_cast</subbase2><subbase2 *>(b); is malformed and results in undefined behavior. Since we are in the constructor of SubBase2, type SubBase1 is not a base of SubType2<br />
1.5) a->Arr(); Well formed, calls SubBase2::Arr().<br />
1.6) b->Arr(); Malformed, undefined behavior. SubBase1 is not a base of SubBase2.</p>
<p>This example was almost an exact copy of two similar examples from the standard, in section 12.7. When typeid or dynamic_cast is used in a constructor or in a destructor, or in a function called from either, and if the operator of typeid/dynamic_cast refers to the object under construction or destruction, and the static type of the operand is neither the constructor nor the destructors class, nor one of its bases, then the behavior is undefined.</p>
<p>Question 2:<br />
2.1) X<t>* p; Is well formed, pointers to incomplete types are allowed.<br />
2.2) X</t><t *> a; Is malformed, this results in infinite recursion and hence the result is undefined behavior.</p>
<p>This one was an exact copy of the standard, from section: 14.7.1 -14.<br />
</t></subbase2></div>
</td>
</tr>
</table>
<div class='easySpoilerConclude' style=''>
<table class='easySpoilerTable' border='0' style='text-align:center;' frame='box' align='center' bgcolor='FFFFFF'>
<tr>
<th class='easySpoilerEnd' style='width:100%;'></th>
<td class='easySpoilerEnd' style='white-space:nowrap;' colspan='2'></td>
</tr>
<tr>
<td class='easySpoilerGroupWrapperLastRow' colspan='2' style=''></td>
</tr>
</table>
</div>
</div>
]]></content:encoded>
	</item>
	<item>
		<title>Comment on C++ Quiz #3 by Washu</title>
		<link>http://www.scapecode.com/2011/05/c-quiz-3/#comment-4</link>
		<dc:creator>Washu</dc:creator>
		<pubDate>Thu, 19 May 2011 05:34:30 +0000</pubDate>
		<guid isPermaLink="false">https://scapecode.com/?p=323#comment-4</guid>
		<description>[spoiler]1. Does the first numbered line result in defined behavior? 

Yes 

2. What should the first numbered line do? 

It should reuse the storage occupied by *this. 

3. Is the behavior of the second and third numbered lines well defined? 

Yes and no - the third line potentially writes to unallocated memory, depending on the size of Derived. If it does not, it is well defined. 

4. Is the fourth numbered line valid? If so, why? If not, why? 

It is syntactically valid and semantically valid as per 3.8.5. Ignoring any undefined behaviour which resulted from previous lines, that is. 

5. Is the fifth numbered line valid? If so, why? If not, why? 

It is syntactically valid, but results in undefined behaviour as the lifetime of *p has ended. 


2.What is the behavior of calling void exit(int);? 

The exit function from cstdlib exits the program without unwinding the stack or completing execution of its current block. It has undefined behaviour if you call the function when destroying an object with static storage duration. 

3.1 Does the first numbered line result in defined behavior? 

Yes. It is well defined as per 3.7.3.8. 

3.2 Is the behavior of the second line defined? If so, why? If not, why is the behavior not defined? 

It is not defined as you must ensure that an object of the same type occupies the storage when the implicit call to the objects destructor takes place. This applies to objects with static and automatic storage duration (objects which have an implicit destructor call). 

4. What is the behavior of int&amp; p = *(int*)0;? Why does it have that particular behavior? Is this a null reference? 

This results in undefined behaviour, as it is derefencing a null pointer. 

5. What is the behavior of the numbered line in the following code if I is defined as typedef int I;? 

It ends the lifetime of the int object, as per 12.4.15.[/spoiler] </description>
		<content:encoded><![CDATA[<div class='easySpoilerWrapper' style=''>
<table class='easySpoilerTable' border='0' style='text-align:center;' align='center' bgcolor='FFFFFF' >
<tr style='white-space:normal;'>
<th class='easySpoilerTitleA'  style='white-space:normal;font-weight:normal;text-align:left;vertical-align:middle;font-size:120%;color:#000000;'>Spoiler Inside</th>
<th class='easySpoilerTitleB'  style='text-align:right;vertical-align:middle;font-size:100%; white-space:nowrap;'>
<a href='' onclick='wpSpoilerSelect("spoilerDiv714b8002"); return false;' class='easySpoilerButtonOther' style='font-size:100%;color:#000000;background-color:#fcfcfc;background-image:none;border: 1px inset;border-style:solid;border-color:#cccccc;  margin: 3px 0px 3px; padding: 4px; ' align='right'>Select</a><a href='' onclick='wpSpoilerToggle("spoilerDiv714b8002",true,"Show","Hide","fast",false); return false;' id='spoilerDiv714b8002_action' class='easySpoilerButton' value="Show" align='right' style='font-size:100%;color:#000000;background-color:#fcfcfc;background-image:none;border: 1px inset;border-style:solid;border-color:#cccccc; margin: 3px 0px 3px 5px; padding: 4px;"'>Show</a></th>
</tr>
<tr>
<td class='easySpoilerRow' colspan='2' style=''>
<div id='spoilerDiv714b8002' class='easySpoilerSpoils'  style='display:none; white-space:wrap; overflow:auto; vertical-align:middle;'>
1. Does the first numbered line result in defined behavior? </p>
<p>Yes </p>
<p>2. What should the first numbered line do? </p>
<p>It should reuse the storage occupied by *this. </p>
<p>3. Is the behavior of the second and third numbered lines well defined? </p>
<p>Yes and no &#8211; the third line potentially writes to unallocated memory, depending on the size of Derived. If it does not, it is well defined. </p>
<p>4. Is the fourth numbered line valid? If so, why? If not, why? </p>
<p>It is syntactically valid and semantically valid as per 3.8.5. Ignoring any undefined behaviour which resulted from previous lines, that is. </p>
<p>5. Is the fifth numbered line valid? If so, why? If not, why? </p>
<p>It is syntactically valid, but results in undefined behaviour as the lifetime of *p has ended. </p>
<p>2.What is the behavior of calling void exit(int);? </p>
<p>The exit function from cstdlib exits the program without unwinding the stack or completing execution of its current block. It has undefined behaviour if you call the function when destroying an object with static storage duration. </p>
<p>3.1 Does the first numbered line result in defined behavior? </p>
<p>Yes. It is well defined as per 3.7.3.8. </p>
<p>3.2 Is the behavior of the second line defined? If so, why? If not, why is the behavior not defined? </p>
<p>It is not defined as you must ensure that an object of the same type occupies the storage when the implicit call to the objects destructor takes place. This applies to objects with static and automatic storage duration (objects which have an implicit destructor call). </p>
<p>4. What is the behavior of int&#038; p = *(int*)0;? Why does it have that particular behavior? Is this a null reference? </p>
<p>This results in undefined behaviour, as it is derefencing a null pointer. </p>
<p>5. What is the behavior of the numbered line in the following code if I is defined as typedef int I;? </p>
<p>It ends the lifetime of the int object, as per 12.4.15.
</p></div>
</td>
</tr>
</table>
<div class='easySpoilerConclude' style=''>
<table class='easySpoilerTable' border='0' style='text-align:center;' frame='box' align='center' bgcolor='FFFFFF'>
<tr>
<th class='easySpoilerEnd' style='width:100%;'></th>
<td class='easySpoilerEnd' style='white-space:nowrap;' colspan='2'></td>
</tr>
<tr>
<td class='easySpoilerGroupWrapperLastRow' colspan='2' style=''></td>
</tr>
</table>
</div>
</div>
]]></content:encoded>
	</item>
	<item>
		<title>Comment on C++ Quiz #4 by Washu</title>
		<link>http://www.scapecode.com/2011/05/c-quiz-4/#comment-3</link>
		<dc:creator>Washu</dc:creator>
		<pubDate>Thu, 19 May 2011 05:19:18 +0000</pubDate>
		<guid isPermaLink="false">https://scapecode.com/?p=310#comment-3</guid>
		<description>[spoiler]Quiz answers:
During the construction of a const object, if the value of the object or any of its sub objects are accessed through an lvalue that is not obtained, either directly or indirectly, from the constructor’s this pointer, the value of the object or sub object thus obtained is unspecified. As such the answers to questions 1, 2 and 3 are:
1.	i will contain an unspecified value as obj.c results in an unspecified value.
2.	It will print out 1.  
3.	After the evaluation of the third numbered line, p-&gt;c will contain the unspecified value that was in i.
4.	The unspecified value in obj.c will be printed.

At most only a single user defined conversion (constructor or conversion function) is implicitly applied to a single value. As such the answers for questions 4 and 5 are:
5.	This should result in a compile time error.
6.	The type is explicitly converted to type X using a conversion constructor, and operator int() will be called on that X type, resulting in the int containing the integral value returned by X::operator int()

An explicit constructor constructs objects only where the direct-initialization syntax or casts are explicitly used. As such the answer for question 6 is:
7.	The first numbered line will result in an error, the second numbered line will result in z2 being assigned the value in an unnamed temporary constructed during the static cast.

A typedef-name that names a class shall not be used as the identifier in the declarator for a destructor declaration. As such the answer to question 7 is:
8.	The results of the calls on the numbered lines, irrespective of the other lines are.
a.	Calls Base::~Base()
b.	Calls Derived::~Derived()
c.	Calls Derived::~Derived()
d.	Calls Base::~Base()
e.	This line should result in an error as this is clearly a decleration of a destructor (used in a function call syntax). As such the usage of the typedef is illegal. However it has been noted that many compilers will accept this syntax as legal. That does not mean that you should do it, nor expect it to work correctly.[/spoiler] </description>
		<content:encoded><![CDATA[<div class='easySpoilerWrapper' style=''>
<table class='easySpoilerTable' border='0' style='text-align:center;' align='center' bgcolor='FFFFFF' >
<tr style='white-space:normal;'>
<th class='easySpoilerTitleA'  style='white-space:normal;font-weight:normal;text-align:left;vertical-align:middle;font-size:120%;color:#000000;'>Spoiler Inside</th>
<th class='easySpoilerTitleB'  style='text-align:right;vertical-align:middle;font-size:100%; white-space:nowrap;'>
<a href='' onclick='wpSpoilerSelect("spoilerDiv4e908003"); return false;' class='easySpoilerButtonOther' style='font-size:100%;color:#000000;background-color:#fcfcfc;background-image:none;border: 1px inset;border-style:solid;border-color:#cccccc;  margin: 3px 0px 3px; padding: 4px; ' align='right'>Select</a><a href='' onclick='wpSpoilerToggle("spoilerDiv4e908003",true,"Show","Hide","fast",false); return false;' id='spoilerDiv4e908003_action' class='easySpoilerButton' value="Show" align='right' style='font-size:100%;color:#000000;background-color:#fcfcfc;background-image:none;border: 1px inset;border-style:solid;border-color:#cccccc; margin: 3px 0px 3px 5px; padding: 4px;"'>Show</a></th>
</tr>
<tr>
<td class='easySpoilerRow' colspan='2' style=''>
<div id='spoilerDiv4e908003' class='easySpoilerSpoils'  style='display:none; white-space:wrap; overflow:auto; vertical-align:middle;'>
Quiz answers:<br />
During the construction of a const object, if the value of the object or any of its sub objects are accessed through an lvalue that is not obtained, either directly or indirectly, from the constructor’s this pointer, the value of the object or sub object thus obtained is unspecified. As such the answers to questions 1, 2 and 3 are:<br />
1.	i will contain an unspecified value as obj.c results in an unspecified value.<br />
2.	It will print out 1.<br />
3.	After the evaluation of the third numbered line, p->c will contain the unspecified value that was in i.<br />
4.	The unspecified value in obj.c will be printed.</p>
<p>At most only a single user defined conversion (constructor or conversion function) is implicitly applied to a single value. As such the answers for questions 4 and 5 are:<br />
5.	This should result in a compile time error.<br />
6.	The type is explicitly converted to type X using a conversion constructor, and operator int() will be called on that X type, resulting in the int containing the integral value returned by X::operator int()</p>
<p>An explicit constructor constructs objects only where the direct-initialization syntax or casts are explicitly used. As such the answer for question 6 is:<br />
7.	The first numbered line will result in an error, the second numbered line will result in z2 being assigned the value in an unnamed temporary constructed during the static cast.</p>
<p>A typedef-name that names a class shall not be used as the identifier in the declarator for a destructor declaration. As such the answer to question 7 is:<br />
8.	The results of the calls on the numbered lines, irrespective of the other lines are.<br />
a.	Calls Base::~Base()<br />
b.	Calls Derived::~Derived()<br />
c.	Calls Derived::~Derived()<br />
d.	Calls Base::~Base()<br />
e.	This line should result in an error as this is clearly a decleration of a destructor (used in a function call syntax). As such the usage of the typedef is illegal. However it has been noted that many compilers will accept this syntax as legal. That does not mean that you should do it, nor expect it to work correctly.
</p></div>
</td>
</tr>
</table>
<div class='easySpoilerConclude' style=''>
<table class='easySpoilerTable' border='0' style='text-align:center;' frame='box' align='center' bgcolor='FFFFFF'>
<tr>
<th class='easySpoilerEnd' style='width:100%;'></th>
<td class='easySpoilerEnd' style='white-space:nowrap;' colspan='2'></td>
</tr>
<tr>
<td class='easySpoilerGroupWrapperLastRow' colspan='2' style=''></td>
</tr>
</table>
</div>
</div>
]]></content:encoded>
	</item>
	<item>
		<title>Comment on A Simple C++ Quiz by Washu</title>
		<link>http://www.scapecode.com/2011/05/a-simple-c-quiz/#comment-2</link>
		<dc:creator>Washu</dc:creator>
		<pubDate>Thu, 08 Oct 2009 06:05:04 +0000</pubDate>
		<guid isPermaLink="false">http://scapecode.com/?p=107#comment-2</guid>
		<description>[spoiler]&lt;p&gt;1.1 – No, it is undefined behavior as the only allowable range for pointers derived from &lt;code&gt;p&lt;/code&gt; is &lt;code&gt;[p, p + 10]&lt;/code&gt;.&lt;/code&gt; &lt;/p&gt;

&lt;p&gt;1.2 – Since the behavior is undefined, this is meaningless&lt;/p&gt;

&lt;p&gt;1.3 – Comparison between other pointers derived from &lt;code&gt;p&lt;/code&gt;. Decrement and subtraction by any value that would keep &lt;code&gt;k&lt;/code&gt; within &lt;code&gt;[p, p + 10]&lt;/code&gt;. Subtraction between &lt;code&gt;k&lt;/code&gt; and any other pointer derived from &lt;code&gt;p&lt;/code&gt; to obtain a &lt;code&gt;ptrdiff_t.&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;2. Undefined behavior. Due to &lt;code&gt;a&lt;/code&gt; being a scalar type, and the lack of sequence points present, along with the fact that function parameters are evaluated in an unspecified order (but are guaranteed to be evaluated prior to the function call), &lt;code&gt;a&lt;/code&gt; will be modified more than once between sequence points. This results in undefined behavior per the standard.&lt;/p&gt;

&lt;p&gt;3. the &lt;code&gt;, operator&lt;/code&gt; will evaluate its arguments from left to right, returning the result of the rightmost expression. As such the parameters passed to &lt;code&gt;f&lt;/code&gt; are: &lt;code&gt;c&lt;/code&gt;, &lt;code&gt;d&lt;/code&gt;, &lt;code&gt;e&lt;/code&gt;, and &lt;code&gt;i&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;4. There is a potential for the first or second new to be called, and then the alternate new to throw an exception, causing a leak. Also, there may be construction order dependencies between &lt;code&gt;A&lt;/code&gt; and &lt;code&gt;B&lt;/code&gt;, and due to the implementation defined order in which function parameters are evaluated, that dependency could be violated. There is the potential for leakage from &lt;code&gt;f&lt;/code&gt;, although that’s a non-issue since we appear to be transferring ownership of the pointer to &lt;code&gt;f&lt;/code&gt;.&lt;/p&gt;[/spoiler] </description>
		<content:encoded><![CDATA[<div class='easySpoilerWrapper' style=''>
<table class='easySpoilerTable' border='0' style='text-align:center;' align='center' bgcolor='FFFFFF' >
<tr style='white-space:normal;'>
<th class='easySpoilerTitleA'  style='white-space:normal;font-weight:normal;text-align:left;vertical-align:middle;font-size:120%;color:#000000;'>Spoiler Inside</th>
<th class='easySpoilerTitleB'  style='text-align:right;vertical-align:middle;font-size:100%; white-space:nowrap;'>
<a href='' onclick='wpSpoilerSelect("spoilerDiv402a8004"); return false;' class='easySpoilerButtonOther' style='font-size:100%;color:#000000;background-color:#fcfcfc;background-image:none;border: 1px inset;border-style:solid;border-color:#cccccc;  margin: 3px 0px 3px; padding: 4px; ' align='right'>Select</a><a href='' onclick='wpSpoilerToggle("spoilerDiv402a8004",true,"Show","Hide","fast",false); return false;' id='spoilerDiv402a8004_action' class='easySpoilerButton' value="Show" align='right' style='font-size:100%;color:#000000;background-color:#fcfcfc;background-image:none;border: 1px inset;border-style:solid;border-color:#cccccc; margin: 3px 0px 3px 5px; padding: 4px;"'>Show</a></th>
</tr>
<tr>
<td class='easySpoilerRow' colspan='2' style=''>
<div id='spoilerDiv402a8004' class='easySpoilerSpoils'  style='display:none; white-space:wrap; overflow:auto; vertical-align:middle;'>
<p>1.1 – No, it is undefined behavior as the only allowable range for pointers derived from <code>p</code> is <code>[p, p + 10]</code>. </p>
<p>1.2 – Since the behavior is undefined, this is meaningless</p>
<p>1.3 – Comparison between other pointers derived from <code>p</code>. Decrement and subtraction by any value that would keep <code>k</code> within <code>[p, p + 10]</code>. Subtraction between <code>k</code> and any other pointer derived from <code>p</code> to obtain a <code>ptrdiff_t.</code></p>
<p>2. Undefined behavior. Due to <code>a</code> being a scalar type, and the lack of sequence points present, along with the fact that function parameters are evaluated in an unspecified order (but are guaranteed to be evaluated prior to the function call), <code>a</code> will be modified more than once between sequence points. This results in undefined behavior per the standard.</p>
<p>3. the <code>, operator</code> will evaluate its arguments from left to right, returning the result of the rightmost expression. As such the parameters passed to <code>f</code> are: <code>c</code>, <code>d</code>, <code>e</code>, and <code>i</code>.</p>
<p>4. There is a potential for the first or second new to be called, and then the alternate new to throw an exception, causing a leak. Also, there may be construction order dependencies between <code>A</code> and <code>B</code>, and due to the implementation defined order in which function parameters are evaluated, that dependency could be violated. There is the potential for leakage from <code>f</code>, although that’s a non-issue since we appear to be transferring ownership of the pointer to <code>f</code>.</p>
</div>
</td>
</tr>
</table>
<div class='easySpoilerConclude' style=''>
<table class='easySpoilerTable' border='0' style='text-align:center;' frame='box' align='center' bgcolor='FFFFFF'>
<tr>
<th class='easySpoilerEnd' style='width:100%;'></th>
<td class='easySpoilerEnd' style='white-space:nowrap;' colspan='2'></td>
</tr>
<tr>
<td class='easySpoilerGroupWrapperLastRow' colspan='2' style=''></td>
</tr>
</table>
</div>
</div>
]]></content:encoded>
	</item>
</channel>
</rss>

