<?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[speicher von Char-arrays und andere Variablen wieder &amp;quot;freigeben&amp;quot;]]></title><description><![CDATA[<p>Hallo,</p>
<p>Ich wollte mal fragen wie man Char-arrays und andere variablen wieder Freigeben kann, ständig haben variablen immer die alten werte wenn sie abgefragt werden <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f61e.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--disappointed_face"
      title="=("
      alt="😞"
    /></p>
]]></description><link>https://www.c-plusplus.net/forum/topic/323916/speicher-von-char-arrays-und-andere-variablen-wieder-quot-freigeben-quot</link><generator>RSS for Node</generator><lastBuildDate>Wed, 15 Jul 2026 09:07:45 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/323916.rss" rel="self" type="application/rss+xml"/><pubDate>Sat, 22 Feb 2014 20:31:35 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to speicher von Char-arrays und andere Variablen wieder &amp;quot;freigeben&amp;quot; on Sat, 22 Feb 2014 20:31:35 GMT]]></title><description><![CDATA[<p>Hallo,</p>
<p>Ich wollte mal fragen wie man Char-arrays und andere variablen wieder Freigeben kann, ständig haben variablen immer die alten werte wenn sie abgefragt werden <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f61e.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--disappointed_face"
      title="=("
      alt="😞"
    /></p>
]]></description><link>https://www.c-plusplus.net/forum/post/2384650</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2384650</guid><dc:creator><![CDATA[LipkeGu]]></dc:creator><pubDate>Sat, 22 Feb 2014 20:31:35 GMT</pubDate></item><item><title><![CDATA[Reply to speicher von Char-arrays und andere Variablen wieder &amp;quot;freigeben&amp;quot; on Sat, 22 Feb 2014 20:35:36 GMT]]></title><description><![CDATA[<p>Das läuft jetzt nicht direkt unter &quot;freigeben,&quot; aber schreib halt was rein.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2384651</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2384651</guid><dc:creator><![CDATA[seldon]]></dc:creator><pubDate>Sat, 22 Feb 2014 20:35:36 GMT</pubDate></item><item><title><![CDATA[Reply to speicher von Char-arrays und andere Variablen wieder &amp;quot;freigeben&amp;quot; on Sat, 22 Feb 2014 20:36:59 GMT]]></title><description><![CDATA[<p>Zeig mal deinen Code, reduziert auf ein kompilierbares Minimalbeispiel, damit man sieht was du genau meinst/möchtest.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2384652</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2384652</guid><dc:creator><![CDATA[icarus2]]></dc:creator><pubDate>Sat, 22 Feb 2014 20:36:59 GMT</pubDate></item><item><title><![CDATA[Reply to speicher von Char-arrays und andere Variablen wieder &amp;quot;freigeben&amp;quot; on Sat, 22 Feb 2014 21:01:27 GMT]]></title><description><![CDATA[<pre><code>void Handle_oschoice_request(int con, char* data)
{
	char eop = 0;
	char _name[8];
	string OSC_Data = &quot;&quot;;
	uint32_t type = 0, value = 0;
	int _length = 0, offset = 0;
	char packet[1024];
	int _result;

	_length = (sizeof(data) - 36);

	type = SWAB32(PKT_RSU);
	offset = 0;

	memcpy(&amp;_name,&amp;data[36],_length);

	cout &lt;&lt; &quot;File: &quot; &lt;&lt; _name &lt;&lt; endl;
	if (memcmp(OSC_WELCOME_MESSAGE,&amp;_name,sizeof(OSC_WELCOME_MESSAGE)) == 0)
	{
		OSC_Data = ReadOSCFile(&quot;../OSChooser/welcome.osc&quot;);
	}
	else
	{
		if(memcmp(OSC_LOGIN_FILENAME,&amp;_name,sizeof(OSC_LOGIN_FILENAME)) == 0)
		{
			OSC_Data = ReadOSCFile(&quot;../OSChooser/English/login.osc&quot;);
		}

		if(memcmp(OSC_ERR_4E28_FILENAME,&amp;_name,sizeof(OSC_ERR_4E28_FILENAME)) == 0)
		{
			OSC_Data = ReadOSCFile(&quot;../OSChooser/English/00004e28.osc&quot;);
		}
	}

	memcpy(&amp;packet[0], &amp;type, sizeof(type));
	offset += sizeof(type);

	offset += 4;
	memcpy(&amp;packet[offset], &amp;data[8], 28);

	offset += 28;
	OSC_Data.copy(&amp;packet[offset],OSC_Data.length());

	offset += OSC_Data.length();
	memcpy(&amp;packet[offset], &amp;eop,sizeof(eop));

	value = SWAB32(offset);
	memcpy(&amp;packet[0x4], &amp;value, sizeof(value));

	_result = sendto(con, packet, offset, 0, (struct sockaddr *) &amp;from, sizeof(from));

	if (_result == SOCKET_ERROR)
	{
		fprintf(stderr, &quot;send() failed: Error %d\n&quot;, WSAGetLastError());
	}
	else
	{
		cout &lt;&lt; &quot;sent &quot; &lt;&lt; _result &lt;&lt; &quot; bytes to Client&quot; &lt;&lt; endl;
	}

	OSC_Data = &quot;&quot;;
}
</code></pre>
<p>Nehmen wir als Beispiel &quot;char _name[8];&quot;... Diese ist sozusagen zum Deklarierungs-Zeitpunkt leer (NULL), jetzt verbindet ein Rechner und sendet &quot;\n&quot;, was besagt &quot;zeige mir die Willkommen-seite&quot;. Danach sendet er &quot;LOGIN&quot;, dieser Wert wird in &quot;_name&quot; geschrieben, überprüft und kriegt den passenden Dateiinhalt gesendet.</p>
<p>nun zum Problem, verbindet ein 2ter Rechner, so kriegt er, weil die Variable &quot;_name&quot; noch den alten Wert (&quot;LOGIN&quot;) hat auch die Login-Seite, statt der Willkommens-Seite <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f61e.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--disappointed_face"
      title=":("
      alt="😞"
    /></p>
]]></description><link>https://www.c-plusplus.net/forum/post/2384655</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2384655</guid><dc:creator><![CDATA[LipkeGu]]></dc:creator><pubDate>Sat, 22 Feb 2014 21:01:27 GMT</pubDate></item><item><title><![CDATA[Reply to speicher von Char-arrays und andere Variablen wieder &amp;quot;freigeben&amp;quot; on Sat, 22 Feb 2014 21:02:21 GMT]]></title><description><![CDATA[<p>Das ist so ziemlich komischer Code. Den richtig zu analysieren macht keinen Spaß.</p>
<p>Nehmen wir als Beispiel &quot;char _name[8];&quot;... Diese ist sozusagen zum Deklarierungs-Zeitpunkt leer (NULL), jetzt verbindet ein Rechner und sendet &quot;\n&quot;, was besagt &quot;zeige mir die Willkommen-seite&quot;. Danach sendet er &quot;LOGIN&quot;, dieser Wert wird in &quot;_name&quot; geschrieben, überprüft und kriegt den passenden Dateiinhalt gesendet.</p>
<p>nun zum Problem, verbindet ein 2ter Rechner, so kriegt er, weil die Variable &quot;_name&quot; noch den alten Wert (&quot;LOGIN&quot;) hat auch die Login-Seite, statt der Willkommens-Seite :([/quote]Das ist sehr merkwürdig, was Du da machst. Mal so ein paar Sachen, die spontan auffallen:</p>
<pre><code>int _length = 0, offset = 0;  // _lenght wird mit 0 initialisiert
...

	_length = (sizeof(data) - 36); // _length bekommt den Wert -28 (sizeof(char*)==8 abzüglich 36)
	_length = 0;                   // _length wird wieder zurück auf 0 gesetzt
...
	memcpy(&amp;_name,&amp;_name,_length);  // kopiere 0 Bytes, also gar nichts von _name nach _name  :confused:
</code></pre>
<p>Im übrigen kann _name nie NULL sein, wie Du schreibst. Ein Zeiger kann NULL sein, _name ist aber kein Zeiger sondern ein char array. Da steht immer was drin.</p>
<p>Auch setzt Du am Ende der Funktion den String OSC_Data auf einen Leerstring, obwohl er im nächsten Moment eh seinen Gültigkeitsbereich verlässt und damit gelöscht wird.</p>
<p>Das Du bei so einen Stil nicht mehr durchblickst, wann welche Variable welchen Wert hat, wundert mich nicht. Räum erst mal auf. Dann siehst Du entweder selbst was oder frag hier noch mal.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2384658</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2384658</guid><dc:creator><![CDATA[tntnet]]></dc:creator><pubDate>Sat, 22 Feb 2014 21:02:21 GMT</pubDate></item><item><title><![CDATA[Reply to speicher von Char-arrays und andere Variablen wieder &amp;quot;freigeben&amp;quot; on Sat, 22 Feb 2014 21:03:01 GMT]]></title><description><![CDATA[<p>Du solltest Variablen auch erstmal Werte <strong>zuweisen</strong>, <strong>bevor</strong> du sie benutzt.</p>
<p>Ein</p>
<pre><code>char _name[8] = &quot;&quot;;
</code></pre>
<p>reicht schon.</p>
<p>Noch ein paar Fragen:<br />
Was soll Zeile 11?<br />
<code>sizeof(data)</code> ist meist 4 oder 8, da data ein Zeiger ist.</p>
<p>Was soll Zeile 16.<br />
Bei memcpy dürfen sich die Speicherbereiche nicht überlappen.</p>
<p>Was soll der Adressoperator bei <em>_name</em>?<br />
DEr Arrayname steht schon für die Adresse des ersten Elements.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2384660</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2384660</guid><dc:creator><![CDATA[DirkB]]></dc:creator><pubDate>Sat, 22 Feb 2014 21:03:01 GMT</pubDate></item><item><title><![CDATA[Reply to speicher von Char-arrays und andere Variablen wieder &amp;quot;freigeben&amp;quot; on Sat, 22 Feb 2014 21:41:57 GMT]]></title><description><![CDATA[<p>Ja hatte es noch schnell korrigiert hier der aufgeräumte code ... <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f615.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--confused_face"
      title=":/"
      alt="😕"
    /></p>
<p>und nein, sizeof(data) hat bei mir die länge vom empfangenden Paket (42 Bytes)</p>
<p>Aber um klarheit zuschaffen...</p>
<pre><code>int _recv()
{
	uint32_t retval = 0, type = 0;
	socklen_t fromlen;

	char buffer[1024];

	while (true)
	{
		fromlen = sizeof(from);
		retval = recvfrom(m_socket, buffer, sizeof(buffer), 0, (struct sockaddr *) &amp;from, &amp;fromlen);

		if (retval &lt; 0)
		{
			fprintf(stderr, &quot;recvfrom() failed: error %d\n&quot;, WSAGetLastError());
			continue;
		}

		if (retval == 0)
		{
			cout &lt;&lt; &quot;Client closed connection&quot; &lt;&lt; endl;
			continue;
		}

		cout &lt;&lt; &quot;Received datagram &quot;&lt;&lt; retval &lt;&lt; &quot; bytes from: &quot; &lt;&lt; inet_ntoa(from.sin_addr) &lt;&lt; endl;

		char Data[retval];

		memcpy(&amp;Data, buffer, retval);
		memcpy(&amp;type, Data, sizeof(type));

		switch (SWAB32(type))
		{
			case PKT_NCQ:
			{
				cout &lt;&lt; &quot;NIC Card query&quot; &lt;&lt; endl;

				Handle_driver_request(m_socket,Data,&amp;fromlen);
				break;
			}
			case BOOTP_MCO:
			{
				cout &lt;&lt; &quot;Boot file detection (over DHCP)&quot; &lt;&lt; endl;

				Handle_BootFile_request(m_socket,Data, &amp;fromlen);
				break;
			}
			case PKT_RQU:	/* Handle OSC file Request*/
			{
				cout &lt;&lt; &quot;OSChooser File request&quot; &lt;&lt; endl;

				Handle_oschoice_request(m_socket,Data);
				break;
			}
			default:
			{
				break;
			}
		}
	}

	closesocket(m_socket);
	WSACleanup();
	return errno;
}
</code></pre>
<pre><code>void Handle_oschoice_request(int con, char* data)
{
	char eop = 0;
	char _name[8] = &quot;&quot;;

	string OSC_Data = &quot;&quot;;
	uint32_t type = SWAB32(PKT_RSU), value = 0;

	int _length = (sizeof(data) - 36), offset = 0; /* _length = länge von Data - 36 = Länge des Dateinamen... */
	char packet[1024];
	int _result = 0;

	memcpy(&amp;_name, &amp;data[36], _length); /* 36 ist die stelle wo der Dateiname anfängt */

	cout &lt;&lt; &quot;File: &quot; &lt;&lt; _name &lt;&lt; endl;

	if (memcmp(OSC_WELCOME_MESSAGE,&amp;data[36],sizeof(OSC_WELCOME_MESSAGE)) == 0)
	{
		OSC_Data = ReadOSCFile(&quot;../OSChooser/welcome.osc&quot;);
	}
	else
	{
		if(memcmp(OSC_LOGIN_FILENAME,&amp;data[36],sizeof(OSC_LOGIN_FILENAME)) == 0)
		{
			OSC_Data = ReadOSCFile(&quot;../OSChooser/English/login.osc&quot;);
		}

		if(memcmp(OSC_ERR_4E28_FILENAME,&amp;data[36],sizeof(OSC_ERR_4E28_FILENAME)) == 0)
		{
			OSC_Data = ReadOSCFile(&quot;../OSChooser/English/00004e28.osc&quot;);
		}
	}

	memcpy(&amp;packet[0], &amp;type, sizeof(type));
	offset += sizeof(type);

	offset += 4;
	memcpy(&amp;packet[offset], &amp;data[8], 28);

	offset += 28;
	OSC_Data.copy(&amp;packet[offset],OSC_Data.length());

	offset += OSC_Data.length();
	memcpy(&amp;packet[offset], &amp;eop,sizeof(eop));

	value = SWAB32(offset);
	memcpy(&amp;packet[0x4], &amp;value, sizeof(value));

	_result = sendto(con, packet, offset, 0, (struct sockaddr *) &amp;from, sizeof(from));

	if (_result == SOCKET_ERROR)
	{
		fprintf(stderr, &quot;send() failed: Error %d\n&quot;, WSAGetLastError());
	}
	else
	{
		cout &lt;&lt; &quot;sent &quot; &lt;&lt; _result &lt;&lt; &quot; bytes to Client&quot; &lt;&lt; endl;
	}
}
</code></pre>
<p>gut da meinte einer was von fragen, denn erklärt mir mal bitte jemand den unterschied zwischen:</p>
<pre><code>memcpy(&amp;Data, buffer, retval);
 memcpy(&amp;type, Data, sizeof(type));
</code></pre>
<p>und dem:</p>
<pre><code>memcpy(&amp;Data, &amp;buffer, retval);
memcpy(&amp;type, &amp;Data, sizeof(type));
</code></pre>
<p><img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f61e.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--disappointed_face"
      title=":("
      alt="😞"
    /></p>
]]></description><link>https://www.c-plusplus.net/forum/post/2384664</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2384664</guid><dc:creator><![CDATA[LipkeGu]]></dc:creator><pubDate>Sat, 22 Feb 2014 21:41:57 GMT</pubDate></item><item><title><![CDATA[Reply to speicher von Char-arrays und andere Variablen wieder &amp;quot;freigeben&amp;quot; on Sat, 22 Feb 2014 21:38:41 GMT]]></title><description><![CDATA[<p>LipkeGu schrieb:</p>
<blockquote>
<p>und nein, sizeof(data) hat bei mir die länge vom empfangenden Paket (42 Bytes)</p>
</blockquote>
<p>Bestimmt nicht. Gib mal sizeof(data) aus, dann wirst du sehen, dass sizeof(data) != 42 ist.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2384665</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2384665</guid><dc:creator><![CDATA[icarus2]]></dc:creator><pubDate>Sat, 22 Feb 2014 21:38:41 GMT</pubDate></item><item><title><![CDATA[Reply to speicher von Char-arrays und andere Variablen wieder &amp;quot;freigeben&amp;quot; on Sat, 22 Feb 2014 22:09:06 GMT]]></title><description><![CDATA[<p>Hmm tatsache, das sind wirklich nur 4, das erklärt einiges warum ich keinen namen aus &amp;_name erhalte jedoch aus &amp;data[36] <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f61e.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--disappointed_face"
      title="=("
      alt="😞"
    /></p>
<p>kann mir jemand erklären wie man das richtig macht? also das ermitteln von inhalt und der länge? <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f61e.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--disappointed_face"
      title="=("
      alt="😞"
    /></p>
]]></description><link>https://www.c-plusplus.net/forum/post/2384669</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2384669</guid><dc:creator><![CDATA[LipkeGu]]></dc:creator><pubDate>Sat, 22 Feb 2014 22:09:06 GMT</pubDate></item><item><title><![CDATA[Reply to speicher von Char-arrays und andere Variablen wieder &amp;quot;freigeben&amp;quot; on Sat, 22 Feb 2014 22:24:09 GMT]]></title><description><![CDATA[<p>Was willst du wissen, dei Länge vom Text oder die Größe des Speicherbereichs auf den <em>data</em> zeigt?<br />
Das erste geht mit <code>strlen</code> .<br />
Im zweiten Fall musst du der Funktion einen extra Parameter spendieren.</p>
<p>Oder programmiere richtiges C++ (und nutze dessen Möglichkeiten) und nicht C mit <code>cout</code> .</p>
<p>Und nochmal zu deiner <code>memcpy</code> -Frage bezüglich Adressoperator:<br />
Das Verhalten beim Adressoperator hängt davon ab, ob deine Variablen Arrays oder Pointer sind.<br />
Und das sollte es auf gar keinen Fall.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2384672</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2384672</guid><dc:creator><![CDATA[DirkB]]></dc:creator><pubDate>Sat, 22 Feb 2014 22:24:09 GMT</pubDate></item><item><title><![CDATA[Reply to speicher von Char-arrays und andere Variablen wieder &amp;quot;freigeben&amp;quot; on Sat, 22 Feb 2014 22:32:21 GMT]]></title><description><![CDATA[<p>wo wird das denn erklärt, mit dem richtigen C++ <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f61e.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--disappointed_face"
      title="=("
      alt="😞"
    /></p>
]]></description><link>https://www.c-plusplus.net/forum/post/2384673</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2384673</guid><dc:creator><![CDATA[LipkeGu]]></dc:creator><pubDate>Sat, 22 Feb 2014 22:32:21 GMT</pubDate></item><item><title><![CDATA[Reply to speicher von Char-arrays und andere Variablen wieder &amp;quot;freigeben&amp;quot; on Sat, 22 Feb 2014 23:19:10 GMT]]></title><description><![CDATA[<p>LipkeGu schrieb:</p>
<blockquote>
<p>wo wird das denn erklärt, mit dem richtigen C++ <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f61e.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--disappointed_face"
      title="=("
      alt="😞"
    /></p>
</blockquote>
<p>In einem guten C++ Buch <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f609.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--winking_face"
      title=";)"
      alt="😉"
    /></p>
<p>Ich persönlich mag den <a href="http://www.amazon.com/Primer-5th-Edition-Stanley-Lippman/dp/0321714113" rel="nofollow">C++ Primer von Lippman</a>, den es aber nur auf englisch gibt. Ein deutsches Buch wäre z.B. der <a href="http://www.amazon.com/Der-Programmierer-lernen-Professionell-anwenden/dp/3446416447" rel="nofollow">C++ Programmierer</a>.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2384680</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2384680</guid><dc:creator><![CDATA[icarus2]]></dc:creator><pubDate>Sat, 22 Feb 2014 23:19:10 GMT</pubDate></item></channel></rss>