<?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[const String?]]></title><description><![CDATA[<p>Hallo Leute,</p>
<p>ich habe einen const std::string und mÃ¶chte ihn in eine api schreiben:</p>
<pre><code class="language-cpp">void StartProcess(const string sExePath) {
result = CreateProcess( 0, (char *)(sExePath-&gt;c_str()), 0, 0, FALSE, 0, 0, 0, &amp;si, &amp;pi);
}
</code></pre>
<p>Kann es da zu Fehlern kommen? Die Funktion CreateProcess verÃ¤ndert ev den String. Ist das was c_str() rurÃ¼ckgibt eine Kopie oder nicht? Hier steht das nicht so genau:<br />
<a href="http://www.cppreference.com/cppstring/c_str.html" rel="nofollow">http://www.cppreference.com/cppstring/c_str.html</a></p>
<p>Vielen Dank im voraus</p>
]]></description><link>https://www.c-plusplus.net/forum/topic/177269/const-string</link><generator>RSS for Node</generator><lastBuildDate>Thu, 25 Jun 2026 03:38:03 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/177269.rss" rel="self" type="application/rss+xml"/><pubDate>Thu, 29 Mar 2007 16:56:57 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to const String? on Thu, 29 Mar 2007 16:56:57 GMT]]></title><description><![CDATA[<p>Hallo Leute,</p>
<p>ich habe einen const std::string und mÃ¶chte ihn in eine api schreiben:</p>
<pre><code class="language-cpp">void StartProcess(const string sExePath) {
result = CreateProcess( 0, (char *)(sExePath-&gt;c_str()), 0, 0, FALSE, 0, 0, 0, &amp;si, &amp;pi);
}
</code></pre>
<p>Kann es da zu Fehlern kommen? Die Funktion CreateProcess verÃ¤ndert ev den String. Ist das was c_str() rurÃ¼ckgibt eine Kopie oder nicht? Hier steht das nicht so genau:<br />
<a href="http://www.cppreference.com/cppstring/c_str.html" rel="nofollow">http://www.cppreference.com/cppstring/c_str.html</a></p>
<p>Vielen Dank im voraus</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1255506</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1255506</guid><dc:creator><![CDATA[WurzelD]]></dc:creator><pubDate>Thu, 29 Mar 2007 16:56:57 GMT</pubDate></item><item><title><![CDATA[Reply to const String? on Thu, 29 Mar 2007 17:06:26 GMT]]></title><description><![CDATA[<p>Hallo</p>
<blockquote>
<p>Ist das was c_str() rurÃ¼ckgibt eine Kopie oder nicht?</p>
</blockquote>
<p>Nein ist keine Kopie. Auf die Rückgabe sind ausschließlich Leseoperationen erlaubt. Und es ist nur solange gültig wie auch der string selber gültig ist und nicht verändert wird.</p>
<blockquote>
<p>Die Funktion CreateProcess verÃ¤ndert ev den String</p>
</blockquote>
<p>&quot;Eventuell&quot; ist nicht gut. Exaktheit ist besser. Und beim nachforschen in MSDN kannst du lesen das es ein reiner In-Parameter ist, der Speicherbereich wird nicht verändert.</p>
<blockquote>
<p>Kann es da zu Fehlern kommen?</p>
</blockquote>
<p>Im gezeigten Anwendungsfall nein. Der C-Cast ist noch unschön, typsicherer wäre const_cast wenn überhaupt nötig.</p>
<p>/Edit : Du meinst sicher</p>
<pre><code class="language-cpp">sExePath.c_str()
</code></pre>
<p>bis bald<br />
akari</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1255509</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1255509</guid><dc:creator><![CDATA[akari]]></dc:creator><pubDate>Thu, 29 Mar 2007 17:06:26 GMT</pubDate></item><item><title><![CDATA[Reply to const String? on Thu, 29 Mar 2007 17:14:54 GMT]]></title><description><![CDATA[<p>akari schrieb:</p>
<blockquote>
<p>Und beim nachforschen in MSDN kannst du lesen das es ein reiner In-Parameter ist, der Speicherbereich wird nicht verändert.</p>
</blockquote>
<p>Du musst eine andere MSDN Library haben als ich:</p>
<blockquote>
<p>The system adds a null character to the command line string to separate the file name from the arguments. This divides the original string into two strings for internal processing.</p>
</blockquote>
]]></description><link>https://www.c-plusplus.net/forum/post/1255515</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1255515</guid><dc:creator><![CDATA[MFK]]></dc:creator><pubDate>Thu, 29 Mar 2007 17:14:54 GMT</pubDate></item><item><title><![CDATA[Reply to const String? on Thu, 29 Mar 2007 17:36:15 GMT]]></title><description><![CDATA[<p>Hallo</p>
<p>*grummel* Ja... ich hatte natürlich an lpApplicationName gedacht, nicht an lpCommandLine. Bei ShellExecute bedeutet die erste 0 auch nur ein nicht notwendiges Handle...</p>
<p>Tja dann ist meine Schlußfolgerung auch nicht richtig. Da muß in der Tat eine Kopie vom Rückgabewert von c_str() angelegt werden.</p>
<p>bis bald<br />
akari</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1255528</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1255528</guid><dc:creator><![CDATA[akari]]></dc:creator><pubDate>Thu, 29 Mar 2007 17:36:15 GMT</pubDate></item><item><title><![CDATA[Reply to const String? on Fri, 30 Mar 2007 09:12:38 GMT]]></title><description><![CDATA[<p>Ok. Dann muÃŸ ich es anders machen. Danke</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1255845</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1255845</guid><dc:creator><![CDATA[WurzelD]]></dc:creator><pubDate>Fri, 30 Mar 2007 09:12:38 GMT</pubDate></item><item><title><![CDATA[Reply to const String? on Fri, 30 Mar 2007 10:05:55 GMT]]></title><description><![CDATA[<p>WurzelD schrieb:</p>
<blockquote>
<p>Ok. Dann muÃŸ ich es anders machen. Danke</p>
</blockquote>
<p>Ist aber kein Problem:</p>
<pre><code class="language-cpp">char* buffer = new char[str.length() + 1];
std::copy(str.begin(), str.end(), buffer);
// Übergabe an WinAPI ...
delete [] buffer;
</code></pre>
<p>PS, aus Interesse: Was verwendest Du für einen Browser bzw. was für eine komische Textcodierung benutzt der? Bei Deinen Texten sind die Umlaute verhauen.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1255878</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1255878</guid><dc:creator><![CDATA[Konrad Rudolph]]></dc:creator><pubDate>Fri, 30 Mar 2007 10:05:55 GMT</pubDate></item><item><title><![CDATA[Reply to const String? on Fri, 30 Mar 2007 12:11:55 GMT]]></title><description><![CDATA[<p>WurzelD schrieb:</p>
<blockquote>
<p>Ok. Dann muÃŸ ich es anders machen. Danke</p>
</blockquote>
<p>Hi,</p>
<p>als Standardweg kannst Du einfach eine Kopie anlegen:</p>
<pre><code class="language-cpp">void StartProcess(string const&amp; sExePath) {
   char* tmp = new char[sExePath.length()+1]; // für exceptionsafety noch smart_ptr/auto_ptr/... (spare ich mir wg. Übersichtlickeit)
   copy(sExePath.begin(), sExePath.end(), tmp);
   result = CreateProcess( 0, tmp, 0, 0, FALSE, 0, 0, 0, &amp;si, &amp;pi);
   delete[] tmp;
}
</code></pre>
<p>Für &quot;ändernde Funktionen&quot;, kannst Du vor dem delete[] noch die Daten wieder zurückkopieren.</p>
<p>EDIT: Ähhh - ich hätte vor dem Meeting doch noch abschicken sollen. Aber schön, dass Konrad meiner Version offensichtlich bestätigt.</p>
<p>Gruß,</p>
<p>Simon2.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1255910</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1255910</guid><dc:creator><![CDATA[Simon2]]></dc:creator><pubDate>Fri, 30 Mar 2007 12:11:55 GMT</pubDate></item><item><title><![CDATA[Reply to const String? on Fri, 30 Mar 2007 11:35:57 GMT]]></title><description><![CDATA[<p>Meine Loesung:</p>
<pre><code class="language-cpp">string sExePathChangeable = sExePath;
result = CreateProcessA(0, const_cast&lt;char *&gt;(sExePathChangeable.c_str()), 0, 0, FALSE, 0, 0, 0, &amp;si, &amp;pi);
</code></pre>
<p>Die Kopie sollte ja beschreibbar sein.</p>
<p><a class="plugin-mentions-user plugin-mentions-a" href="https://www.c-plusplus.net/forum/uid/13624">@Simon2</a>: Sehe ich da einen alten C Cast *g*</p>
<p><a class="plugin-mentions-user plugin-mentions-a" href="https://www.c-plusplus.net/forum/uid/5021">@Konrad</a>: Ich schreibe Ã¼ber einen Proxy (<a href="http://www.proxy.org" rel="nofollow">www.proxy.org</a>). Dieser scheint die Umlaute zu verfaelschen.<br />
Ich kenn Dich sogar noch aus dem ActiveVB Forum. Wie klein die Welt doch ist <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/1255933</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1255933</guid><dc:creator><![CDATA[WurzelD]]></dc:creator><pubDate>Fri, 30 Mar 2007 11:35:57 GMT</pubDate></item><item><title><![CDATA[Reply to const String? on Fri, 30 Mar 2007 11:52:04 GMT]]></title><description><![CDATA[<p>WurzelD schrieb:</p>
<blockquote>
<pre><code class="language-cpp">string sExePathChangeable = sExePath;
result = CreateProcessA(0, const_cast&lt;char *&gt;(sExePathChangeable.c_str()), 0, 0, FALSE, 0, 0, 0, &amp;si, &amp;pi);
</code></pre>
<p>Die Kopie sollte ja beschreibbar sein.</p>
</blockquote>
<p>Hmm. Man kann zwar wahrscheinlich davon ausgehen aber hier verlässt Du Dich auch Kenntnisse über die Interna der Stringklasse. Das finde ich tendenziell nicht so schön. Im Prinzip ist dieser const-cast ja ein Hack.</p>
<blockquote>
<p><a class="plugin-mentions-user plugin-mentions-a" href="https://www.c-plusplus.net/forum/uid/5021">@Konrad</a>: Ich kenn Dich sogar noch aus dem ActiveVB Forum. Wie klein die Welt doch ist <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>Oh. In der Tat.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1255939</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1255939</guid><dc:creator><![CDATA[Konrad Rudolph]]></dc:creator><pubDate>Fri, 30 Mar 2007 11:52:04 GMT</pubDate></item><item><title><![CDATA[Reply to const String? on Fri, 30 Mar 2007 12:13:02 GMT]]></title><description><![CDATA[<p>WurzelD schrieb:</p>
<blockquote>
<p>...<br />
<a class="plugin-mentions-user plugin-mentions-a" href="https://www.c-plusplus.net/forum/uid/13624">@Simon2</a>: Sehe ich da einen alten C Cast *g*...</p>
</blockquote>
<p>Du siehst da den Originalcode des OPs, weil das nicht Teil meiner Aufgabe war.. <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f603.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--grinning_face_with_big_eyes"
      title=":D"
      alt="😃"
    /></p>
<p>Außerdem: Wer einen const_cast anbietet, sollte nicht mit Steinen werfen ... <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="😉"
    /><br />
zumal mein C-Cast schlimmstenfalls überflüssig aber durchaus sicher war (jedenfalls so, wie ich ihn gemeint habe - Copy&amp;Paste-Fehler mal außen vor gelassen), während Dein const_cast eine gefährliche Sache ist.</p>
<p>Gruß,</p>
<p>Simon2.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1255945</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1255945</guid><dc:creator><![CDATA[Simon2]]></dc:creator><pubDate>Fri, 30 Mar 2007 12:13:02 GMT</pubDate></item><item><title><![CDATA[Reply to const String? on Fri, 30 Mar 2007 12:23:45 GMT]]></title><description><![CDATA[<p>So ein Mist! Ich muss mich auch noch auf die neuen Casts umstellen <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>
<p>Ich habe den DirtyHack entfernt. Allerdings hat bei Euch noch die terminierende 0 gefehlt:<br />
cBuffer[sExePath.length()] = '\0';</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1255959</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1255959</guid><dc:creator><![CDATA[WurzelD]]></dc:creator><pubDate>Fri, 30 Mar 2007 12:23:45 GMT</pubDate></item><item><title><![CDATA[Reply to const String? on Fri, 30 Mar 2007 12:25:48 GMT]]></title><description><![CDATA[<p>WurzelD schrieb:</p>
<blockquote>
<p>So ein Mist! Ich muss mich auch noch auf die neuen Casts umstellen <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>
<p>Ich habe den DirtyHack entfernt. Allerdings hat bei Euch noch die terminierende 0 gefehlt:<br />
cBuffer[sExePath.length()] = '\0';</p>
</blockquote>
<p>Nicht, wenn new[] die Standardinitialisierung vornimmt ... (und auf Plattformen, in denen 0 == '\0' ist).</p>
<p>Gruß,</p>
<p>Simon2.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1255961</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1255961</guid><dc:creator><![CDATA[Simon2]]></dc:creator><pubDate>Fri, 30 Mar 2007 12:25:48 GMT</pubDate></item><item><title><![CDATA[Reply to const String? on Fri, 30 Mar 2007 12:39:17 GMT]]></title><description><![CDATA[<p>Wenn ich mit der Maus nach Ausfuehrung dieser Zeile ueber den Buffer gehe steht da jede Menge Mist drin und nicht 0:<br />
char* tmp = new char[sExePath.length()+1];</p>
<p>Und wenn man sich diesen Link ansieht:<br />
<a href="http://www.math.uni-bayreuth.de/~rbaier/lectures/c++_ss2001/html/node156.html" rel="nofollow">http://www.math.uni-bayreuth.de/~rbaier/lectures/c++_ss2001/html/node156.html</a></p>
<p>Wird bei Standardtypen auch nicht intitialisiert.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1255974</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1255974</guid><dc:creator><![CDATA[WurzelD]]></dc:creator><pubDate>Fri, 30 Mar 2007 12:39:17 GMT</pubDate></item><item><title><![CDATA[Reply to const String? on Fri, 30 Mar 2007 12:56:15 GMT]]></title><description><![CDATA[<p>WurzelD schrieb:</p>
<blockquote>
<p>Wenn ich mit der Maus nach Ausfuehrung dieser Zeile ueber den Buffer gehe steht da jede Menge Mist drin und nicht 0:<br />
...<br />
Wird bei Standardtypen auch nicht intitialisiert.</p>
</blockquote>
<p>Tja, da war ich mir nicht ganz sicher (ist auch nicht einfach festzustellen) und bei meinem Compiler ist das anders (da sind 0en drin). Ich hatte in meiner ursprünglichen Version auch die Zeile drin und erst später rausgemacht (weil - wie ich dachte - überflüssig).....</p>
<p>OK, nur noch 2:1 für mich <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>
<p>Gruß,</p>
<p>Simon2.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1255986</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1255986</guid><dc:creator><![CDATA[Simon2]]></dc:creator><pubDate>Fri, 30 Mar 2007 12:56:15 GMT</pubDate></item><item><title><![CDATA[Reply to const String? on Fri, 30 Mar 2007 13:10:09 GMT]]></title><description><![CDATA[<p>Simon2 schrieb:</p>
<blockquote>
<p>OK, nur noch 2:1 fuer mich <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>So gross ist das Forum ja nicht... see you again! *g*</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1255994</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1255994</guid><dc:creator><![CDATA[WurzelD]]></dc:creator><pubDate>Fri, 30 Mar 2007 13:10:09 GMT</pubDate></item><item><title><![CDATA[Reply to const String? on Fri, 30 Mar 2007 13:14:04 GMT]]></title><description><![CDATA[<p>WurzelD schrieb:</p>
<blockquote>
<p>Simon2 schrieb:</p>
<blockquote>
<p>OK, nur noch 2:1 fuer mich <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>So gross ist das Forum ja nicht... see you again! *g*</p>
</blockquote>
<p>Ach was - ich lass mich auszahlen .... so einen Glückstreffer bekomme ich hier nicht so schnell wieder *schnell-abmeld* <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f603.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--grinning_face_with_big_eyes"
      title=":D"
      alt="😃"
    /></p>
<p>Gruß,</p>
<p>Simon2.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1255997</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1255997</guid><dc:creator><![CDATA[Simon2]]></dc:creator><pubDate>Fri, 30 Mar 2007 13:14:04 GMT</pubDate></item></channel></rss>