<?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[free(pointer) nach returnen des pointers]]></title><description><![CDATA[<p>Hallo!</p>
<p>Ich habe mir eine Funktion gebastelt, die einen String in einen anderen einfügen kann - das funktioniert prächtig - doch stoße dabei auf ein Problem. Hier mal kurz zur Veranschaulichung ein geeignetes Code-Beispiel:</p>
<pre><code class="language-cpp">extern char* weldstr(int ascii); //Deklaration

char* somestr(int ascii) { //Definition
   *char c = (char*)malloc(sizeof(char)*2);
   *char = ascii;
   *(char+1) = '\0';
   return c;
}

//Irgendwo anders im code wird die Funktion somestr aufgerufen:
char das_a[2];
// Gib mir ein 'A'!
strcpy(das_a, somestr(65)); //65 = ASCII-Dezimalzahl für 'A'
</code></pre>
<p>Das Problem: Der mit malloc() angeforderte Speicher wird nicht wieder freigegeben.<br />
Das richtige Problem: Mir fällt kein Weg ein wie man das Problem SCHÖN lösen kann. Ich könnte nun eine globale Variable machen, die sich den Pointer für mich merkt, und nach dem Aufruf von somestr() diesen freeen mit free(). Das unschöne daran ist, dass ich dann jedes mal manuell freeen müsste. Da frag ich mich, ob man das nicht besser irgendwie irgendwo verbauen kann. Unmittelbar vor dem return ist nämlich leider zu früh, wie ich feststellen musste. Der char* war schon nicht mehr der selbe. Ideen? :o</p>
<p>Danke im Vorraus,</p>
<p>Silencer.</p>
]]></description><link>https://www.c-plusplus.net/forum/topic/244686/free-pointer-nach-returnen-des-pointers</link><generator>RSS for Node</generator><lastBuildDate>Sat, 19 Sep 2026 04:03:18 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/244686.rss" rel="self" type="application/rss+xml"/><pubDate>Fri, 03 Jul 2009 21:49:02 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to free(pointer) nach returnen des pointers on Fri, 03 Jul 2009 21:49:02 GMT]]></title><description><![CDATA[<p>Hallo!</p>
<p>Ich habe mir eine Funktion gebastelt, die einen String in einen anderen einfügen kann - das funktioniert prächtig - doch stoße dabei auf ein Problem. Hier mal kurz zur Veranschaulichung ein geeignetes Code-Beispiel:</p>
<pre><code class="language-cpp">extern char* weldstr(int ascii); //Deklaration

char* somestr(int ascii) { //Definition
   *char c = (char*)malloc(sizeof(char)*2);
   *char = ascii;
   *(char+1) = '\0';
   return c;
}

//Irgendwo anders im code wird die Funktion somestr aufgerufen:
char das_a[2];
// Gib mir ein 'A'!
strcpy(das_a, somestr(65)); //65 = ASCII-Dezimalzahl für 'A'
</code></pre>
<p>Das Problem: Der mit malloc() angeforderte Speicher wird nicht wieder freigegeben.<br />
Das richtige Problem: Mir fällt kein Weg ein wie man das Problem SCHÖN lösen kann. Ich könnte nun eine globale Variable machen, die sich den Pointer für mich merkt, und nach dem Aufruf von somestr() diesen freeen mit free(). Das unschöne daran ist, dass ich dann jedes mal manuell freeen müsste. Da frag ich mich, ob man das nicht besser irgendwie irgendwo verbauen kann. Unmittelbar vor dem return ist nämlich leider zu früh, wie ich feststellen musste. Der char* war schon nicht mehr der selbe. Ideen? :o</p>
<p>Danke im Vorraus,</p>
<p>Silencer.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1736900</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1736900</guid><dc:creator><![CDATA[Silencer4252]]></dc:creator><pubDate>Fri, 03 Jul 2009 21:49:02 GMT</pubDate></item><item><title><![CDATA[Reply to free(pointer) nach returnen des pointers on Fri, 03 Jul 2009 21:54:37 GMT]]></title><description><![CDATA[<p>Ich seh da kein bisschen ++ nach dem C :p</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1736901</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1736901</guid><dc:creator><![CDATA[Nachtscherz]]></dc:creator><pubDate>Fri, 03 Jul 2009 21:54:37 GMT</pubDate></item><item><title><![CDATA[Reply to free(pointer) nach returnen des pointers on Fri, 03 Jul 2009 22:17:27 GMT]]></title><description><![CDATA[<p>Hm... na gut stimmt. ^^<br />
Sollte aber jetzt nicht dramatisch sein (bitte). :p<br />
Ich merk grade ich hab etwas scheisse geschrieben im Code.<br />
Was man nicht alles tut um 0 Uhr.<br />
Also nochmal in richtig:</p>
<pre><code class="language-cpp">extern char* weldstr(int ascii); //Deklaration

char* somestr(int ascii) { //Definition
   *char c = (char*)malloc(sizeof(char)*2);
   *c = ascii; //Hier stand char statt c
   *(c+1) = '\0'; //Hier auch xD
   return c;
}

//Irgendwo anders im code wird die Funktion somestr aufgerufen:
char das_a[2];
// Gib mir ein 'A'!
strcpy(das_a, somestr(65)); //65 = ASCII-Dezimalzahl für 'A'
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/1736906</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1736906</guid><dc:creator><![CDATA[Silencer4252]]></dc:creator><pubDate>Fri, 03 Jul 2009 22:17:27 GMT</pubDate></item><item><title><![CDATA[Reply to free(pointer) nach returnen des pointers on Fri, 03 Jul 2009 22:42:29 GMT]]></title><description><![CDATA[<p>Soll dir jetzt jemand die Geschichte von std::string, auto_ptr und/oder RAII erzählen (C++) oder willst du doch eher nach ANSI C verschoben werden?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1736913</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1736913</guid><dc:creator><![CDATA[erneut_]]></dc:creator><pubDate>Fri, 03 Jul 2009 22:42:29 GMT</pubDate></item><item><title><![CDATA[Reply to free(pointer) nach returnen des pointers on Fri, 03 Jul 2009 22:58:16 GMT]]></title><description><![CDATA[<p>Tuh was du für richtig hälst. |:cX<br />
(verschieb mich eben ^^)</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1736915</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1736915</guid><dc:creator><![CDATA[Silencer4252]]></dc:creator><pubDate>Fri, 03 Jul 2009 22:58:16 GMT</pubDate></item><item><title><![CDATA[Reply to free(pointer) nach returnen des pointers on Fri, 03 Jul 2009 23:17:15 GMT]]></title><description><![CDATA[<p>Warum überhaupt den Speicher dynamisch allozieren, wenn die Größe doch konstant ist?;p</p>
<pre><code class="language-cpp">char* create_string(char ascii)
{
	static char buf[2];

	buf[0] = ascii;
	buf[1] = '\0';

	return buf;
}
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/1736917</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1736917</guid><dc:creator><![CDATA[Chiffre]]></dc:creator><pubDate>Fri, 03 Jul 2009 23:17:15 GMT</pubDate></item><item><title><![CDATA[Reply to free(pointer) nach returnen des pointers on Fri, 03 Jul 2009 23:35:48 GMT]]></title><description><![CDATA[<p>Weil darum:</p>
<p>error C2057: expected constant expression<br />
error C2466: cannot allocate an array of constant size 0<br />
error C2133: 'newStr' : unknown size<br />
warning C4172: returning address of local variable or temporary</p>
<p>Frag mich nicht wie er auf 0 kommt. Hier mal das ganze mit Array statt pointer.</p>
<pre><code class="language-cpp">//Inserts one string into another for a length of maxNewLen chars at position
//insAtPos. If overwrite is true, inserted String will overwrite source string
//from position insAtPos onwards.
const char* strins(const char* srcStr, const int maxNewLen, const char* insStr, const int insAtPos, const bool overwrite) {
	//srcStr is the string in which we want to insert another string
	//maxNewLen is the maximum length in chars of the resulting string
	//insStr is the string to be inserted
	//insAtPos indicates the position where to insert insStr into srcStr
	//overwrite indicates whether to actually insert into or to overwrite srcStr at/from insAtPos with insStr
	char newStr[maxNewLen+1]; //Create newStr to deposit new string in
	int newCurPos = 0; //Counter for position
	int srcCurPos = 0; //Counter for position
	int insCurPos = 0; //Counter for position
	int srcLen = (int)strlen(srcStr); //Amount of chars before '\0'
	int insLen = (int)strlen(insStr); //Amount of chars before '\0'
	while(newCurPos &lt; maxNewLen) { //While not exceeding maxNewLen
		if(newCurPos &lt; insAtPos &amp;&amp; srcCurPos &lt; srcLen) { //If we are before the position to insert string at AND the source string has not already copied entirely
			newStr[newCurPos] = *(srcStr+srcCurPos); //Copy the char from/to current position
			       newCurPos++;          srcCurPos++; //Increase positions by one
		} else { //Else
			if(newCurPos &lt; insAtPos - 1) { //If there is a gap of over 0 '\0's between current position of the new string and the position where to insert insStr
				return newStr; //Return the string as far as it has been processed (should not randomly fill the gap)
			}
			break; //Exit the while-loop
		}
	}
	while(newCurPos &lt; maxNewLen) { //Enter while-loop with same condition (doing this to workaround predictable if-checks in previous loop -&gt; increase algorithm speed)
		if(insCurPos &lt; insLen) { //If insStr has unprocessed chars
			newStr[newCurPos] = *(insStr+insCurPos); //Copy them into newStr
			       newCurPos++;          insCurPos++; //Increase positions by one
			if(overwrite) srcCurPos++; //If srcStr shall be overwritten, increase srcCurPos to skip the corresponding char
		} else { //Else
			if(srcCurPos &lt; srcLen) { //If srcStr has unprocessed chars
				newStr[newCurPos] = *(srcStr+srcCurPos); //Copy them into newStr
				       newCurPos++;          srcCurPos++; //Increase positions by one
			} else { //Else
				break; //Exit the while-loop
			}
		}
	}
	newStr[newCurPos] = '\0'; //Add null-terminator
	return newStr; //Return the fully processed, new string
}
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/1736918</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1736918</guid><dc:creator><![CDATA[Silencer4252]]></dc:creator><pubDate>Fri, 03 Jul 2009 23:35:48 GMT</pubDate></item><item><title><![CDATA[Reply to free(pointer) nach returnen des pointers on Sat, 04 Jul 2009 00:00:03 GMT]]></title><description><![CDATA[<p>Silencer4252 schrieb:</p>
<blockquote>
<pre><code class="language-cpp">...
</code></pre>
</blockquote>
<p>Ich wollte grad ins Bett... Das geht jetz nich, weil ich nach dem Post Code-Alpträume bekomme. <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f62e.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--face_with_open_mouth"
      title=":open_mouth:"
      alt="😮"
    /></p>
]]></description><link>https://www.c-plusplus.net/forum/post/1736925</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1736925</guid><dc:creator><![CDATA[pumuckl]]></dc:creator><pubDate>Sat, 04 Jul 2009 00:00:03 GMT</pubDate></item><item><title><![CDATA[Reply to free(pointer) nach returnen des pointers on Sat, 04 Jul 2009 00:34:40 GMT]]></title><description><![CDATA[<p>Okay, hab jetzt ein &quot;Quick'n'Dirty&quot;-Lösung gefunden.<br />
Gebe einfach eine absolute maximale Länge const int MAX_KEY = 4096 vor, und alles klappt wunderbar. <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="🙂"
    /><br />
Und wovon gibts da Alpträume zu bekommen? x3 Egal. Hier die funktionierende Version:</p>
<pre><code class="language-cpp">//Inserts one string into another for a length of maxNewLen chars at position
//insAtPos. If overwrite is true, inserted String will overwrite source string
//from position insPos onwards.
const char* strins(const char* srcStr, const int maxNewLen, const char* insStr, const int insAtPos, const bool overwrite) {
	//srcStr is the string in which we want to insert another string
	//maxNewLen is the maximum length in chars of the resulting string
	//insStr is the string to be inserted
	//insAtPos indicates the position where to insert insStr into srcStr
	//overwrite indicates whether to actually insert into or to overwrite srcStr at/from insAtPos with insStr
	//char* newStr = (char*)malloc(sizeof(char)*(maxNewLen+1)); //Allocate sufficient memory
	if(maxNewLen &gt;= MAX_KEY)
		return 0;
	char* newStr = new char[MAX_KEY];
	int newCurPos = 0; //Counter for position
	int srcCurPos = 0; //Counter for position
	int insCurPos = 0; //Counter for position
	int srcLen = (int)strlen(srcStr); //Amount of chars before '\0'
	int insLen = (int)strlen(insStr); //Amount of chars before '\0'
	while(newCurPos &lt; maxNewLen) { //While not exceeding maxNewLen
		if(newCurPos &lt; insAtPos &amp;&amp; srcCurPos &lt; srcLen) { //If we are before the position to insert string at AND the source string has not already copied entirely
			*(newStr+newCurPos) = *(srcStr+srcCurPos); //Copy the char from/to current position
			         newCurPos++;          srcCurPos++; //Increase positions by one
		} else { //Else
			if(newCurPos &lt; insAtPos - 1) { //If there is a gap of over 0 '\0's between current position of the new string and the position where to insert insStr
				return newStr; //Return the string as far as it has been processed (should not randomly fill the gap)
			}
			break; //Exit the while loop
		}
	}
	while(newCurPos &lt; maxNewLen) { //Enter while-loop with same condition (doing this to workaround predictable if-checks in previous loop -&gt; increase algorithm speed)
		if(insCurPos &lt; insLen) { //If insStr has unprocessed chars
			*(newStr+newCurPos) = *(insStr+insCurPos); //Copy them into newStr
					 newCurPos++;          insCurPos++; //Increase positions by one
			if(overwrite) srcCurPos++; //If srcStr shall be overwritten, increase srcCurPos to skip the corresponding char
		} else { //Else
			if(srcCurPos &lt; srcLen) { //If srcStr has unprocessed chars
				*(newStr+newCurPos) = *(srcStr+srcCurPos); //Copy them into newStr
				         newCurPos++;          srcCurPos++; //Increase positions by one
			} else { //Else
				break; //Exit the while-loop
			}
		}
	}
	*(newStr+newCurPos) = '\0'; //Add null-terminator
	return newStr; //Return the fully processed, new string
}
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/1736930</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1736930</guid><dc:creator><![CDATA[Silencer4252]]></dc:creator><pubDate>Sat, 04 Jul 2009 00:34:40 GMT</pubDate></item><item><title><![CDATA[Reply to free(pointer) nach returnen des pointers on Sat, 04 Jul 2009 05:55:55 GMT]]></title><description><![CDATA[<p>Ich verstehe dein Problem gar nicht.</p>
<p>Silencer4252 schrieb:</p>
<blockquote>
<pre><code class="language-cpp">extern char* weldstr(int ascii); //Deklaration

char* somestr(int ascii) { //Definition
   *char c = (char*)malloc(sizeof(char)*2);
   *c = ascii; //Hier stand char statt c
   *(c+1) = '\0'; //Hier auch xD
   return c;
}

//Irgendwo anders im code wird die Funktion somestr aufgerufen:
char das_a[2];
// Gib mir ein 'A'!
strcpy(das_a, somestr(65)); //65 = ASCII-Dezimalzahl für 'A'
</code></pre>
</blockquote>
<p>Du willst den Speicherplatz wieder freigeben. Warum machst du das nicht direkt nach deinem Funtkionsaufruf?</p>
<pre><code class="language-cpp">// Irgendwo anders im code...
char* tmp_ptr = somestr(65);
strcpy(das_a, tmp_ptr);
free (tmp_ptr);
</code></pre>
<p>Außerdem verstehe ich auch nicht, warum du nicht Chriffre's Lösung nimmst, was ist daran auszusetzen?</p>
<p>Grüße</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1736939</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1736939</guid><dc:creator><![CDATA[Kronos_m]]></dc:creator><pubDate>Sat, 04 Jul 2009 05:55:55 GMT</pubDate></item><item><title><![CDATA[Reply to free(pointer) nach returnen des pointers on Sat, 04 Jul 2009 08:29:40 GMT]]></title><description><![CDATA[<p>erneut_ schrieb:</p>
<blockquote>
<p>Soll dir jetzt jemand die Geschichte von std::string, auto_ptr und/oder RAII erzählen (C++) oder willst du doch eher nach ANSI C verschoben werden?</p>
</blockquote>
<p>Silencer4252 schrieb:</p>
<blockquote>
<p>Tuh was du für richtig hälst. |:cX<br />
(verschieb mich eben ^^)</p>
</blockquote>
<p>Das waren gute Hinweise, die erneut_ gebracht hat. Recherchiere nach Kapselung, <code>std::string</code> und RAII. Da werden Sie geholfen.</p>
<p>Selbst in C kapselt man die Speicherverwaltung für dynamische Zeichenketten und hantiert nicht direkt mit char* herum. Der Unterschied ist nur, dass sich eine solche Kapselung in C jeder selbst bastelt während es sie in C++ schon vorgefertigt gibt: <code>std::string</code> .</p>
<p>Gruß,<br />
SP</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1736960</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1736960</guid><dc:creator><![CDATA[Sebastian Pizer]]></dc:creator><pubDate>Sat, 04 Jul 2009 08:29:40 GMT</pubDate></item><item><title><![CDATA[Reply to free(pointer) nach returnen des pointers on Sat, 04 Jul 2009 10:10:38 GMT]]></title><description><![CDATA[<p>Also, das Beispiel mit somestr() hab ich nur zur Vereinfachung gemacht, um jedem<br />
das Verstehen der eigentlichen Funktion strins, beim Schreiben welcher ich auf<br />
das Problem gestoßen bin, zu ersparen.</p>
<p>Das Beispiel war in so fern blöd, weil in diesem die array-Größe bekannt ist.<br />
Im Fall von strins() ist das ja nicht so, es sei denn man gibt ein absolutes<br />
maximum der Länge der Zeichenkette vor - schön ist das aber auch nicht.</p>
<p>Natürlich kann ich das freeen direkt nach dem Funktionsaufruf machen, aber diese<br />
Lösung ist in so fern unpraktisch, dass man dann bei häufigen Aufrufen von strins()<br />
oder was auch immer für einer Funktion immer und immer wieder &quot;manuell&quot; den pointer<br />
freeen muss.</p>
<p>Ich informier mich mal wegen Kapselunf, std::string und RAII. Danke. :o</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1736989</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1736989</guid><dc:creator><![CDATA[Silencer4252]]></dc:creator><pubDate>Sat, 04 Jul 2009 10:10:38 GMT</pubDate></item></channel></rss>