<?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 lokal, mögl. Stack-Overflow]]></title><description><![CDATA[<p>Hallo Leute,</p>
<p>habe ein Problem mit folgendem Code(ausschnitt):</p>
<p>bool Open(const std::string &amp; name)<br />
{<br />
std::ifstream file_in(name.c_str(), std::ios::in | std::ios::binary);<br />
...<br />
data = std::string(size, '\0'); // data = std::string member, size = 200kB<br />
file_in.read(const_cast&lt;char*&gt;(data.data()), data.size()); // File -&gt; String<br />
---<br />
file_in.close();<br />
return true;<br />
}</p>
<p>Verwendet man für Member &lt;data&gt; einen std::string-Zeiger wird am Heap Speicher reserviert und alles läuft super. Will mir aber das Speichermanagement sparen und wollte per normaler Zuweisung (cctor) Member &lt;data&gt; belegen. Beim return schwirrt die App./der Debugger dann ab. Ich nehme nun an, dass der lokale std::string mit der Größe size (200kB) am Stack angelegt wird und ein Überlauf den weiteren Programmablauf unmöglich macht. Hat jemand dazu Ideen (die mit new std::string und delete std::string) hab ich schon?</p>
<p>Danke für eure Bemühungen!</p>
<p>MfG Chris</p>
]]></description><link>https://www.c-plusplus.net/forum/topic/137162/std-string-lokal-mögl-stack-overflow</link><generator>RSS for Node</generator><lastBuildDate>Sat, 29 Aug 2026 16:53:43 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/137162.rss" rel="self" type="application/rss+xml"/><pubDate>Wed, 15 Feb 2006 16:42:50 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to std::string lokal, mögl. Stack-Overflow on Wed, 15 Feb 2006 16:42:50 GMT]]></title><description><![CDATA[<p>Hallo Leute,</p>
<p>habe ein Problem mit folgendem Code(ausschnitt):</p>
<p>bool Open(const std::string &amp; name)<br />
{<br />
std::ifstream file_in(name.c_str(), std::ios::in | std::ios::binary);<br />
...<br />
data = std::string(size, '\0'); // data = std::string member, size = 200kB<br />
file_in.read(const_cast&lt;char*&gt;(data.data()), data.size()); // File -&gt; String<br />
---<br />
file_in.close();<br />
return true;<br />
}</p>
<p>Verwendet man für Member &lt;data&gt; einen std::string-Zeiger wird am Heap Speicher reserviert und alles läuft super. Will mir aber das Speichermanagement sparen und wollte per normaler Zuweisung (cctor) Member &lt;data&gt; belegen. Beim return schwirrt die App./der Debugger dann ab. Ich nehme nun an, dass der lokale std::string mit der Größe size (200kB) am Stack angelegt wird und ein Überlauf den weiteren Programmablauf unmöglich macht. Hat jemand dazu Ideen (die mit new std::string und delete std::string) hab ich schon?</p>
<p>Danke für eure Bemühungen!</p>
<p>MfG Chris</p>
]]></description><link>https://www.c-plusplus.net/forum/post/995084</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/995084</guid><dc:creator><![CDATA[Walrabe]]></dc:creator><pubDate>Wed, 15 Feb 2006 16:42:50 GMT</pubDate></item><item><title><![CDATA[Reply to std::string lokal, mögl. Stack-Overflow on Wed, 15 Feb 2006 16:48:52 GMT]]></title><description><![CDATA[<p>mach mal std::cout &lt;&lt; sizeof(std::string);</p>
<p>mehr speicher wird nicht auf dem stack gebraucht.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/995089</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/995089</guid><dc:creator><![CDATA[tipp]]></dc:creator><pubDate>Wed, 15 Feb 2006 16:48:52 GMT</pubDate></item><item><title><![CDATA[Reply to std::string lokal, mögl. Stack-Overflow on Wed, 15 Feb 2006 16:49:54 GMT]]></title><description><![CDATA[<blockquote>
<pre><code class="language-cpp">data = std::string(size, '\0'); // data = std::string member, size = 200kB
file_in.read(const_cast&lt;char*&gt;(data.data()), data.size()); // File -&gt; String
</code></pre>
</blockquote>
<p>Frickler...</p>
]]></description><link>https://www.c-plusplus.net/forum/post/995092</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/995092</guid><dc:creator><![CDATA[:warning:]]></dc:creator><pubDate>Wed, 15 Feb 2006 16:49:54 GMT</pubDate></item><item><title><![CDATA[Reply to std::string lokal, mögl. Stack-Overflow on Wed, 15 Feb 2006 17:25:28 GMT]]></title><description><![CDATA[<blockquote>
<pre><code class="language-cpp">data = std::string(size, '\0'); // data = std::string member, size = 200kB
file_in.read(const_cast&lt;char*&gt;(data.data()), data.size()); // File -&gt; String
</code></pre>
</blockquote>
<p>das const von data() wegzucasten und dann darauf zu schreiben führt zu UB</p>
<pre><code class="language-cpp">data.assign(size, '\0'); // bzw data.resize(size); falls es nicht auf 0-initialisierung ankommt
file_in.read(&amp;data[0], size); // oder &amp;*data.begin()
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/995128</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/995128</guid><dc:creator><![CDATA[camper]]></dc:creator><pubDate>Wed, 15 Feb 2006 17:25:28 GMT</pubDate></item><item><title><![CDATA[Reply to std::string lokal, mögl. Stack-Overflow on Thu, 16 Feb 2006 09:41:43 GMT]]></title><description><![CDATA[<p>camper schrieb:</p>
<blockquote>
<blockquote>
<pre><code class="language-cpp">data = std::string(size, '\0'); // data = std::string member, size = 200kB
file_in.read(const_cast&lt;char*&gt;(data.data()), data.size()); // File -&gt; String
</code></pre>
</blockquote>
<p>das const von data() wegzucasten und dann darauf zu schreiben führt zu UB</p>
<pre><code class="language-cpp">data.assign(size, '\0'); // bzw data.resize(size); falls es nicht auf 0-initialisierung ankommt
file_in.read(&amp;data[0], size); // oder &amp;*data.begin()
</code></pre>
</blockquote>
<p>Auch davon würde ich persönlich abraten, da der Standard nicht garantiert, dass std::string seinen String &quot;am Stück&quot; als Array von char speichert. Dann doch lieber einen std::vector&lt;char&gt; verwenden.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/995525</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/995525</guid><dc:creator><![CDATA[HumeSikkins]]></dc:creator><pubDate>Thu, 16 Feb 2006 09:41:43 GMT</pubDate></item></channel></rss>