<?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::vector push_back crash im Zusammenhang mit realloc]]></title><description><![CDATA[<p>Hallo Forum,</p>
<p>dieser code crasht in <strong>ptr[ 0 ].push_back( 0.0f )</strong>. Allerdings <strong>nur im Release mode!</strong></p>
<pre><code class="language-cpp">typedef std::vector&lt; float &gt; V;

V s;

V* ptr = NULL;

// 1st &quot;push_back&quot; on ptr

if ( V* tmp = (V*)realloc( ptr, sizeof( V ) ) )
{
    ptr = tmp;

    new ( ptr ) V( s );
}

// 2nd &quot;push_back&quot; on ptr

if ( V* tmp = (V*)realloc( ptr, sizeof( V ) * 2 ) )
{
    ptr = tmp;

    new ( ptr + 1 ) V( s );
}

ptr[ 0 ].push_back( 0.0f );
</code></pre>
<p>Ich hab mich schon dran tot debuggt... das zweite realloc scheint das<br />
Problem zu sein. Hab mir also im VS 2008 memory viewer angesehen, ob<br />
das realloc auch wirklich den alten memory block in den neuen kopiert<br />
und ja das tut es. Verstehe also nicht, wieso es einen crash gibt<br />
wenn ich auf das erste Feld im neu allokierten memory block zugreife?</p>
<p>Hat jemand eine Idee?</p>
<p>LG</p>
]]></description><link>https://www.c-plusplus.net/forum/topic/304732/std-vector-push_back-crash-im-zusammenhang-mit-realloc</link><generator>RSS for Node</generator><lastBuildDate>Thu, 25 Jun 2026 04:20:42 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/304732.rss" rel="self" type="application/rss+xml"/><pubDate>Wed, 13 Jun 2012 10:19:16 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to std::vector push_back crash im Zusammenhang mit realloc on Wed, 13 Jun 2012 10:19:16 GMT]]></title><description><![CDATA[<p>Hallo Forum,</p>
<p>dieser code crasht in <strong>ptr[ 0 ].push_back( 0.0f )</strong>. Allerdings <strong>nur im Release mode!</strong></p>
<pre><code class="language-cpp">typedef std::vector&lt; float &gt; V;

V s;

V* ptr = NULL;

// 1st &quot;push_back&quot; on ptr

if ( V* tmp = (V*)realloc( ptr, sizeof( V ) ) )
{
    ptr = tmp;

    new ( ptr ) V( s );
}

// 2nd &quot;push_back&quot; on ptr

if ( V* tmp = (V*)realloc( ptr, sizeof( V ) * 2 ) )
{
    ptr = tmp;

    new ( ptr + 1 ) V( s );
}

ptr[ 0 ].push_back( 0.0f );
</code></pre>
<p>Ich hab mich schon dran tot debuggt... das zweite realloc scheint das<br />
Problem zu sein. Hab mir also im VS 2008 memory viewer angesehen, ob<br />
das realloc auch wirklich den alten memory block in den neuen kopiert<br />
und ja das tut es. Verstehe also nicht, wieso es einen crash gibt<br />
wenn ich auf das erste Feld im neu allokierten memory block zugreife?</p>
<p>Hat jemand eine Idee?</p>
<p>LG</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2222685</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2222685</guid><dc:creator><![CDATA[Gast777]]></dc:creator><pubDate>Wed, 13 Jun 2012 10:19:16 GMT</pubDate></item><item><title><![CDATA[Reply to std::vector push_back crash im Zusammenhang mit realloc on Wed, 13 Jun 2012 10:32:34 GMT]]></title><description><![CDATA[<p>vector ist nicht trivial kopierbar, also kannst du nicht memcpy und Konsorten (realloc gehört auch dazu) dafür verwenden.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2222690</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2222690</guid><dc:creator><![CDATA[camper]]></dc:creator><pubDate>Wed, 13 Jun 2012 10:32:34 GMT</pubDate></item><item><title><![CDATA[Reply to std::vector push_back crash im Zusammenhang mit realloc on Wed, 13 Jun 2012 10:37:12 GMT]]></title><description><![CDATA[<p>edit: Hier stand Mist.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2222692</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2222692</guid><dc:creator><![CDATA[SeppJ]]></dc:creator><pubDate>Wed, 13 Jun 2012 10:37:12 GMT</pubDate></item><item><title><![CDATA[Reply to std::vector push_back crash im Zusammenhang mit realloc on Wed, 13 Jun 2012 10:46:07 GMT]]></title><description><![CDATA[<pre><code class="language-cpp">typedef std::vector&lt; float &gt; V;

V s;

std::vector&lt;V&gt; p;

p.push_back( s );
p.push_back( s );

p[ 0 ].push_back( 0.0f );
</code></pre>
<p>Ungetestet, aber so ähnlich sollte es gehen</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2222694</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2222694</guid><dc:creator><![CDATA[manni66]]></dc:creator><pubDate>Wed, 13 Jun 2012 10:46:07 GMT</pubDate></item><item><title><![CDATA[Reply to std::vector push_back crash im Zusammenhang mit realloc on Wed, 13 Jun 2012 10:48:28 GMT]]></title><description><![CDATA[<p>Ich finde die Kombination seltsam. Wie kommt man auf die Idee, wo std::vector doch bekannt ist, mehrere Vektoren wieder so frickelig mit realloc anzulegen? Warum nicht</p>
<pre><code class="language-cpp">vector&lt;vector&lt;float&gt; &gt; vv;
</code></pre>
<p>?!</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2222696</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2222696</guid><dc:creator><![CDATA[krümelkacker]]></dc:creator><pubDate>Wed, 13 Jun 2012 10:48:28 GMT</pubDate></item><item><title><![CDATA[Reply to std::vector push_back crash im Zusammenhang mit realloc on Wed, 13 Jun 2012 11:18:06 GMT]]></title><description><![CDATA[<pre><code class="language-cpp">V* ptr = NULL; 
if ( V* tmp = (V*)realloc( ptr, sizeof( V ) ) )
</code></pre>
<p>Was soll das denn? Also:<br />
1.) mit ptr == 0 ist dein realloc das gleiche wie malloc<br />
2.) bei C++ benutzt man new<br />
3.) Speicherallokation von C++ nicht mit der von C wie malloc,realloc oder calloc mischen</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2222706</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2222706</guid><dc:creator><![CDATA[knivil]]></dc:creator><pubDate>Wed, 13 Jun 2012 11:18:06 GMT</pubDate></item><item><title><![CDATA[Reply to std::vector push_back crash im Zusammenhang mit realloc on Wed, 13 Jun 2012 11:52:41 GMT]]></title><description><![CDATA[<p>knivil schrieb:</p>
<blockquote>
<p>1.) mit ptr == 0 ist dein realloc das gleiche wie malloc</p>
</blockquote>
<p>Und mit size==0 ist es wie free... Es ist nicht ganz sinnlos, malloc/free nicht mit realloc zu vermischen. I.d.R. wird man in C++ allerdings keins von beiden verwenden wollen.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2222721</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2222721</guid><dc:creator><![CDATA[camper]]></dc:creator><pubDate>Wed, 13 Jun 2012 11:52:41 GMT</pubDate></item><item><title><![CDATA[Reply to std::vector push_back crash im Zusammenhang mit realloc on Wed, 13 Jun 2012 12:13:39 GMT]]></title><description><![CDATA[<p>Also der code so wie er da steht wird nicht eingesetzt.<br />
Das ist nur ein minimalistisches Beispiel hier für das Forum.</p>
<p>Es geht nur darum herauszufinden, wieso es crasht.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2222730</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2222730</guid><dc:creator><![CDATA[Gast777]]></dc:creator><pubDate>Wed, 13 Jun 2012 12:13:39 GMT</pubDate></item><item><title><![CDATA[Reply to std::vector push_back crash im Zusammenhang mit realloc on Wed, 13 Jun 2012 12:16:13 GMT]]></title><description><![CDATA[<p>camper schrieb:</p>
<blockquote>
<p>vector ist nicht trivial kopierbar, also kannst du nicht memcpy und Konsorten (realloc gehört auch dazu) dafür verwenden.</p>
</blockquote>
<p>Warum nicht? std::vector enthält doch eigentlich nur...</p>
<p>pointer _Myfirst;<br />
pointer _Mylast;<br />
pointer _Myend;</p>
<p>...und sollte daher doch auch unproblematisch sein, wenn er während<br />
eines reallocs kopiert wird?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2222732</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2222732</guid><dc:creator><![CDATA[Gast777]]></dc:creator><pubDate>Wed, 13 Jun 2012 12:16:13 GMT</pubDate></item><item><title><![CDATA[Reply to std::vector push_back crash im Zusammenhang mit realloc on Wed, 13 Jun 2012 12:24:26 GMT]]></title><description><![CDATA[<p>Non-PODs vertragen sich mit dem ganzen C-Speichergefrickel nicht, das ergibt undefiniertes Verhalten. Da gibts auch keine Veranlassung zu, sowas in C++ zu benutzen. Ganz unabhängig davon, ob das bei diesem einen Fall jetzt aus Versehen auf den ersten Blick eigentlich doch irgendwie funktionieren <em>sollte</em> - gewöhn dir sowas garnicht erst an.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2222737</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2222737</guid><dc:creator><![CDATA[pumuckl]]></dc:creator><pubDate>Wed, 13 Jun 2012 12:24:26 GMT</pubDate></item><item><title><![CDATA[Reply to std::vector push_back crash im Zusammenhang mit realloc on Wed, 13 Jun 2012 12:25:20 GMT]]></title><description><![CDATA[<p>Gast777 schrieb:</p>
<blockquote>
<p>Es geht nur darum herauszufinden, wieso es crasht.</p>
</blockquote>
<p>Weil das Kopieren von nicht-POD-Typen mit memcpy und Konsorten schlicht und ergreifend undefiniert ist. Da nützt es nichts, zu diskutieren, warum denn oder dass es ja eigentlich egal sein sollte, es ist so. Sowas macht man nicht. Punkt. <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/2222738</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2222738</guid><dc:creator><![CDATA[LordJaxom]]></dc:creator><pubDate>Wed, 13 Jun 2012 12:25:20 GMT</pubDate></item><item><title><![CDATA[Reply to std::vector push_back crash im Zusammenhang mit realloc on Wed, 13 Jun 2012 12:26:24 GMT]]></title><description><![CDATA[<p>Gast777 schrieb:</p>
<blockquote>
<p>camper schrieb:</p>
<blockquote>
<p>vector ist nicht trivial kopierbar</p>
</blockquote>
<p>Warum nicht? std::vector enthält doch eigentlich nur...</p>
</blockquote>
<p><a href="http://en.cppreference.com/w/cpp/concept/TriviallyCopyable" rel="nofollow">TriviallyCopyable</a></p>
]]></description><link>https://www.c-plusplus.net/forum/post/2222740</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2222740</guid><dc:creator><![CDATA[neuerGast]]></dc:creator><pubDate>Wed, 13 Jun 2012 12:26:24 GMT</pubDate></item><item><title><![CDATA[Reply to std::vector push_back crash im Zusammenhang mit realloc on Wed, 13 Jun 2012 15:35:58 GMT]]></title><description><![CDATA[<p>Also das man non-PODs auf diese Weise nicht kopieren sollte ist mir bewusst.</p>
<p>Mich interessiert trotzdem, was genau in diesem hier genannten Fall auf Speicherebene passiert bzw. schief läuft. Das ist auch der Grund,<br />
warum ich so ein absolut minimalistisches Beispiel gepostet habe.</p>
<p>Ich hatte die Hoffung, dass jemand genau erklären kann, wieso das reine<br />
Kopieren hier in diesem Fall nicht ausreicht. Warum genau crasht es,<br />
was fehlt oder wurde nicht korrekt initialisiert oder zeigt auf irgendwas<br />
ungültiges?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2222824</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2222824</guid><dc:creator><![CDATA[Gast777]]></dc:creator><pubDate>Wed, 13 Jun 2012 15:35:58 GMT</pubDate></item><item><title><![CDATA[Reply to std::vector push_back crash im Zusammenhang mit realloc on Wed, 13 Jun 2012 15:52:34 GMT]]></title><description><![CDATA[<p>Dann nimm dir einen Debugger und vollzieh das Programm Schritt für Schritt nach. Guck dir genau die Interna deiner vectoren an, wie sich diese verändern, ob der Speicher auf den sie zeigen noch gut ist und wann es genau kracht. Bei mir stürzt es beispielsweise nicht ab, aber ich habe vermutlich ein anderes System als du. Ist eben undefiniertes Verhalten. Ganz clevere Compiler optimieren nämlich einfach alles weg.</p>
<p>Ein mögliches Absturzszenario welches ich mir vorstellen könnte, wäre ein vector mit Optimierung für kleine Felder, der einen Teil seiner internen Verwaltungsdaten als Speicher benutzt und den Heap erst dann anpackt, wenn die Datenmenge zu groß wird. Hier wurde dann der Zeiger auf die Daten nach dem realloc auf die eventuell schon freigegebenen alten Daten zeigen.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2222834</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2222834</guid><dc:creator><![CDATA[SeppJ]]></dc:creator><pubDate>Wed, 13 Jun 2012 15:52:34 GMT</pubDate></item><item><title><![CDATA[Reply to std::vector push_back crash im Zusammenhang mit realloc on Wed, 13 Jun 2012 16:36:23 GMT]]></title><description><![CDATA[<p>Ja ich hab mir schon alles reingezogen (disassembly und memory viewer),<br />
aber leider nichts rausfinden können. Das hat mich jetzt so genervt,<br />
dass ich hier einfach mal gepostet habe.</p>
<p>Danke schon mal für die Vermutungen!</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2222851</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2222851</guid><dc:creator><![CDATA[Gast777]]></dc:creator><pubDate>Wed, 13 Jun 2012 16:36:23 GMT</pubDate></item><item><title><![CDATA[Reply to std::vector push_back crash im Zusammenhang mit realloc on Wed, 13 Jun 2012 21:17:24 GMT]]></title><description><![CDATA[<p>Es kann beispielsweise das Alignment nicht stimmen.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2222942</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2222942</guid><dc:creator><![CDATA[knivil]]></dc:creator><pubDate>Wed, 13 Jun 2012 21:17:24 GMT</pubDate></item><item><title><![CDATA[Reply to std::vector push_back crash im Zusammenhang mit realloc on Wed, 13 Jun 2012 21:45:36 GMT]]></title><description><![CDATA[<p>knivil schrieb:</p>
<blockquote>
<p>Es kann beispielsweise das Alignment nicht stimmen.</p>
</blockquote>
<p>Das kann nicht passieren.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2222950</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2222950</guid><dc:creator><![CDATA[camper]]></dc:creator><pubDate>Wed, 13 Jun 2012 21:45:36 GMT</pubDate></item><item><title><![CDATA[Reply to std::vector push_back crash im Zusammenhang mit realloc on Thu, 14 Jun 2012 08:13:02 GMT]]></title><description><![CDATA[<p>So wie ich das sehe, müssten die drei Pointer immer noch auf den gleichen Speicherbereich zeigen. Es kann sich also nur die Bedeutung geändert haben. Zum Beispiel könnte _Myfirst immer auf das zeigen, worauf <code>data()</code> zeigen sollte, wenn der Vektor leer ist, also auf den Speicherbereich direkt nach dem Vektor. Wenn du ihn reallokierst stimmt das nicht mehr und es kracht wenn du auf den Vektor zugreifen willst.</p>
<p>Um das herauszufinden, musst du dir die Implementation vom Vektor im MSVC genau anschauen.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2223008</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2223008</guid><dc:creator><![CDATA[ratespiel]]></dc:creator><pubDate>Thu, 14 Jun 2012 08:13:02 GMT</pubDate></item></channel></rss>