<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[std::string - modifizieren über &amp;amp;str[0] OK?]]></title><description><![CDATA[<p>für std::vector gilt ja, dass man sich mit &amp;vec[0] die &quot;startadresse&quot; des arrays holen darf, und über diesen zeiger dann fleissig das array modifizieren.</p>
<p>frage: gilt das für std::string auch?</p>
<p>oder andersrum: ich darf ja auch bei std::string die von operator [] referenz zum ändern des strings verwenden. ist bei std::string zusätzlich garantiert, dass die elemente wie bei std::vector im speicher unmittelbar hintereinander abgelegt sind?</p>
]]></description><link>https://www.c-plusplus.net/forum/topic/260820/std-string-modifizieren-über-amp-str-0-ok</link><generator>RSS for Node</generator><lastBuildDate>Mon, 07 Sep 2026 22:29:47 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/260820.rss" rel="self" type="application/rss+xml"/><pubDate>Tue, 09 Feb 2010 15:58:05 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to std::string - modifizieren über &amp;amp;str[0] OK? on Tue, 09 Feb 2010 15:58:05 GMT]]></title><description><![CDATA[<p>für std::vector gilt ja, dass man sich mit &amp;vec[0] die &quot;startadresse&quot; des arrays holen darf, und über diesen zeiger dann fleissig das array modifizieren.</p>
<p>frage: gilt das für std::string auch?</p>
<p>oder andersrum: ich darf ja auch bei std::string die von operator [] referenz zum ändern des strings verwenden. ist bei std::string zusätzlich garantiert, dass die elemente wie bei std::vector im speicher unmittelbar hintereinander abgelegt sind?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1853214</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1853214</guid><dc:creator><![CDATA[hustbaer]]></dc:creator><pubDate>Tue, 09 Feb 2010 15:58:05 GMT</pubDate></item><item><title><![CDATA[Reply to std::string - modifizieren über &amp;amp;str[0] OK? on Tue, 09 Feb 2010 16:08:22 GMT]]></title><description><![CDATA[<p>Ja.</p>
<p>21.3/2 schrieb:</p>
<blockquote>
<p>The class template basic_string conforms to the requirements of a Sequence, as specified in (23.1.1).[...]</p>
</blockquote>
<p>und:</p>
<p>23.1.1/1 schrieb:</p>
<blockquote>
<p>A sequence is a kind of container that organizes a finite set of objects, all of the same type, into a strictly<br />
linear arrangement. The library provides three basic kinds of sequence containers: vector, list, and<br />
deque.[...]</p>
</blockquote>
<p>Da wird ja indirekt sogar ein Bezug zu <code>std::vector</code> gemacht.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1853222</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1853222</guid><dc:creator><![CDATA[drakon]]></dc:creator><pubDate>Tue, 09 Feb 2010 16:08:22 GMT</pubDate></item><item><title><![CDATA[Reply to std::string - modifizieren über &amp;amp;str[0] OK? on Tue, 09 Feb 2010 16:12:13 GMT]]></title><description><![CDATA[<p>drakon schrieb:</p>
<blockquote>
<p>23.1.1/1 schrieb:</p>
<blockquote>
<p>A sequence is a kind of container that organizes a finite set of objects, all of the same type, into a strictly<br />
linear arrangement. The library provides three basic kinds of sequence containers: vector, list, and<br />
deque.[...]</p>
</blockquote>
<p>Da wird ja indirekt sogar ein Bezug zu <code>std::vector</code> gemacht.</p>
</blockquote>
<p>Da steht auch list, und da liegt garantiert nichts hintereinander im Speicher.<br />
Ich hab vorhin auch mal im Standard gesucht und keine Garantie gefunden. Und wenn ich mich recht erinnere wurde hier im Forum schonmal darauf hingewiesen, dass man sich nicht darauf verlassen kann, dass das hintereinander im Speicher liegt.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1853226</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1853226</guid><dc:creator><![CDATA[Ryuzaki]]></dc:creator><pubDate>Tue, 09 Feb 2010 16:12:13 GMT</pubDate></item><item><title><![CDATA[Reply to std::string - modifizieren über &amp;amp;str[0] OK? on Tue, 09 Feb 2010 16:24:49 GMT]]></title><description><![CDATA[<p>Ryuzaki schrieb:</p>
<blockquote>
<p>Und wenn ich mich recht erinnere wurde hier im Forum schonmal darauf hingewiesen, dass man sich nicht darauf verlassen kann</p>
</blockquote>
<p>Stimmt, ich hatte damals permanent das Gegenteil behauptet.. Jemand (Tachyon?) kannte auch eine std::string-Implementierung, afair für 'nen Mikrocontroller, die die Zeichen in einer dequeue verwaltet und erst bei <code>c_str()</code> einen fertigen string zusammenstellt. Obwohl <code>c_str</code> ja <code>const</code> ist, aber es ist vlt nur ein &quot;außen-const&quot;. Aber ich hab keine Ahnung, wie es im Standard aussieht.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1853231</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1853231</guid><dc:creator><![CDATA[Badestrand_]]></dc:creator><pubDate>Tue, 09 Feb 2010 16:24:49 GMT</pubDate></item><item><title><![CDATA[Reply to std::string - modifizieren über &amp;amp;str[0] OK? on Tue, 09 Feb 2010 16:27:24 GMT]]></title><description><![CDATA[<p>21.3.4/1 schrieb:</p>
<blockquote>
<p>Returns: If pos &lt; size(), returns data()[pos]. Otherwise, if pos == size(), the const<br />
version returns charT(). Otherwise, the behavior is undefined.</p>
</blockquote>
<p>und</p>
<p>21.3.6/3/4 schrieb:</p>
<blockquote>
<p>Returns: If size() is nonzero, the member returns a pointer to the initial element of an array whose first<br />
size() elements equal the corresponding elements of the string controlled by *this. If size() is<br />
zero, the member returns a non-null pointer that is copyable and can have zero added to it.<br />
4 Requires: The program shall not alter any of the values stored in the character array. Nor shall the program<br />
treat the returned value as a valid pointer value after any subsequent call to a non- const member<br />
function of basic_string that designates the same object as this.</p>
</blockquote>
<p>Aber: <a href="http://www.open-std.org/jtc1/sc22/wg21/docs/lwg-defects.html#530" rel="nofollow">http://www.open-std.org/jtc1/sc22/wg21/docs/lwg-defects.html#530</a></p>
]]></description><link>https://www.c-plusplus.net/forum/post/1853234</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1853234</guid><dc:creator><![CDATA[David_pb]]></dc:creator><pubDate>Tue, 09 Feb 2010 16:27:24 GMT</pubDate></item><item><title><![CDATA[Reply to std::string - modifizieren über &amp;amp;str[0] OK? on Tue, 09 Feb 2010 16:34:39 GMT]]></title><description><![CDATA[<p>Ryuzaki schrieb:</p>
<blockquote>
<p>drakon schrieb:</p>
<blockquote>
<p>23.1.1/1 schrieb:</p>
<blockquote>
<p>A sequence is a kind of container that organizes a finite set of objects, all of the same type, into a strictly<br />
linear arrangement. The library provides three basic kinds of sequence containers: vector, list, and<br />
deque.[...]</p>
</blockquote>
<p>Da wird ja indirekt sogar ein Bezug zu <code>std::vector</code> gemacht.</p>
</blockquote>
<p>Da steht auch list, und da liegt garantiert nichts hintereinander im Speicher.<br />
Ich hab vorhin auch mal im Standard gesucht und keine Garantie gefunden. Und wenn ich mich recht erinnere wurde hier im Forum schonmal darauf hingewiesen, dass man sich nicht darauf verlassen kann, dass das hintereinander im Speicher liegt.</p>
</blockquote>
<p>Stimmt. Da habe ich vorschnell das linear falsch gedeutet.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1853238</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1853238</guid><dc:creator><![CDATA[drakon]]></dc:creator><pubDate>Tue, 09 Feb 2010 16:34:39 GMT</pubDate></item><item><title><![CDATA[Reply to std::string - modifizieren über &amp;amp;str[0] OK? on Tue, 09 Feb 2010 16:36:14 GMT]]></title><description><![CDATA[<p>Ryuzaki schrieb:</p>
<blockquote>
<p>Da steht auch list, und da liegt garantiert nichts hintereinander im Speicher.</p>
</blockquote>
<p>Garantiert der Standard etwa, das bei <code>std::list</code> nichts hintereinander liegt? <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f609.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--winking_face"
      title=";)"
      alt="😉"
    /></p>
]]></description><link>https://www.c-plusplus.net/forum/post/1853239</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1853239</guid><dc:creator><![CDATA[[[global:guest]]]]></dc:creator><pubDate>Tue, 09 Feb 2010 16:36:14 GMT</pubDate></item><item><title><![CDATA[Reply to std::string - modifizieren über &amp;amp;str[0] OK? on Tue, 09 Feb 2010 18:42:59 GMT]]></title><description><![CDATA[<p>daersc schrieb:</p>
<blockquote>
<p>Ryuzaki schrieb:</p>
<blockquote>
<p>Da steht auch list, und da liegt garantiert nichts hintereinander im Speicher.</p>
</blockquote>
<p>Garantiert der Standard etwa, das bei <code>std::list</code> nichts hintereinander liegt? <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f609.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--winking_face"
      title=";)"
      alt="😉"
    /></p>
</blockquote>
<p>Ja, indirekt. Nämlich durch die complexity-bounds diverser Operationen wie splice().</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1853281</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1853281</guid><dc:creator><![CDATA[hustbaer]]></dc:creator><pubDate>Tue, 09 Feb 2010 18:42:59 GMT</pubDate></item><item><title><![CDATA[Reply to std::string - modifizieren über &amp;amp;str[0] OK? on Tue, 09 Feb 2010 18:47:20 GMT]]></title><description><![CDATA[<p>David_pb schrieb:</p>
<blockquote>
<p>21.3.4/1 schrieb:</p>
<blockquote>
<p>Returns: If pos &lt; size(), returns data()[pos]. Otherwise, if pos == size(), the const<br />
version returns charT(). Otherwise, the behavior is undefined.</p>
</blockquote>
<p>und</p>
<p>21.3.6/3/4 schrieb:</p>
<blockquote>
<p>Returns: If size() is nonzero, the member returns a pointer to the initial element of an array whose first<br />
size() elements equal the corresponding elements of the string controlled by *this. If size() is<br />
zero, the member returns a non-null pointer that is copyable and can have zero added to it.<br />
4 Requires: The program shall not alter any of the values stored in the character array. Nor shall the program<br />
treat the returned value as a valid pointer value after any subsequent call to a non- const member<br />
function of basic_string that designates the same object as this.</p>
</blockquote>
<p>Aber: <a href="http://www.open-std.org/jtc1/sc22/wg21/docs/lwg-defects.html#530" rel="nofollow">http://www.open-std.org/jtc1/sc22/wg21/docs/lwg-defects.html#530</a></p>
</blockquote>
<p>Also wenn ich das richtig verstehe, dann kann ich getrost str.data() verwenden um in einen String &quot;reinzumalen&quot; - mittels memcpy() oder was auch immer.</p>
<p>Vorausgesetzt natürlich dass der String gross genug ist.</p>
<p>Ob die Adresse die ich über das Iterator-Interface bekomme auch die selbe ist, ist mir ziemlich egal. Ich muss das Iterator-Interface ja nicht verwenden.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1853282</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1853282</guid><dc:creator><![CDATA[hustbaer]]></dc:creator><pubDate>Tue, 09 Feb 2010 18:47:20 GMT</pubDate></item><item><title><![CDATA[Reply to std::string - modifizieren über &amp;amp;str[0] OK? on Tue, 09 Feb 2010 18:51:43 GMT]]></title><description><![CDATA[<p>hustbaer schrieb:</p>
<blockquote>
<p>Also wenn ich das richtig verstehe, dann kann ich getrost str.data() verwenden um in einen String &quot;reinzumalen&quot; - mittels memcpy() oder was auch immer.</p>
</blockquote>
<p>Nein, da data einen Zeiger auf konstantes charT liefert. Spätestens bei einer COW-Implementierung dürfte es Probleme geben.<br />
&amp;*begin() oder &amp;at(0) etc. sind dagegen unproblematisch.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1853283</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1853283</guid><dc:creator><![CDATA[camper]]></dc:creator><pubDate>Tue, 09 Feb 2010 18:51:43 GMT</pubDate></item><item><title><![CDATA[Reply to std::string - modifizieren über &amp;amp;str[0] OK? on Tue, 09 Feb 2010 19:04:22 GMT]]></title><description><![CDATA[<p>Jain, in 21.3.6/4 steht ganz zu beginn: <em>The program shall not alter any of the values stored in the character array</em> und da operator[] auf <em>data()</em> basiert gilt das für diesen wohl auch.</p>
<p>Aufgrund <a href="http://www.open-std.org/jtc1/sc22/wg21/docs/lwg-defects.html#530" rel="nofollow">http://www.open-std.org/jtc1/sc22/wg21/docs/lwg-defects.html#530</a> funktioniert das wohl trotzdem mit allen möglichen Implementierungen von basic_string. Allerdings ist das wohl nicht durch den Standard definiert.</p>
<p>In C++0x gibts da glaub ich dann eine Klausel die das garantiert. <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f642.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--slightly_smiling_face"
      title=":)"
      alt="🙂"
    /></p>
]]></description><link>https://www.c-plusplus.net/forum/post/1853284</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1853284</guid><dc:creator><![CDATA[David_pb]]></dc:creator><pubDate>Tue, 09 Feb 2010 19:04:22 GMT</pubDate></item><item><title><![CDATA[Reply to std::string - modifizieren über &amp;amp;str[0] OK? on Tue, 09 Feb 2010 19:10:46 GMT]]></title><description><![CDATA[<p>David_pb schrieb:</p>
<blockquote>
<p>Jain, in 21.3.6/4 steht ganz zu beginn: <em>The program shall not alter any of the values stored in the character array</em> und da operator[] auf <em>data()</em> basiert gilt das für diesen wohl auch.</p>
<p>Aufgrund <a href="http://www.open-std.org/jtc1/sc22/wg21/docs/lwg-defects.html#530" rel="nofollow">http://www.open-std.org/jtc1/sc22/wg21/docs/lwg-defects.html#530</a> funktioniert das wohl trotzdem mit allen möglichen Implementierungen von basic_string. Allerdings ist das wohl nicht durch den Standard definiert.</p>
<p>In C++0x gibts da glaub ich dann eine Klausel die das garantiert. <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f642.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--slightly_smiling_face"
      title=":)"
      alt="🙂"
    /></p>
</blockquote>
<p>Nicht ganz <a href="http://www.open-std.org/jtc1/sc22/wg21/docs/lwg-defects.html#259" rel="nofollow">basic_string::operator[] and const correctness</a></p>
]]></description><link>https://www.c-plusplus.net/forum/post/1853289</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1853289</guid><dc:creator><![CDATA[camper]]></dc:creator><pubDate>Tue, 09 Feb 2010 19:10:46 GMT</pubDate></item><item><title><![CDATA[Reply to std::string - modifizieren über &amp;amp;str[0] OK? on Tue, 09 Feb 2010 20:03:35 GMT]]></title><description><![CDATA[<p>Er, ja, da ich data() nie verwende, wusste ich nicht, dass es davon keine non-const Variante gibt. Const wegcasten werde ich natürlich nicht.</p>
<p>Ich denke ich werde einfach &amp;at(0) bzw. &amp;[0] verwenden.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1853313</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1853313</guid><dc:creator><![CDATA[hustbaer]]></dc:creator><pubDate>Tue, 09 Feb 2010 20:03:35 GMT</pubDate></item><item><title><![CDATA[Reply to std::string - modifizieren über &amp;amp;str[0] OK? on Tue, 09 Feb 2010 21:02:18 GMT]]></title><description><![CDATA[<p>daersc schrieb:</p>
<blockquote>
<p>Garantiert der Standard etwa, das bei <code>std::list</code> nichts hintereinander liegt? <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f609.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--winking_face"
      title=";)"
      alt="😉"
    /></p>
</blockquote>
<p>Ja, indirekt durch die Anforderungen an die Zeitkomplexität der Operationen.</p>
<p>hustbaer schrieb:</p>
<blockquote>
<p>Ich denke ich werde einfach &amp;at(0) bzw. &amp;[0] verwenden.</p>
</blockquote>
<p>Blöd ist nur, wenn du (oder sonst jemand) den Code portiert und dann nicht mehr daran denkt, und plötzlich hat man merkwürdige Bugs. Vielleicht kannst du ja eine Assertion schreiben, à la</p>
<pre><code class="language-cpp">char* string_data(std::string&amp; str)
{
   assert(&amp;str[str.size()-1] - &amp;str[0] == static_cast&lt;ptrdiff_t&gt;(str.size()-1));
   return &amp;str[0];
}
</code></pre>
<p>Die Subtraktion erzeugt zwar undefiniertes Verhalten, wenn die beiden Zeiger nicht im gleichen Array stehen, aber sowas dürfte meiner Ansicht nach noch eher toleriert werden (und falls nicht, hat man auch nichts verloren).</p>
<p>Ist nur so ein Vorschlag. Wenn du den Code nie portieren wirst und dir sicher bist, dass sich deine StdLib-Implementierung nicht ändert, ist das natürlich unnötig.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1853341</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1853341</guid><dc:creator><![CDATA[Nexus]]></dc:creator><pubDate>Tue, 09 Feb 2010 21:02:18 GMT</pubDate></item><item><title><![CDATA[Reply to std::string - modifizieren über &amp;amp;str[0] OK? on Tue, 09 Feb 2010 21:23:19 GMT]]></title><description><![CDATA[<p>Ja mal sehen.<br />
Lustigerweise funktioniert es sogar mit der COW Implementierung vom VC6.<br />
Sogar die erzwingt bei at() und [] (und begin/end/rbegin/rend) korrekterweise dass der String eindeutig gemacht wird.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1853345</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1853345</guid><dc:creator><![CDATA[hustbaer]]></dc:creator><pubDate>Tue, 09 Feb 2010 21:23:19 GMT</pubDate></item></channel></rss>