<?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[Automatische Speicherreservierung von Strukturen]]></title><description><![CDATA[<p>Hi,</p>
<p>ich möchte eine Funktion schreiben, in der ich automatisch Speicher für verschiedene Strukturen reservieren möchte.<br />
Jetzt habe ich nur das Prob, wie übergebe ich verschiedene Strukturen in diese Funktion.</p>
<p>Hier die Funktion:</p>
<pre><code>struck_gfx2d				*gfx2d; //global
struck_planeten				*planeten;
void speicherverwaltung_funktion::add(int anzahl)
{
struck_gfx2d *tmp_obj = new struck_gfx2d[gfx2d[0].anzahl+anzahl];
for(unsigned int i2 = 0; i2 &lt; gfx2d[0].anzahl; ++i2)
{
	tmp_obj[i2] = gfx2d[i2]; 
}
delete [] gfx2d;
gfx2d = NULL;
gfx2d = new struck_gfx2d[i2+anzahl];
for(i2 = 0; i2 &lt; tmp_obj[0].anzahl+anzahl; ++i2)
{
	gfx2d[i2] = tmp_obj[i2];
}
delete [] tmp_obj;
tmp_obj = NULL;
gfx2d[0].anzahl = i2;
}
</code></pre>
<p>Jetzt möchte ich das aber so haben, das ich die Funktion so aufrufen kann das die Struktur gleich mitgebe.<br />
So wie sie oben steht, kann ich ja nur &quot;struck_gfx2d&quot; bearbeiten.<br />
Ich möchte aber eine Funktion wo ich &quot;struck_gfx2d&quot; und &quot;struck_planeten&quot; Speicher reservieren kann.<br />
Sprich, ich muss der Funktion die Struktur übergeben, ich habe aber keine Ahnung wie.</p>
]]></description><link>https://www.c-plusplus.net/forum/topic/123342/automatische-speicherreservierung-von-strukturen</link><generator>RSS for Node</generator><lastBuildDate>Sun, 23 Aug 2026 14:58:51 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/123342.rss" rel="self" type="application/rss+xml"/><pubDate>Sat, 15 Oct 2005 17:00:56 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to Automatische Speicherreservierung von Strukturen on Sat, 15 Oct 2005 17:00:56 GMT]]></title><description><![CDATA[<p>Hi,</p>
<p>ich möchte eine Funktion schreiben, in der ich automatisch Speicher für verschiedene Strukturen reservieren möchte.<br />
Jetzt habe ich nur das Prob, wie übergebe ich verschiedene Strukturen in diese Funktion.</p>
<p>Hier die Funktion:</p>
<pre><code>struck_gfx2d				*gfx2d; //global
struck_planeten				*planeten;
void speicherverwaltung_funktion::add(int anzahl)
{
struck_gfx2d *tmp_obj = new struck_gfx2d[gfx2d[0].anzahl+anzahl];
for(unsigned int i2 = 0; i2 &lt; gfx2d[0].anzahl; ++i2)
{
	tmp_obj[i2] = gfx2d[i2]; 
}
delete [] gfx2d;
gfx2d = NULL;
gfx2d = new struck_gfx2d[i2+anzahl];
for(i2 = 0; i2 &lt; tmp_obj[0].anzahl+anzahl; ++i2)
{
	gfx2d[i2] = tmp_obj[i2];
}
delete [] tmp_obj;
tmp_obj = NULL;
gfx2d[0].anzahl = i2;
}
</code></pre>
<p>Jetzt möchte ich das aber so haben, das ich die Funktion so aufrufen kann das die Struktur gleich mitgebe.<br />
So wie sie oben steht, kann ich ja nur &quot;struck_gfx2d&quot; bearbeiten.<br />
Ich möchte aber eine Funktion wo ich &quot;struck_gfx2d&quot; und &quot;struck_planeten&quot; Speicher reservieren kann.<br />
Sprich, ich muss der Funktion die Struktur übergeben, ich habe aber keine Ahnung wie.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/892954</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/892954</guid><dc:creator><![CDATA[BlackWolf]]></dc:creator><pubDate>Sat, 15 Oct 2005 17:00:56 GMT</pubDate></item><item><title><![CDATA[Reply to Automatische Speicherreservierung von Strukturen on Sat, 15 Oct 2005 17:03:15 GMT]]></title><description><![CDATA[<p>Entweder als Pointer auf einen Pointer, oder antürlich viel besser: Eine Klasse bei der du im Konstruktor (oder in einer anderen Methode) den Speicherreservierst und dann wieder im Destruktor frei gibst.</p>
<p>Gruß</p>
]]></description><link>https://www.c-plusplus.net/forum/post/892955</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/892955</guid><dc:creator><![CDATA[FireFlow]]></dc:creator><pubDate>Sat, 15 Oct 2005 17:03:15 GMT</pubDate></item><item><title><![CDATA[Reply to Automatische Speicherreservierung von Strukturen on Sat, 15 Oct 2005 17:06:46 GMT]]></title><description><![CDATA[<p>Für ein kleines Beispiel wäre ich sehr dankbar <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/892958</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/892958</guid><dc:creator><![CDATA[BlackWolf]]></dc:creator><pubDate>Sat, 15 Oct 2005 17:06:46 GMT</pubDate></item><item><title><![CDATA[Reply to Automatische Speicherreservierung von Strukturen on Sat, 15 Oct 2005 17:11:54 GMT]]></title><description><![CDATA[<p>Ich machs mal mit int, macht ja kein Unterschied.</p>
<pre><code class="language-cpp">void holMirSpeicher(int** address, size_t size)
{
    *address = new int[size];
}
</code></pre>
<p>Der Nachteil an dieser Methode ist ganz klar dass du den Speicher wieder freigeben muss, das vergisst man sehr sehr sehr schnell. Vor allem wenn man im Team programmiert und keiner den Code vom anderen kennt.</p>
<p>Da ist doch so viel besser:</p>
<pre><code class="language-cpp">class meinKleinerIntSpeicher
{
public:
    meinKleinerIntSpeicher(size_t size) : pointer(new int[size]) { }
    ~meinKleinerIntSpeicher() { delete[] pointer; }
    int get(size_t pos) { return pointer[pos]; }
    void set(size_t pos, int val) { pointer[pos] = val; }
private:
    int* pointer;
};
</code></pre>
<p>Der Speicher wird hier automatisch freigebene wenn das Objekt zerstört wird.</p>
<p>Gruß</p>
]]></description><link>https://www.c-plusplus.net/forum/post/892963</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/892963</guid><dc:creator><![CDATA[FireFlow]]></dc:creator><pubDate>Sat, 15 Oct 2005 17:11:54 GMT</pubDate></item><item><title><![CDATA[Reply to Automatische Speicherreservierung von Strukturen on Sat, 15 Oct 2005 17:23:50 GMT]]></title><description><![CDATA[<pre><code class="language-cpp">struck_gfx2d
*speicherverwaltung_funktion::add (struck_gfx2d *gfx2d, int anzahl) 
{ 
  struck_gfx2d *tmp_obj = new struck_gfx2d[gfx2d[0].anzahl+anzahl]; 

  for(unsigned int i=0; i&lt;gfx2d[0].anzahl; ++i) 
    tmp_obj[i]=gfx2d[i]; 

  delete[] gfx2d; 

  gfx2d           = tmp_obj;
  gfx2d[0].anzahl = i;

  return gfx2d;
}
</code></pre>
<p>an deiner stelle würde ich aber die vector-klasse der stl benutzen. die kann alles, was auch c-arrays können, vergrößert aber ihren speicher bei bedarf automatisch.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/892971</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/892971</guid><dc:creator><![CDATA[Konfusius]]></dc:creator><pubDate>Sat, 15 Oct 2005 17:23:50 GMT</pubDate></item><item><title><![CDATA[Reply to Automatische Speicherreservierung von Strukturen on Sat, 15 Oct 2005 17:39:10 GMT]]></title><description><![CDATA[<p><a class="plugin-mentions-user plugin-mentions-a" href="https://www.c-plusplus.net/forum/uid/9894">@FireFlow</a></p>
<p>Kann sein das ich auf den Kopf gefallen bin, aber zeig mir mal bitte, wie du in deinem Beispiel einen float übergibst. So wie ich es verstehe geht das ja bei dir nur mit int.</p>
<p>Deiner Funktion übergeb ich ja die Adresse eines int, was passiert aber wenn Speicher für einen float reservieren möchte ?</p>
<p><a class="plugin-mentions-user plugin-mentions-a" href="https://www.c-plusplus.net/forum/uid/9573">@Konfusius</a><br />
Ich möchte aber ein und die selbe Funktion für verschiedene Strukturen nutzen können, nicht nur für struck_gfx2d.</p>
<p>Aber du kannst mir zeigen, wie ich folgende Struktur</p>
<pre><code>typedef struct base_TYP
{	
int	anzahl;
char	ressource[100];
} base;
</code></pre>
<p>in einen vector bekomme und dann auf die einzelnen inhalte (anzhal, ressource) zugreifen kann.</p>
<p>Den vector anlegen ist klar<br />
typedef vector&lt;base&gt; ressourcen;</p>
<p>aber wie bekomme ich dann daten wieder aus dem vector heraus ?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/892972</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/892972</guid><dc:creator><![CDATA[BlackWolf]]></dc:creator><pubDate>Sat, 15 Oct 2005 17:39:10 GMT</pubDate></item><item><title><![CDATA[Reply to Automatische Speicherreservierung von Strukturen on Sat, 15 Oct 2005 17:44:11 GMT]]></title><description><![CDATA[<pre><code class="language-cpp">template&lt;class TYPE&gt;
TYPE
*speicherverwaltung_funktion::add (TYPE *array, int anzahl) 
{ 
  TYPE *tmp_obj = new TYPE[array[0].anzahl+anzahl]; 

  for(unsigned int i=0; i&lt;array[0].anzahl; ++i) 
    tmp_obj[i]=array[i];

  tmp_obj[0].anzahl=i;

  delete[] array; 

  return tmp_obj;
}
</code></pre>
<p>das klappt aber nur, wenn alle typen es erlauben, die länge des arrays über array[0].anzahl zu bestimmen. außerdem muß diese funktion dann im header stehen. separate kompilation ist mit templates nämlich nur sehr eingeschränkt möglich.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/892986</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/892986</guid><dc:creator><![CDATA[Konfusius]]></dc:creator><pubDate>Sat, 15 Oct 2005 17:44:11 GMT</pubDate></item><item><title><![CDATA[Reply to Automatische Speicherreservierung von Strukturen on Sat, 15 Oct 2005 18:11:41 GMT]]></title><description><![CDATA[<p>BlackWolf schrieb:</p>
<blockquote>
<p><a class="plugin-mentions-user plugin-mentions-a" href="https://www.c-plusplus.net/forum/uid/9894">@FireFlow</a></p>
<p>Kann sein das ich auf den Kopf gefallen bin, aber zeig mir mal bitte, wie du in deinem Beispiel einen float übergibst. So wie ich es verstehe geht das ja bei dir nur mit int.</p>
</blockquote>
<p>Das war wie gesagt nur beispielhaft. Du kannst wie schon gesagt Templates benutzen, oder eigentlich noch besser (wie auch schon gesagt -lol) ein Container aus der STL.</p>
<p>Gruß</p>
]]></description><link>https://www.c-plusplus.net/forum/post/893007</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/893007</guid><dc:creator><![CDATA[FireFlow]]></dc:creator><pubDate>Sat, 15 Oct 2005 18:11:41 GMT</pubDate></item><item><title><![CDATA[Reply to Automatische Speicherreservierung von Strukturen on Sat, 15 Oct 2005 18:16:15 GMT]]></title><description><![CDATA[<p>Die Funktion geht, aber beim aufruf habe ich probs.</p>
<pre><code>speicherverwaltung_funktion	*v_speicherverwaltung;
struck_ressourcen_base		*ressourcen;
v_speicherverwaltung-&gt;add(ressourcen,1);
</code></pre>
<p>Dann kommt folgende Fehlermeldung:<br />
start.obj : error LNK2001: Nichtaufgeloestes externes Symbol &quot;public: ....&quot;</p>
]]></description><link>https://www.c-plusplus.net/forum/post/893015</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/893015</guid><dc:creator><![CDATA[BlackWolf]]></dc:creator><pubDate>Sat, 15 Oct 2005 18:16:15 GMT</pubDate></item><item><title><![CDATA[Reply to Automatische Speicherreservierung von Strukturen on Sat, 15 Oct 2005 19:54:09 GMT]]></title><description><![CDATA[<p>Dann wär's das beste sich zu überlegen, wie man den Fehler beseitigt.</p>
<p>Bye, TGGC (<a href="http://tggc.tg.funpic.de/index.php?cat=4" rel="nofollow">Demo or Die</a>)</p>
]]></description><link>https://www.c-plusplus.net/forum/post/893077</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/893077</guid><dc:creator><![CDATA[TGGC]]></dc:creator><pubDate>Sat, 15 Oct 2005 19:54:09 GMT</pubDate></item><item><title><![CDATA[Reply to Automatische Speicherreservierung von Strukturen on Sun, 16 Oct 2005 17:51:48 GMT]]></title><description><![CDATA[<p>die funktion ist ein sogenanntes member-template. das muß man in der klassendeklaration speziell deklarieren:</p>
<pre><code class="language-cpp">template&lt;class TYPE&gt;
TYPE *add (TYPE *array, int anzahl);
</code></pre>
<p>außerdem mußt du die implementation der funktion mit in die headerdatei setzten.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/893656</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/893656</guid><dc:creator><![CDATA[Konfusius]]></dc:creator><pubDate>Sun, 16 Oct 2005 17:51:48 GMT</pubDate></item></channel></rss>