<?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 on: VuGen String Comparison Behaviour</title>
	<atom:link href="http://www.jds.net.au/tech-tips/string-comparison-behaviour/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.jds.net.au/tech-tips/string-comparison-behaviour/</link>
	<description>Expert consulting services with HP Software</description>
	<lastBuildDate>Tue, 08 Jun 2010 15:08:25 +1000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.6</generator>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<xhtml:meta xmlns:xhtml="http://www.w3.org/1999/xhtml" name="robots" content="noindex" />
	<item>
		<title>By: Stuart Moncrieff</title>
		<link>http://www.jds.net.au/tech-tips/string-comparison-behaviour/#comment-4251</link>
		<dc:creator>Stuart Moncrieff</dc:creator>
		<pubDate>Thu, 28 May 2009 01:24:47 +0000</pubDate>
		<guid isPermaLink="false">http://www.jds.net.au/?p=594#comment-4251</guid>
		<description>DING DING DING. We have a winner!

Doug&#039;s comment is spot-on.

The == operator is comparing the memory address of the first character of the string, while strcmp compares the strings character by character.

Here is some code to demonstrate:
&lt;pre lang=&quot;C&quot; line=&quot;1&quot;&gt;
Action()
{
  char* string1 = &quot;hello world&quot;;
  char* string2 = &quot;hello world&quot;;
  char buf[12];
  char* string3;
 
  strcat(buf, &quot;hello &quot;);
  string3 = (char*)strcat(buf, &quot;world&quot;);
  lr_output_message(&quot;string3: %s&quot;, string3);

  // Print memory addresses
  lr_output_message(&quot;string1 memory address: %u&quot;, string1);
  lr_output_message(&quot;string2 memory address: %u&quot;, string2);
  lr_output_message(&quot;string3 memory address: %u&quot;, string3);
}
&lt;/pre&gt;
Here is the output from the code:
&lt;pre lang=&quot;HTML&quot; line=&quot;1&quot;&gt; 
Starting action Action.
Action.c(12): string3: hello world
Action.c(15): string1 memory address: 28181591
Action.c(16): string2 memory address: 28181591
Action.c(17): string3 memory address: 29622316
Ending action Action.
Ending iteration 1.
&lt;/pre&gt;

string1 and string2 have the same memory address because of a compiler optimisation. The compiler can see that two character arrays have been created that are identical. It saves memory by only creating one character array in memory, and putting a reference to the first element of the array in string1 and string2. 

In Java, you would use the term &quot;string pool&quot;. More generally, the concept is known as &quot;&lt;a href=&quot;http://en.wikipedia.org/wiki/String_interning&quot; rel=&quot;nofollow&quot;&gt;string interning&lt;/a&gt;&quot;, and is common to many languages/compilers.</description>
		<content:encoded><![CDATA[<p>DING DING DING. We have a winner!</p>
<p>Doug&#8217;s comment is spot-on.</p>
<p>The == operator is comparing the memory address of the first character of the string, while strcmp compares the strings character by character.</p>
<p>Here is some code to demonstrate:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
</pre></td><td class="code"><pre class="c" style="font-family:monospace;">Action<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
  <span style="color: #993333;">char</span><span style="color: #339933;">*</span> string1 <span style="color: #339933;">=</span> <span style="color: #ff0000;">&quot;hello world&quot;</span><span style="color: #339933;">;</span>
  <span style="color: #993333;">char</span><span style="color: #339933;">*</span> string2 <span style="color: #339933;">=</span> <span style="color: #ff0000;">&quot;hello world&quot;</span><span style="color: #339933;">;</span>
  <span style="color: #993333;">char</span> buf<span style="color: #009900;">&#91;</span><span style="color: #0000dd;">12</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
  <span style="color: #993333;">char</span><span style="color: #339933;">*</span> string3<span style="color: #339933;">;</span>
&nbsp;
  strcat<span style="color: #009900;">&#40;</span>buf<span style="color: #339933;">,</span> <span style="color: #ff0000;">&quot;hello &quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  string3 <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span><span style="color: #993333;">char</span><span style="color: #339933;">*</span><span style="color: #009900;">&#41;</span>strcat<span style="color: #009900;">&#40;</span>buf<span style="color: #339933;">,</span> <span style="color: #ff0000;">&quot;world&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  lr_output_message<span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;string3: %s&quot;</span><span style="color: #339933;">,</span> string3<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #666666; font-style: italic;">// Print memory addresses</span>
  lr_output_message<span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;string1 memory address: %u&quot;</span><span style="color: #339933;">,</span> string1<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  lr_output_message<span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;string2 memory address: %u&quot;</span><span style="color: #339933;">,</span> string2<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  lr_output_message<span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;string3 memory address: %u&quot;</span><span style="color: #339933;">,</span> string3<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>Here is the output from the code:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
</pre></td><td class="code"><pre class="html" style="font-family:monospace;">Starting action Action.
Action.c(12): string3: hello world
Action.c(15): string1 memory address: 28181591
Action.c(16): string2 memory address: 28181591
Action.c(17): string3 memory address: 29622316
Ending action Action.
Ending iteration 1.</pre></td></tr></table></div>

<p>string1 and string2 have the same memory address because of a compiler optimisation. The compiler can see that two character arrays have been created that are identical. It saves memory by only creating one character array in memory, and putting a reference to the first element of the array in string1 and string2. </p>
<p>In Java, you would use the term &#8220;string pool&#8221;. More generally, the concept is known as &#8220;<a href="http://en.wikipedia.org/wiki/String_interning" rel="nofollow">string interning</a>&#8220;, and is common to many languages/compilers.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Doug Sayer-Jones</title>
		<link>http://www.jds.net.au/tech-tips/string-comparison-behaviour/#comment-4183</link>
		<dc:creator>Doug Sayer-Jones</dc:creator>
		<pubDate>Tue, 26 May 2009 07:30:09 +0000</pubDate>
		<guid isPermaLink="false">http://www.jds.net.au/?p=594#comment-4183</guid>
		<description>Hi,

The reason that string1==string2 is true is a product of compiler optimisation and nothing else. 

The two &quot;strings&quot; are defined as char arrays, and the compiler will notice that they are in fact the same string. Because of the initialisation at definition the compiler will therefore create only one array in memory and will place the memory address of the start of the array in to both char pointers (string1 &amp; string2).

In fact early C compilers did not do this optimisation and would have blindly created two arrays and put the appropriate starting memory addresses into each char pointer, and in this case string1==string2 would return false.

The reason string1==string3 is false is that the buf array is uninitialised at creation time so the compiler cannot create the buffer at the same memory location as that used to hold the &quot;hello world&quot; array.

The reason they never become true is that C does not have the concept of singleton string literals (or strings at all of course) that some modern languages do. So even after making the content of the buf array contain the same values, this does not move it in memory. So the start of the buf array (what is held in the pointer string3) will never be the same as the start of the array created with &quot;hello world&quot; in it.

Also please be aware that this should be ANSI-C not C++ so there is no concept of &quot;objects&quot;, a char array is just a piece of memory holding char values, and initialised with ascii zero (null) as the final character of the array in the static array between quotes so that the standard C string library functions work.</description>
		<content:encoded><![CDATA[<p>Hi,</p>
<p>The reason that string1==string2 is true is a product of compiler optimisation and nothing else. </p>
<p>The two &#8220;strings&#8221; are defined as char arrays, and the compiler will notice that they are in fact the same string. Because of the initialisation at definition the compiler will therefore create only one array in memory and will place the memory address of the start of the array in to both char pointers (string1 &amp; string2).</p>
<p>In fact early C compilers did not do this optimisation and would have blindly created two arrays and put the appropriate starting memory addresses into each char pointer, and in this case string1==string2 would return false.</p>
<p>The reason string1==string3 is false is that the buf array is uninitialised at creation time so the compiler cannot create the buffer at the same memory location as that used to hold the &#8220;hello world&#8221; array.</p>
<p>The reason they never become true is that C does not have the concept of singleton string literals (or strings at all of course) that some modern languages do. So even after making the content of the buf array contain the same values, this does not move it in memory. So the start of the buf array (what is held in the pointer string3) will never be the same as the start of the array created with &#8220;hello world&#8221; in it.</p>
<p>Also please be aware that this should be ANSI-C not C++ so there is no concept of &#8220;objects&#8221;, a char array is just a piece of memory holding char values, and initialised with ascii zero (null) as the final character of the array in the static array between quotes so that the standard C string library functions work.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Mohit</title>
		<link>http://www.jds.net.au/tech-tips/string-comparison-behaviour/#comment-1551</link>
		<dc:creator>Mohit</dc:creator>
		<pubDate>Fri, 06 Mar 2009 16:58:54 +0000</pubDate>
		<guid isPermaLink="false">http://www.jds.net.au/?p=594#comment-1551</guid>
		<description>Right, string &quot;hello &quot; is actually &#039;hello &#039; and when its concatenated to &quot;world&quot; the overall string becomes &quot;hello world&quot;. That&#039;s why strcmp doesn&#039;t work. If you replace the existing code with strncat(buf, &quot;hello &quot;,6); in line 10, it should work.

&lt;em&gt;[Stuart&#039;s Reply: Nope. The strings are clearly identical when using strcmp, but using == shows inequality. Why?]&lt;/em&gt;</description>
		<content:encoded><![CDATA[<p>Right, string &#8220;hello &#8221; is actually &#8216;hello &#8216; and when its concatenated to &#8220;world&#8221; the overall string becomes &#8220;hello world&#8221;. That&#8217;s why strcmp doesn&#8217;t work. If you replace the existing code with strncat(buf, &#8220;hello &#8220;,6); in line 10, it should work.</p>
<p><em>[Stuart's Reply: Nope. The strings are clearly identical when using strcmp, but using == shows inequality. Why?]</em></p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Fareed</title>
		<link>http://www.jds.net.au/tech-tips/string-comparison-behaviour/#comment-1418</link>
		<dc:creator>Fareed</dc:creator>
		<pubDate>Wed, 25 Feb 2009 06:25:03 +0000</pubDate>
		<guid isPermaLink="false">http://www.jds.net.au/?p=594#comment-1418</guid>
		<description>== operator is used to compare primitive data types like int,char and double. It can also be used to compare two objects,but it will only check to see if they are the same objects, not if they hold the same contents.

so,

string1 == string2 --&gt; checks only whether they are same objects.(string1 and string2 are same objects)
string1 == string3 --&gt; Here both are different objects as string 3 is a buffer.

&lt;em&gt;[Stuart&#039;s Reply: Nice try, but the == operator does not compare types]&lt;/em&gt;</description>
		<content:encoded><![CDATA[<p>== operator is used to compare primitive data types like int,char and double. It can also be used to compare two objects,but it will only check to see if they are the same objects, not if they hold the same contents.</p>
<p>so,</p>
<p>string1 == string2 &#8211;&gt; checks only whether they are same objects.(string1 and string2 are same objects)<br />
string1 == string3 &#8211;&gt; Here both are different objects as string 3 is a buffer.</p>
<p><em>[Stuart's Reply: Nice try, but the == operator does not compare types]</em></p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Anonymous</title>
		<link>http://www.jds.net.au/tech-tips/string-comparison-behaviour/#comment-1419</link>
		<dc:creator>Anonymous</dc:creator>
		<pubDate>Wed, 07 Jan 2009 06:48:09 +0000</pubDate>
		<guid isPermaLink="false">http://www.jds.net.au/?p=594#comment-1419</guid>
		<description>I think it’s because you have allocated more space to buf (12) and so when this is used to marry up with string3 it actually has more space in reserve although you can’t see it.  By using the == it is able to tell the difference.

Alternatively, the == is somehow able to see the difference between how the two strings have been put together.

The big question is “Am I correct?”.

&lt;em&gt;[Stuart&#039;s Reply: No, you are not correct. :) ]&lt;/em&gt;</description>
		<content:encoded><![CDATA[<p>I think it’s because you have allocated more space to buf (12) and so when this is used to marry up with string3 it actually has more space in reserve although you can’t see it.  By using the == it is able to tell the difference.</p>
<p>Alternatively, the == is somehow able to see the difference between how the two strings have been put together.</p>
<p>The big question is “Am I correct?”.</p>
<p><em>[Stuart's Reply: No, you are not correct. :) ]</em></p>
]]></content:encoded>
	</item>
</channel>
</rss>
