<?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[Absturz bei realloc()]]></title><description><![CDATA[<p>Hi,</p>
<p>ich bin gerade dabei etwas mit Klassen herumzuspielen, dabei bin ich bisher zu folgendem Code gelangt:</p>
<pre><code>#include &lt;iostream&gt;
#include &lt;string&gt;
using namespace std;

class Geldausgabe
{
    private:
        int monat;
        int tag;
        float summe;
        string grund;
    public:
        void setMonat(int neuerMonat) { monat = neuerMonat; }
        void setTag(int neuerTag) { tag = neuerTag; }
        void setSumme(float neueSumme) { summe = neueSumme; }
        void setGrund(string neuerGrund) { grund = neuerGrund; } 
        int getMonat() { return monat; }
        int getTag() { return tag; }
        float getSumme() { return summe; }
        string getGrund() { return grund; }
};

class GeldausgabenVerwaltung
{
    private:
        Geldausgabe *ausgaben;
        int anzahl;
    public:
        GeldausgabenVerwaltung()
        {
            anzahl = 0;
            ausgaben = (Geldausgabe*)malloc(0);
        }
        ~GeldausgabenVerwaltung()
        {
            free(ausgaben);
        }    
        void speichereGeldausgabe(int tag, int monat, float summe, string grund)
        {
            Geldausgabe ausgabe;

            ausgabe.setTag(tag);
            ausgabe.setMonat(monat);
            ausgabe.setSumme(summe);
            ausgabe.setGrund(grund);

            anzahl++;
            ausgaben = (Geldausgabe*)realloc(ausgaben, anzahl*sizeof(Geldausgabe));
            ausgaben[anzahl-1] = ausgabe;
        }
};
</code></pre>
<p>Die main-Methode sollte hier nicht wichtig sein. Wenn ich nun speichereGeldausgabe mit korrekten Parametern aufrufe stürzt das Programm ab. Allerdings nur, wenn die letzte Zeile (ausgaben[anzahl-1] = ausgabe) nicht auskommentiert ist. Lasse ich sie weg, stürzt das Programm zumindest nicht ab. Was meint ihr, woran das liegen könnte? Ich seh den Fehler einfach nicht.</p>
<p>Danke und Gruß</p>
]]></description><link>https://www.c-plusplus.net/forum/topic/242740/absturz-bei-realloc</link><generator>RSS for Node</generator><lastBuildDate>Sun, 20 Sep 2026 21:15:47 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/242740.rss" rel="self" type="application/rss+xml"/><pubDate>Sun, 07 Jun 2009 17:21:19 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to Absturz bei realloc() on Sun, 07 Jun 2009 17:22:12 GMT]]></title><description><![CDATA[<p>Hi,</p>
<p>ich bin gerade dabei etwas mit Klassen herumzuspielen, dabei bin ich bisher zu folgendem Code gelangt:</p>
<pre><code>#include &lt;iostream&gt;
#include &lt;string&gt;
using namespace std;

class Geldausgabe
{
    private:
        int monat;
        int tag;
        float summe;
        string grund;
    public:
        void setMonat(int neuerMonat) { monat = neuerMonat; }
        void setTag(int neuerTag) { tag = neuerTag; }
        void setSumme(float neueSumme) { summe = neueSumme; }
        void setGrund(string neuerGrund) { grund = neuerGrund; } 
        int getMonat() { return monat; }
        int getTag() { return tag; }
        float getSumme() { return summe; }
        string getGrund() { return grund; }
};

class GeldausgabenVerwaltung
{
    private:
        Geldausgabe *ausgaben;
        int anzahl;
    public:
        GeldausgabenVerwaltung()
        {
            anzahl = 0;
            ausgaben = (Geldausgabe*)malloc(0);
        }
        ~GeldausgabenVerwaltung()
        {
            free(ausgaben);
        }    
        void speichereGeldausgabe(int tag, int monat, float summe, string grund)
        {
            Geldausgabe ausgabe;

            ausgabe.setTag(tag);
            ausgabe.setMonat(monat);
            ausgabe.setSumme(summe);
            ausgabe.setGrund(grund);

            anzahl++;
            ausgaben = (Geldausgabe*)realloc(ausgaben, anzahl*sizeof(Geldausgabe));
            ausgaben[anzahl-1] = ausgabe;
        }
};
</code></pre>
<p>Die main-Methode sollte hier nicht wichtig sein. Wenn ich nun speichereGeldausgabe mit korrekten Parametern aufrufe stürzt das Programm ab. Allerdings nur, wenn die letzte Zeile (ausgaben[anzahl-1] = ausgabe) nicht auskommentiert ist. Lasse ich sie weg, stürzt das Programm zumindest nicht ab. Was meint ihr, woran das liegen könnte? Ich seh den Fehler einfach nicht.</p>
<p>Danke und Gruß</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1722853</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1722853</guid><dc:creator><![CDATA[Herr-Vorragend]]></dc:creator><pubDate>Sun, 07 Jun 2009 17:22:12 GMT</pubDate></item><item><title><![CDATA[Reply to Absturz bei realloc() on Sun, 07 Jun 2009 17:22:59 GMT]]></title><description><![CDATA[<p><code>malloc</code> und <code>realloc</code> sind C. Du willst C++ programmieren, also nimmst am besten <code>std::vector</code> . Bei den Artikeln auf <a href="http://c-plusplus.net" rel="nofollow">c-plusplus.net</a> gibt es eine Einführung in STL-Container, lies die mal!</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1722854</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1722854</guid><dc:creator><![CDATA[Registrierter Troll]]></dc:creator><pubDate>Sun, 07 Jun 2009 17:22:59 GMT</pubDate></item><item><title><![CDATA[Reply to Absturz bei realloc() on Sun, 07 Jun 2009 17:23:59 GMT]]></title><description><![CDATA[<p>Okay, danke für den Tipp. Aber angenommen ich habe C, wo liegt der Fehler? <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/1722855</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1722855</guid><dc:creator><![CDATA[Herr-Vorragend]]></dc:creator><pubDate>Sun, 07 Jun 2009 17:23:59 GMT</pubDate></item><item><title><![CDATA[Reply to Absturz bei realloc() on Sun, 07 Jun 2009 17:27:06 GMT]]></title><description><![CDATA[<p>Herr-Vorragend schrieb:</p>
<blockquote>
<p>Okay, danke für den Tipp. Aber angenommen ich habe C, wo liegt der Fehler? <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>In C gibt es kein <code>class</code> , keine Kon- und Destruktoren.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1722858</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1722858</guid><dc:creator><![CDATA[Registrierter Troll]]></dc:creator><pubDate>Sun, 07 Jun 2009 17:27:06 GMT</pubDate></item><item><title><![CDATA[Reply to Absturz bei realloc() on Sun, 07 Jun 2009 17:27:35 GMT]]></title><description><![CDATA[<p>Herr-Vorragend schrieb:</p>
<blockquote>
<p>Okay, danke für den Tipp. Aber angenommen ich habe C, wo liegt der Fehler? <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>Da muß ich wohl eine Überweisung an die Fachleute machen..</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1722859</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1722859</guid><dc:creator><![CDATA[volkard]]></dc:creator><pubDate>Sun, 07 Jun 2009 17:27:35 GMT</pubDate></item><item><title><![CDATA[Reply to Absturz bei realloc() on Sun, 07 Jun 2009 17:28:00 GMT]]></title><description><![CDATA[<p>Dieser Thread wurde von Moderator/in <a href="http://www.c-plusplus.net/forum/profile-var-mode-is-viewprofile-and-u-is-5.html" rel="nofollow">volkard</a> aus dem Forum <a href="http://www.c-plusplus.net/forum/viewforum-var-f-is-15.html" rel="nofollow">C++</a> in das Forum <a href="http://www.c-plusplus.net/forum/viewforum-var-f-is-10.html" rel="nofollow">ANSI C</a> verschoben.</p>
<p>Im Zweifelsfall bitte auch folgende Hinweise beachten:<br />
<a href="http://www.c-plusplus.net/forum/viewtopic-var-t-is-39405.html" rel="nofollow">C/C++ Forum :: FAQ - Sonstiges :: Wohin mit meiner Frage?</a></p>
<p><em>Dieses Posting wurde automatisch erzeugt.</em></p>
]]></description><link>https://www.c-plusplus.net/forum/post/1722861</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1722861</guid><dc:creator><![CDATA[C++ Forumbot]]></dc:creator><pubDate>Sun, 07 Jun 2009 17:28:00 GMT</pubDate></item><item><title><![CDATA[Reply to Absturz bei realloc() on Sun, 07 Jun 2009 17:29:13 GMT]]></title><description><![CDATA[<p>Okay, nun bin ich auf Vektoren umgestiegen:</p>
<pre><code>class GeldausgabenVerwaltung
{
    private:
        vector&lt;Geldausgabe&gt; ausgaben;
        int anzahl;
    public:
        GeldausgabenVerwaltung()
        {
            anzahl = 0;
            //ausgaben = (Geldausgabe*)malloc(100*sizeof(Geldausgabe));
        }
        ~GeldausgabenVerwaltung()
        {
            //free(ausgaben);
        }    
        void speichereGeldausgabe(int tag, int monat, float summe, string grund)
        {
            Geldausgabe ausgabe;

            ausgabe.setTag(tag);
            ausgabe.setMonat(monat);
            ausgabe.setSumme(summe);
            ausgabe.setGrund(grund);

            anzahl++;
            //ausgaben = (Geldausgabe*)realloc(ausgaben, anzahl*sizeof(Geldausgabe));
            ausgaben[anzahl-1] = ausgabe;
        }
}
</code></pre>
<p>Und dennonch stürzt das Programm in der gleichen Zeile ab. Anscheinend mache ich einen ziemlich dummen Anfängerfehler, aber welchen? <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/1722864</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1722864</guid><dc:creator><![CDATA[Herr-Vorragend]]></dc:creator><pubDate>Sun, 07 Jun 2009 17:29:13 GMT</pubDate></item><item><title><![CDATA[Reply to Absturz bei realloc() on Sun, 07 Jun 2009 17:37:01 GMT]]></title><description><![CDATA[<p>Herr-Vorragend schrieb:</p>
<blockquote>
<p>Okay, nun bin ich auf Vektoren umgestiegen:</p>
</blockquote>
<p>Gute Entscheidung, leider ist dein Beitrag jetzt wieder im falschen Forum <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>Und dennonch stürzt das Programm in der gleichen Zeile ab. Anscheinend mache ich einen ziemlich dummen Anfängerfehler, aber welchen? <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>Ersetze</p>
<pre><code class="language-cpp">ausgaben[anzahl-1] = ausgabe;
</code></pre>
<p>durch</p>
<pre><code class="language-cpp">ausgaben.push_back(ausgabe);
</code></pre>
<p>und <code>anzahl</code> kann weg, dafür gibts <code>ausgaben.size()</code> ;</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1722873</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1722873</guid><dc:creator><![CDATA[Registrierter Troll]]></dc:creator><pubDate>Sun, 07 Jun 2009 17:37:01 GMT</pubDate></item><item><title><![CDATA[Reply to Absturz bei realloc() on Sun, 07 Jun 2009 17:40:22 GMT]]></title><description><![CDATA[<p>Ja, da wurde ich wohl zu schnell verschoben <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>
<p>Aber danke, nun klappt es.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1722877</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1722877</guid><dc:creator><![CDATA[Herr-Vorragend]]></dc:creator><pubDate>Sun, 07 Jun 2009 17:40:22 GMT</pubDate></item><item><title><![CDATA[Reply to Absturz bei realloc() on Sun, 07 Jun 2009 19:28:44 GMT]]></title><description><![CDATA[<p>Dieser Thread wurde von Moderator/in <a href="http://www.c-plusplus.net/forum/profile-var-mode-is-viewprofile-and-u-is-1819.html" rel="nofollow">rüdiger</a> aus dem Forum <a href="http://www.c-plusplus.net/forum/viewforum-var-f-is-10.html" rel="nofollow">ANSI C</a> in das Forum <a href="http://www.c-plusplus.net/forum/viewforum-var-f-is-15.html" rel="nofollow">C++</a> verschoben.</p>
<p>Im Zweifelsfall bitte auch folgende Hinweise beachten:<br />
<a href="http://www.c-plusplus.net/forum/viewtopic-var-t-is-39405.html" rel="nofollow">C/C++ Forum :: FAQ - Sonstiges :: Wohin mit meiner Frage?</a></p>
<p><em>Dieses Posting wurde automatisch erzeugt.</em></p>
]]></description><link>https://www.c-plusplus.net/forum/post/1722950</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1722950</guid><dc:creator><![CDATA[C++ Forumbot]]></dc:creator><pubDate>Sun, 07 Jun 2009 19:28:44 GMT</pubDate></item></channel></rss>