<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	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/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>chys&#039;s random notes &#187; C/C++</title>
	<atom:link href="http://en.chys.info/tag/cc/feed/" rel="self" type="application/rss+xml" />
	<link>http://en.chys.info</link>
	<description>Study more problems; Talk less of isms.</description>
	<lastBuildDate>Tue, 27 Dec 2011 11:56:38 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Integer division</title>
		<link>http://en.chys.info/2011/05/integer-division/</link>
		<comments>http://en.chys.info/2011/05/integer-division/#comments</comments>
		<pubDate>Wed, 25 May 2011 07:31:46 +0000</pubDate>
		<dc:creator>chys</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[C++0x]]></category>
		<category><![CDATA[C/C++]]></category>
		<category><![CDATA[Python]]></category>

		<guid isPermaLink="false">http://en.chys.info/?p=890</guid>
		<description><![CDATA[C89 and C++98 say the result of an integer division where the divisor and/or dividend is negative is implementation defined. This reflects that early hardware implemented integer divisions differently. According to C89/C++98, we may have either (-3)/2 == -1 (round toward zero) or (-3)/2 == -2 (round toward negative infinity). It appears round toward zero [...]<hr/>
No related posts.]]></description>
			<content:encoded><![CDATA[<p>C89 and C++98 say the result of an integer division where the divisor and/or dividend is negative is <a href="http://gcc.gnu.org/onlinedocs/gcc-3.3.6/cpp/Implementation_002ddefined-behavior.html">implementation defined</a>. This reflects that early hardware implemented integer divisions differently.</p>
<p>According to C89/C++98, we may have either <code>(-3)/2 == -1</code> (round toward zero) or <code>(-3)/2 == -2</code> (round toward negative infinity).</p>
<p>It appears <em>round toward zero</em> has become the overwhelming <em>de facto</em> standard now, adopted by both hardware and software vendors. Now both C and C++ explicitly require round toward zero in their new standards (C99 and <a href="http://en.wikipedia.org/wiki/C++0x">C++2011</a>*).</p>
<p>Division of negative integers has always been a complicated problem. Fortran mandated the same round-toward-zero mode much earlier than C/C++; so did Java. Python, on the other hand, has required round toward -∞ (i.e. <code>(-3)//2 == -2</code>) from its beginning. Everybody, nevertheless, agrees that <code>a/b*b + a%b == a</code> should always hold.</p>
<p>* C++0x has yet to be officially approved. Hopefully it will be approved within this year and known as C++2011. I&#8217;m using this name prematurely.</p>
<hr/><p>No related posts.</p>]]></content:encoded>
			<wfw:commentRss>http://en.chys.info/2011/05/integer-division/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Reference to array</title>
		<link>http://en.chys.info/2010/09/reference-to-array/</link>
		<comments>http://en.chys.info/2010/09/reference-to-array/#comments</comments>
		<pubDate>Sun, 26 Sep 2010 13:43:18 +0000</pubDate>
		<dc:creator>chys</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[C/C++]]></category>

		<guid isPermaLink="false">http://en.chys.info/?p=840</guid>
		<description><![CDATA[I recall Microsoft uses this trick in some of its headers. I believe it&#8217;s something like this: template &#60;size_t _Size&#62; inline char *strcpy (char (&#038;dst)[_Size], const char *src) { &#160;&#160;&#160;&#160;strcpy_s (dst, _Size, src); &#160;&#160;&#160;&#160;return dst; } Related posts: GCC #pragma pack bug An Rvalue Reference Issue Rvalue reference<hr/>
Related posts:<ol>
<li><a href='http://en.chys.info/2008/12/gcc-pragma-pack-bug/' rel='bookmark' title='GCC #pragma pack bug'>GCC #pragma pack bug</a></li>
<li><a href='http://en.chys.info/2009/11/an-rvalue-reference-issue/' rel='bookmark' title='An Rvalue Reference Issue'>An Rvalue Reference Issue</a></li>
<li><a href='http://en.chys.info/2009/06/rvalue-reference/' rel='bookmark' title='Rvalue reference'>Rvalue reference</a></li>
</ol>]]></description>
			<content:encoded><![CDATA[<p>I recall Microsoft uses this trick in some of its headers. I believe it&#8217;s something like this:</p>
<blockquote><p>
<code>template &lt;size_t _Size&gt; inline</code><br />
<code>char *strcpy (char (&#038;dst)[_Size], const char *src)</code><br />
<code>{</code><br />
<code>&nbsp;&nbsp;&nbsp;&nbsp;<a href="http://msdn.microsoft.com/en-us/library/td1esda9%28v=VS.100%29.aspx">strcpy_s</a> (dst, _Size, src);</code><br />
<code>&nbsp;&nbsp;&nbsp;&nbsp;return dst;</code><br />
<code>}</code>
</p></blockquote>
<hr/><p>Related posts:<ol>
<li><a href='http://en.chys.info/2008/12/gcc-pragma-pack-bug/' rel='bookmark' title='GCC #pragma pack bug'>GCC #pragma pack bug</a></li>
<li><a href='http://en.chys.info/2009/11/an-rvalue-reference-issue/' rel='bookmark' title='An Rvalue Reference Issue'>An Rvalue Reference Issue</a></li>
<li><a href='http://en.chys.info/2009/06/rvalue-reference/' rel='bookmark' title='Rvalue reference'>Rvalue reference</a></li>
</ol></p>]]></content:encoded>
			<wfw:commentRss>http://en.chys.info/2010/09/reference-to-array/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>const in C and C++</title>
		<link>http://en.chys.info/2010/05/const-in-c-and-c/</link>
		<comments>http://en.chys.info/2010/05/const-in-c-and-c/#comments</comments>
		<pubDate>Mon, 31 May 2010 02:35:05 +0000</pubDate>
		<dc:creator>chys</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[C/C++]]></category>

		<guid isPermaLink="false">http://en.chys.info/?p=815</guid>
		<description><![CDATA[The const keyword has &#8220;constant&#8221; and &#8220;read-only variable&#8221; semantics in C++, but only &#8220;read-only variable&#8221; semantics in C. To illustrate this difference, try compiling the following code: const int x = 2; int y[x]; This is not legal C, because x is not a compile-time constant semantically. But it is legal C++, because const also [...]<hr/>
Related posts:<ol>
<li><a href='http://en.chys.info/2008/11/the-uncertainty-principle/' rel='bookmark' title='The Uncertainty Principle'>The Uncertainty Principle</a></li>
<li><a href='http://en.chys.info/2009/06/rvalue-reference/' rel='bookmark' title='Rvalue reference'>Rvalue reference</a></li>
<li><a href='http://en.chys.info/2010/03/acquire-and-release-semantics/' rel='bookmark' title='Acquire and Release Semantics'>Acquire and Release Semantics</a></li>
</ol>]]></description>
			<content:encoded><![CDATA[<p>The <code>const</code> keyword has &#8220;constant&#8221; and &#8220;read-only variable&#8221; semantics in C++, but only &#8220;read-only variable&#8221; semantics in C.</p>
<p>To illustrate this difference, try compiling the following code:</p>
<blockquote><pre>const int x = 2;
int y[x];</pre>
</blockquote>
<p>This is not legal C, because <code>x</code> is not a compile-time constant semantically.</p>
<p>But it is legal C++, because <code>const</code> also has constant semantics and thus <code>x</code> <em>is</em> a compile-time constant.</p>
<hr/><p>Related posts:<ol>
<li><a href='http://en.chys.info/2008/11/the-uncertainty-principle/' rel='bookmark' title='The Uncertainty Principle'>The Uncertainty Principle</a></li>
<li><a href='http://en.chys.info/2009/06/rvalue-reference/' rel='bookmark' title='Rvalue reference'>Rvalue reference</a></li>
<li><a href='http://en.chys.info/2010/03/acquire-and-release-semantics/' rel='bookmark' title='Acquire and Release Semantics'>Acquire and Release Semantics</a></li>
</ol></p>]]></content:encoded>
			<wfw:commentRss>http://en.chys.info/2010/05/const-in-c-and-c/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>NULL can be a valid address</title>
		<link>http://en.chys.info/2010/04/null-can-be-a-valid-address/</link>
		<comments>http://en.chys.info/2010/04/null-can-be-a-valid-address/#comments</comments>
		<pubDate>Wed, 21 Apr 2010 11:58:31 +0000</pubDate>
		<dc:creator>chys</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[C/C++]]></category>
		<category><![CDATA[Linux]]></category>
		<category><![CDATA[memory]]></category>

		<guid isPermaLink="false">http://en.chys.info/?p=799</guid>
		<description><![CDATA[It is only a convention to consider NULL (0) as an invalid pointer. Technically, the operating system or hardware does not really care if a pointer is zero or not, although operating systems may restrict the use of valid null pointers as they may be a security hole. Consider this program: #include &#60;stdio.h&#62; #include &#60;sys/mman.h&#62; [...]<hr/>
Related posts:<ol>
<li><a href='http://en.chys.info/2009/07/null-pointer-to-member/' rel='bookmark' title='null pointer to member'>null pointer to member</a></li>
<li><a href='http://en.chys.info/2009/05/install-vs-cp-and-mmap/' rel='bookmark' title='install vs. cp; and mmap'>install vs. cp; and mmap</a></li>
</ol>]]></description>
			<content:encoded><![CDATA[<p>It is only a convention to consider <code>NULL</code> (0) as an invalid pointer. Technically, the operating system or hardware does not really care if a pointer is zero or not, although operating systems may restrict the use of valid null pointers as they may be a security hole.</p>
<p>Consider this program:</p>
<blockquote><pre>#include &lt;stdio.h&gt;
#include &lt;sys/mman.h&gt;

int main ()
{
    int *p = mmap (0, 4096, PROT_READ|PROT_WRITE,
        MAP_FIXED|MAP_PRIVATE|MAP_ANONYMOUS, -1, 0);
    *p = 2554;
    printf ("p=%p; *p=%d\n", p, *p);
    return 0;
}
</pre>
</blockquote>
<p>It attempts to make <code>NULL</code> (0) a valid address and then write to it. On Linux, it runs without error as <code>root</code>, but crashes as a normal user.</p>
<p>Note: Calling <code>mmap</code> with <code>NULL</code> as its first argument usually means the kernel will choose an address. However, if <code>MAP_FIXED</code> is also specified, it instead instructs the kernel to use the very address <code>0</code>. Only privileged processes are allowed to do so; a non-privileged process only gets <code>EPERM</code> (Permission denied).</p>
<p>This also explains why <code>MAP_FAILED</code> is equal to <code>(void *)-1</code> instead of <code>NULL</code>.</p>
<hr/><p>Related posts:<ol>
<li><a href='http://en.chys.info/2009/07/null-pointer-to-member/' rel='bookmark' title='null pointer to member'>null pointer to member</a></li>
<li><a href='http://en.chys.info/2009/05/install-vs-cp-and-mmap/' rel='bookmark' title='install vs. cp; and mmap'>install vs. cp; and mmap</a></li>
</ol></p>]]></content:encoded>
			<wfw:commentRss>http://en.chys.info/2010/04/null-can-be-a-valid-address/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>The clock() function</title>
		<link>http://en.chys.info/2009/11/the-clock-function/</link>
		<comments>http://en.chys.info/2009/11/the-clock-function/#comments</comments>
		<pubDate>Wed, 25 Nov 2009 17:56:27 +0000</pubDate>
		<dc:creator>chys</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[C/C++]]></category>
		<category><![CDATA[DOS]]></category>
		<category><![CDATA[time]]></category>
		<category><![CDATA[Windows]]></category>

		<guid isPermaLink="false">http://en.chys.info/?p=706</guid>
		<description><![CDATA[The ISO C standard specifies that The clock function determines the processor time used. It is clear that the result should be processor time instead of wall-clock time (real time). It turns out that clock() in Microsoft C does return the wall-clock time instead of processor time. I do understand Microsoft probably were not intentionally [...]<hr/>
No related posts.]]></description>
			<content:encoded><![CDATA[<p>The ISO C standard specifies that</p>
<blockquote><p>The clock function determines the <em>processor time</em> used.</p></blockquote>
<p>It is clear that the result should be <em>processor time</em> instead of <em>wall-clock time</em> (real time).</p>
<p>It turns out that <a href="http://msdn.microsoft.com/en-us/library/4e2ess30%28VS.71%29.aspx"><code>clock()</code> in Microsoft C</a> does return the <em>wall-clock time</em> instead of processor time.</p>
<p>I do understand Microsoft probably were not intentionally trying to violate the standard. It is meaningless to talk about processor time in DOS (nor does DOS provide any mechanism to measure processor time, afaik), and many programs used <code>clock()</code> to measure real time even if there were lots of system calls, disk accesses, etc. (which would make processor time significantly differ from real time in time-sharing systems). Probably Microsoft intended to maintain this &#8220;compatibility.&#8221; But is this really necessary? They could have corrected this either during the migration from single-task DOS to time-sharing Windows, or from 16-bit Windows 3 to 32-bit Windows 95/NT &#8211; just one more &#8220;incompatibility,&#8221; compared to other huge differences, not so important, was it?</p>
<hr/><p>No related posts.</p>]]></content:encoded>
			<wfw:commentRss>http://en.chys.info/2009/11/the-clock-function/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>An Rvalue Reference Issue</title>
		<link>http://en.chys.info/2009/11/an-rvalue-reference-issue/</link>
		<comments>http://en.chys.info/2009/11/an-rvalue-reference-issue/#comments</comments>
		<pubDate>Thu, 12 Nov 2009 18:52:47 +0000</pubDate>
		<dc:creator>chys</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[C++0x]]></category>
		<category><![CDATA[C/C++]]></category>
		<category><![CDATA[GCC]]></category>
		<category><![CDATA[segfault]]></category>

		<guid isPermaLink="false">http://en.chys.info/?p=698</guid>
		<description><![CDATA[I&#8217;m now convinced it was way too premature to try to take advantage of C++0x features (r-value references, etc.) in tiary (if the compiler supports). With GCC 4.3.4, even the following innocent function leads to segmentation fault: #include &#60;string&#62; #include &#60;utility&#62; std::string &#038;&#038; my_move (std::string &#038;str) { std::string &#038;&#038; tmp = std::move (str); return tmp; [...]<hr/>
Related posts:<ol>
<li><a href='http://en.chys.info/2009/06/rvalue-reference/' rel='bookmark' title='Rvalue reference'>Rvalue reference</a></li>
<li><a href='http://en.chys.info/2009/10/stdhashstdstring/' rel='bookmark' title='std::hash&lt;std::string&gt;'>std::hash&lt;std::string&gt;</a></li>
<li><a href='http://en.chys.info/2009/05/cpp0x-in-icc11/' rel='bookmark' title='C++0x in ICC 11'>C++0x in ICC 11</a></li>
</ol>]]></description>
			<content:encoded><![CDATA[<p>I&#8217;m now convinced it was way too premature to try to take advantage of C++0x features (<a href="http://www.artima.com/cppsource/rvalue.html">r-value references</a>, etc.) in <a href="http://code.google.com/p/tiary/">tiary</a> (if the compiler supports).</p>
<p>With GCC 4.3.4, even the following innocent function leads to segmentation fault:</p>
<blockquote><pre>
#include &lt;string&gt;
#include &lt;utility&gt;

std::string &#038;&#038; my_move (std::string &#038;str)
{
    std::string &#038;&#038; tmp = std::move (str);
    return tmp;
}</pre>
</blockquote>
<p>In GCC 4.4, this function simply casts the non-const lvalue-reference parameter to an r-value reference and returns it, which I think is correct. In 4.3, however, <code>tmp</code> refers to a temporary object on stack, <a href="http://msdn.microsoft.com/en-us/library/dd293665%28VS.100%29.aspx">move-constructed</a> from <code>str</code>.</p>
<p>Then I replaced <code>std::string</code> with <code>std::list&lt;int&gt;</code> and tried again. This time, GCC (4.3.4) itself <a href="http://en.wikipedia.org/wiki/Segmentation_fault">segfaults</a>. Ooops..</p>
<hr/><p>Related posts:<ol>
<li><a href='http://en.chys.info/2009/06/rvalue-reference/' rel='bookmark' title='Rvalue reference'>Rvalue reference</a></li>
<li><a href='http://en.chys.info/2009/10/stdhashstdstring/' rel='bookmark' title='std::hash&lt;std::string&gt;'>std::hash&lt;std::string&gt;</a></li>
<li><a href='http://en.chys.info/2009/05/cpp0x-in-icc11/' rel='bookmark' title='C++0x in ICC 11'>C++0x in ICC 11</a></li>
</ol></p>]]></content:encoded>
			<wfw:commentRss>http://en.chys.info/2009/11/an-rvalue-reference-issue/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>std::hash&lt;std::string&gt;</title>
		<link>http://en.chys.info/2009/10/stdhashstdstring/</link>
		<comments>http://en.chys.info/2009/10/stdhashstdstring/#comments</comments>
		<pubDate>Sun, 04 Oct 2009 14:12:00 +0000</pubDate>
		<dc:creator>chys</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[C++0x]]></category>
		<category><![CDATA[C/C++]]></category>
		<category><![CDATA[GCC]]></category>
		<category><![CDATA[STL]]></category>

		<guid isPermaLink="false">http://en.chys.info/?p=676</guid>
		<description><![CDATA[TR1 requires std::tr1::hash (std::hash in C++0x) to be instantiable for integer/floating point types, std::string and std::wstring. (C++0x added std::error_code, std::thread:id, std::bitset, std::u16string, std::u32string, and std::vector&#60;bool&#62;.) But for strings, every call to std::hash&#60;string&#62;::operator()(std::string) incurs an unnecessary copy construction, which can be expensive in implementations where std::basic_string does not use COW. Developers of GCC are apparently aware [...]<hr/>
Related posts:<ol>
<li><a href='http://en.chys.info/2009/07/string-literals/' rel='bookmark' title='String literals'>String literals</a></li>
<li><a href='http://en.chys.info/2009/11/an-rvalue-reference-issue/' rel='bookmark' title='An Rvalue Reference Issue'>An Rvalue Reference Issue</a></li>
</ol>]]></description>
			<content:encoded><![CDATA[<p><a href="http://en.wikipedia.org/wiki/C%2B%2B_Technical_Report_1">TR1</a> requires <code>std::tr1::hash</code> (<code>std::hash</code> in C++0x) to be instantiable for integer/floating point types, <code>std::string</code> and <code>std::wstring</code>. (C++0x added <code>std::error_code</code>, <code>std::thread:id</code>, <code>std::bitset</code>, <code>std::u16string</code>, <code>std::u32string</code>, and <code>std::vector&lt;bool&gt;</code>.)</p>
<p>But for strings, every call to <code>std::hash&lt;string&gt;::operator()(std::string)</code> incurs an unnecessary copy construction, which can be expensive in implementations where <code>std::basic_string</code> does not use <a href="http://en.wikipedia.org/wiki/Copy-on-write">COW</a>.</p>
<p>Developers of GCC are apparently aware of this, and they added specializations <code>std::hash&lt;const std::string &amp;&gt;</code> and <code>std::hash&lt;const std::wstring &amp;&gt;</code> starting from GCC 4.3.</p>
<p>However, I still guess we cannot easily benefit from this since we will need to write something like this:</p>
<blockquote><pre>
std::unordered_set&lt;std::string, std::hash&lt;const std::string &amp;&gt;&gt;
</pre>
</blockquote>
<p>(In C++0x it&#8217;s no longer required to insert a space between the two larger-than characters.)</p>
<p>Too ugly and inconvenient to use, unless our program is really time critical.</p>
<hr/><p>Related posts:<ol>
<li><a href='http://en.chys.info/2009/07/string-literals/' rel='bookmark' title='String literals'>String literals</a></li>
<li><a href='http://en.chys.info/2009/11/an-rvalue-reference-issue/' rel='bookmark' title='An Rvalue Reference Issue'>An Rvalue Reference Issue</a></li>
</ol></p>]]></content:encoded>
			<wfw:commentRss>http://en.chys.info/2009/10/stdhashstdstring/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>null pointer to member</title>
		<link>http://en.chys.info/2009/07/null-pointer-to-member/</link>
		<comments>http://en.chys.info/2009/07/null-pointer-to-member/#comments</comments>
		<pubDate>Sun, 19 Jul 2009 21:44:40 +0000</pubDate>
		<dc:creator>chys</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[C/C++]]></category>

		<guid isPermaLink="false">http://en.chys.info/?p=619</guid>
		<description><![CDATA[The most straightforward implementation of a pointer to member is to store the offset: struct Struct { int a; int b; }; The internal value of &#038;Struct::a is 0 and &#038;Struct::b is sizeof(int). This leads to the illusion that &#038;Struct::a, a valid pointer, is equal to a null pointer. Not only does this violate the [...]<hr/>
Related posts:<ol>
<li><a href='http://en.chys.info/2010/04/null-can-be-a-valid-address/' rel='bookmark' title='NULL can be a valid address'>NULL can be a valid address</a></li>
</ol>]]></description>
			<content:encoded><![CDATA[<p>The most straightforward implementation of a pointer to member is to store the offset:</p>
<blockquote><pre>struct Struct
{
    int a;
    int b;
};</pre>
</blockquote>
<p>The internal value of <code>&#038;Struct::a</code> is <code>0</code> and <code>&#038;Struct::b</code> is <code>sizeof(int)</code>.</p>
<p>This leads to the illusion that <code>&#038;Struct::a</code>, a valid pointer, is equal to a null pointer. Not only does this violate the standard, but also breaks many codes existing in practice.</p>
<p>The current (pre-<a href="http://en.wikipedia.org/wiki/C++0x">0x</a>) orthodox method of defining a cast from user-defined class to bool is not using <code>operator bool</code> (which leads to some unwanted consequences), but something like this:</p>
<blockquote><pre>
class MyClass
{
   /* ....... */
   struct BooleanConvert { int val; };
   operator int BooleanConvert::* () const
   {
       return ( /* true */ ) ? &#038;BooleanConvert::val : 0;
   }
};
</pre>
</blockquote>
<p>It is hard to read, but the idea is simple: return a valid pointer (to member) if true, a null pointer (to member) if false.</p>
<p>Q: It seems both pointers evaluate to <code>0</code>. How do they distinguish them?</p>
<p>A: In practice, a null pointer to member is represented, internally, by a value of <code>-1</code> instead of  <code>0</code>!</p>
<p>So,</p>
<blockquote><pre>#include &lt;cstdio&gt;
#include &lt;cstddef&gt;
using namespace std;

struct Struct
{
    int a;
};

int main()
{
    union
    {
        int Struct::*ptr;
        void * val;
    };
    ptr = 0;
    printf ("%p\n", val);
}</pre>
</blockquote>
<p>the above program would output <code>0xffffffff</code> (32-bit systems).</p>
<p>Fortunately, this does not annoy. We can safely ignore this detail (except when writing a compiler, of coz..) It seems that, except using unions or brutal memory access, there is no way to convert pointers to member to/from integers and normal pointers. (C-style casts, <code>reinterpret_cast</code>, etc. all reject such conversions.)</p>
<hr/><p>Related posts:<ol>
<li><a href='http://en.chys.info/2010/04/null-can-be-a-valid-address/' rel='bookmark' title='NULL can be a valid address'>NULL can be a valid address</a></li>
</ol></p>]]></content:encoded>
			<wfw:commentRss>http://en.chys.info/2009/07/null-pointer-to-member/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>String literals</title>
		<link>http://en.chys.info/2009/07/string-literals/</link>
		<comments>http://en.chys.info/2009/07/string-literals/#comments</comments>
		<pubDate>Sun, 05 Jul 2009 05:41:31 +0000</pubDate>
		<dc:creator>chys</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[C/C++]]></category>

		<guid isPermaLink="false">http://en.chys.info/?p=591</guid>
		<description><![CDATA[Have type const char [] in C++, but are, as an exception to the general rule, allowed to convert to type char * (though deprecated). Have type char [] in C (even in C99 which has introduced the const keyword), but it is undefined behavior to modify them. (The standard explicitly allows storing them in [...]<hr/>
Related posts:<ol>
<li><a href='http://en.chys.info/2009/10/stdhashstdstring/' rel='bookmark' title='std::hash&lt;std::string&gt;'>std::hash&lt;std::string&gt;</a></li>
<li><a href='http://en.chys.info/2009/06/rvalue-reference/' rel='bookmark' title='Rvalue reference'>Rvalue reference</a></li>
<li><a href='http://en.chys.info/2008/12/gcc-pragma-pack-bug/' rel='bookmark' title='GCC #pragma pack bug'>GCC #pragma pack bug</a></li>
</ol>]]></description>
			<content:encoded><![CDATA[<p>Have type <code>const char []</code> in C++, but are, as an exception to the general rule, allowed to convert to type <code>char *</code> (though deprecated).</p>
<p>Have type <code>char []</code> in C (even in C99 which has introduced the <code>const</code> keyword), but it is <a href="http://en.wikipedia.org/wiki/Undefined_behavior">undefined behavior</a> to modify them. (<a href="http://www.open-std.org/JTC1/SC22/WG14/www/docs/n1256.pdf">The standard</a> explicitly allows storing them in read-only memory, and overlapping identical string literals.)</p>
<p>It seems to me that it is also possible to modify the C standard to align with C++ without altering the behavior of any existing C code. (On the other hand, we cannot modify the C++ definition of the type of string literals without affecting existing codes.)</p>
<hr/><p>Related posts:<ol>
<li><a href='http://en.chys.info/2009/10/stdhashstdstring/' rel='bookmark' title='std::hash&lt;std::string&gt;'>std::hash&lt;std::string&gt;</a></li>
<li><a href='http://en.chys.info/2009/06/rvalue-reference/' rel='bookmark' title='Rvalue reference'>Rvalue reference</a></li>
<li><a href='http://en.chys.info/2008/12/gcc-pragma-pack-bug/' rel='bookmark' title='GCC #pragma pack bug'>GCC #pragma pack bug</a></li>
</ol></p>]]></content:encoded>
			<wfw:commentRss>http://en.chys.info/2009/07/string-literals/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Rvalue reference</title>
		<link>http://en.chys.info/2009/06/rvalue-reference/</link>
		<comments>http://en.chys.info/2009/06/rvalue-reference/#comments</comments>
		<pubDate>Sun, 28 Jun 2009 21:42:37 +0000</pubDate>
		<dc:creator>chys</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[C++0x]]></category>
		<category><![CDATA[C/C++]]></category>

		<guid isPermaLink="false">http://en.chys.info/?p=569</guid>
		<description><![CDATA[The new feature in C++0x was rather confusing to me until yesterday when I suddenly realized that my codes could be more efficient if we had rvalue references. In my understanding, the main practical use of rvalue references is to eliminate spurious copies by introducing a &#8220;move&#8221; semantics in addition to the existing &#8220;copy&#8221; semantics. [...]<hr/>
Related posts:<ol>
<li><a href='http://en.chys.info/2009/11/an-rvalue-reference-issue/' rel='bookmark' title='An Rvalue Reference Issue'>An Rvalue Reference Issue</a></li>
<li><a href='http://en.chys.info/2009/10/stdhashstdstring/' rel='bookmark' title='std::hash&lt;std::string&gt;'>std::hash&lt;std::string&gt;</a></li>
</ol>]]></description>
			<content:encoded><![CDATA[<p>The new feature in C++0x was rather confusing to me until yesterday when I suddenly realized that my codes could be more efficient if we had rvalue references.</p>
<p>In my understanding, the main practical use of rvalue references is to eliminate spurious copies by introducing a &#8220;move&#8221; semantics in addition to the existing &#8220;copy&#8221; semantics.</p>
<p>Suppose we have a map object: <code>map&lt;int,SomeComplexType&gt; my_map;</code></p>
<p>The most intuitive statement to add something to it is <code>my_map[key] = value;</code>.</p>
<p>In current C++, a copy assignment must be triggered here, potentially unnecessary and expensive. (“Copy” semantics.)</p>
<p>If <code>value</code> will not be used later (esp. it&#8217;s a temporary object), we may want to &#8220;move&#8221; instead of &#8220;copy&#8221; it into the map. (“Move” semantics.)<br />
[Sure, we can use <code>value.swap (my_map[key]);</code> if swapping is efficient (e.g. STL strings &amp; containers). But this is rather unreadable.]</p>
<p>In C++0x, with rvalue references, we can distinguish them easily:</p>
<ol>
<li>Use "copy" semantics in <code>SomeComplexType::operator = (const SomeComplexType &#038;);</code></li>
<li>Use "move" semantics in <code>SomeComplexType::operator = (SomeComplexType &#038;&#038;);</code> (Should we call it a "move assignment"?)</li>
</ol>
<p>Now the compiler automatically chooses between the "copy" or "move" semantics for <code>my_map[key] = value;</code>, depending on whether <code>value</code> is an rvalue or not.</p>
<p>It is also possible to force the "move" semantics: <code>my_map[key] = std::move (value);</code></p>
<p>What <code>std::move</code> does is accept either an lvalue or rvalue reference, and return it as an rvalue reference.</p>
<hr/>
<p>Microsoft Visual C++ supports, as a non-standard extension, binding temporary objects to non-const (lvalue) references. This extension cannot substitute rvalue references:</p>
<p><code>string a = "Hello";</code><br />
<code>string b = a;</code></p>
<p>If we use move semantics in <code>string::string (string &#038;)</code>, then <code>a</code> will be empty after <code>b</code>'s construction. This usually is not what we desire.</p>
<hr/>
<p>Again, my main concern about <code>C++0x</code> is that it's going to be too complicated to learn.</p>
<hr/>
<p><strong>Reference</strong>:<br />
<a href="http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2006/n2027.html">A Brief Introduction to Rvalue References</a></p>
<hr/><p>Related posts:<ol>
<li><a href='http://en.chys.info/2009/11/an-rvalue-reference-issue/' rel='bookmark' title='An Rvalue Reference Issue'>An Rvalue Reference Issue</a></li>
<li><a href='http://en.chys.info/2009/10/stdhashstdstring/' rel='bookmark' title='std::hash&lt;std::string&gt;'>std::hash&lt;std::string&gt;</a></li>
</ol></p>]]></content:encoded>
			<wfw:commentRss>http://en.chys.info/2009/06/rvalue-reference/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

