<?xml version="1.0" encoding="utf-8"?>
<rss version="2.0" xml:base="http://www.eiffelroom.org" xmlns:dc="http://purl.org/dc/elements/1.1/">
<channel>
 <title>eiffelroom - complaint - Comments</title>
 <link>http://www.eiffelroom.org/tag/complaint</link>
 <description>Comments for &quot;complaint&quot;</description>
 <language>en</language>
<item>
 <title>Ah ha, 99, very clever!</title>
 <link>http://www.eiffelroom.org/blog/manus_eiffel/in_case_you_did_not_know_c_s#comment-469</link>
 <description>&lt;p&gt;It&#039;s the oldest trick in the book, 99: &lt;em&gt;deterministic finalization&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;Deterministic finalization, Max?&lt;/p&gt;

&lt;p&gt;Yes, 99, they used to use it in the old days. People even used to like it back then; some people even complained when they took deterministic finalization away and gave everyone &lt;em&gt;garbage collection&lt;/em&gt; instead. Can you believe it?&lt;/p&gt;

</description>
 <pubDate>Wed, 25 Jun 2008 05:42:46 -0700</pubDate>
 <dc:creator>peter_gummer</dc:creator>
 <guid isPermaLink="false">comment 469 at http://www.eiffelroom.org</guid>
</item>
<item>
 <title>Yes, that&#039;s exactly what it</title>
 <link>http://www.eiffelroom.org/blog/manus_eiffel/in_case_you_did_not_know_c_s#comment-468</link>
 <description>&lt;p&gt;Yes, that&#039;s exactly what it is. I wonder how many C++ programmer knows about such a subtle semantics.&lt;/p&gt;

&lt;p&gt;Surprisingly, today someone else posted another C++ odity &lt;a href=&quot;http://blogs.msdn.com/oldnewthing/archive/2008/06/23/8640472.aspx&quot;&gt;here&lt;/a&gt;.&lt;/p&gt;

</description>
 <pubDate>Mon, 23 Jun 2008 22:39:00 -0700</pubDate>
 <dc:creator>manus_eiffel</dc:creator>
 <guid isPermaLink="false">comment 468 at http://www.eiffelroom.org</guid>
</item>
<item>
 <title>It seems like getName</title>
 <link>http://www.eiffelroom.org/blog/manus_eiffel/in_case_you_did_not_know_c_s#comment-467</link>
 <description>&lt;p&gt;It seems like getName creates a new string object and returns it by value.  It is copied on the stack of my_routine for the sole purpose of evaluating the expression my_obj-&amp;gt;getName().c_str().&lt;/p&gt;

&lt;p&gt;After the evaluation, the object goes out of scope and is therefore destroyed.  As I recall, c_str returns a pointer to the inner representation of the string object (probably for efficiency reasons, as most rationales in C/C++).&lt;/p&gt;

&lt;p&gt;When the string object is destroyed, its inner representation is freed.  The pointer fed to memcpy is therefore nothing but a dangling pointer which explains the erratic behavior you&#039;re talking of.&lt;/p&gt;

&lt;p&gt;The fact that you store it in a local variable delays the freeing of the inner representation of the string so memcpy can do its work properly.&lt;/p&gt;

&lt;p&gt;Am I close?&lt;/p&gt;

&lt;p&gt;Simon&lt;/p&gt;

</description>
 <pubDate>Mon, 23 Jun 2008 18:45:09 -0700</pubDate>
 <dc:creator>maverick</dc:creator>
 <guid isPermaLink="false">comment 467 at http://www.eiffelroom.org</guid>
</item>
<item>
 <title>Not yet the answer but the</title>
 <link>http://www.eiffelroom.org/blog/manus_eiffel/in_case_you_did_not_know_c_s#comment-466</link>
 <description>&lt;p&gt;Not yet the answer but the code that works:&lt;/p&gt;

&lt;p&gt;&lt;div class=&quot;geshifilter c&quot; style=&quot;font-family: monospace;&quot;&gt;&lt;span style=&quot;color: #993333;&quot;&gt;void&lt;/span&gt; my_routine &lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#40;&lt;/span&gt;CppClass my_obj, &lt;span style=&quot;color: #993333;&quot;&gt;int&lt;/span&gt; buffer_count, &lt;span style=&quot;color: #993333;&quot;&gt;char&lt;/span&gt; * buffer&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#41;&lt;/span&gt; &lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#123;&lt;/span&gt;&lt;br /&gt;
&amp;nbsp; &amp;nbsp; std::&lt;span style=&quot;color: #993333;&quot;&gt;string&lt;/span&gt; l_str = my_obj-&amp;gt;getName&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#41;&lt;/span&gt;;&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #993333;&quot;&gt;const&lt;/span&gt; &lt;span style=&quot;color: #993333;&quot;&gt;char&lt;/span&gt; * l_cstr = l_str.&lt;span style=&quot;color: #202020;&quot;&gt;c_str&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#41;&lt;/span&gt;;&lt;br /&gt;
&amp;nbsp; &amp;nbsp; memcpy&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#40;&lt;/span&gt;buffer, l_cstr, buffer_count&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#41;&lt;/span&gt;;&lt;br /&gt;
&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#125;&lt;/span&gt;&lt;/div&gt;&lt;/p&gt;

&lt;p&gt;Go figure!&lt;/p&gt;

</description>
 <pubDate>Mon, 23 Jun 2008 16:32:18 -0700</pubDate>
 <dc:creator>manus_eiffel</dc:creator>
 <guid isPermaLink="false">comment 466 at http://www.eiffelroom.org</guid>
</item>
<item>
 <title>Well, aside from Peter&#039;s</title>
 <link>http://www.eiffelroom.org/blog/manus_eiffel/in_case_you_did_not_know_c_s#comment-465</link>
 <description>&lt;p&gt;Well, aside from Peter&#039;s comment about the actual size of the buffer, there is also the issue of not terminating the buffer.  The platform may assign more space to the buffer than was allocated (for alignment purposes), and the contents of that extra space are undefined.  If you do not terminate the buffer at location buffer_count then the contents of that space could be interpreted (most likely incorrectly) by the caller or whomever.  Also don&#039;t forget that buffer_count within the routine should be one larger than the result of my_obj-&amp;gt;getName().size(), and of course that&#039;s how large the buffer should be.  You need the room for the termination.  I&#039;m also assuming that you are just working with char-based strings here, and not wide characters.  You could be trying to copy a wide string into a char* for all I know, which means the size of the buffer needs to be even larger than I noted above.&lt;/p&gt;

&lt;p&gt;On a side note, if all you want to do is copy the contents of a std::string, please put it into another std::string.  Don&#039;t use char* if you don&#039;t have to.  If you&#039;re working with an API that requires the use of const char*, then just pass the result of the c_str() function to that API.  Don&#039;t bother with the annoyances of C-based memory management and C-based arrays if you can avoid it.&lt;/p&gt;

</description>
 <pubDate>Mon, 23 Jun 2008 06:37:26 -0700</pubDate>
 <dc:creator>steven.m.wurster</dc:creator>
 <guid isPermaLink="false">comment 465 at http://www.eiffelroom.org</guid>
</item>
<item>
 <title>You&#039;ve got me</title>
 <link>http://www.eiffelroom.org/blog/manus_eiffel/in_case_you_did_not_know_c_s#comment-464</link>
 <description>&lt;p&gt;You&#039;ve got me, Manu. I can&#039;t see anything wrong, other than the obvious, typical memcpy() problem that we have no way of knowing whether buffer really does have buffer_count bytes in order to avoid a buffer overflow.&lt;/p&gt;

&lt;p&gt;Maybe some byte-alignment problem on certain platforms?&lt;/p&gt;

&lt;p&gt;I look forward to seeing the answer to this mind-twister! Who needs Sudoku when we have C++?&lt;/p&gt;

</description>
 <pubDate>Mon, 23 Jun 2008 02:58:53 -0700</pubDate>
 <dc:creator>peter_gummer</dc:creator>
 <guid isPermaLink="false">comment 464 at http://www.eiffelroom.org</guid>
</item>
</channel>
</rss>
