<?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[Anfänger Problem(verstehe Aufgabenstellung nicht richtig)]]></title><description><![CDATA[<p>Hallo,<br />
ich lese grade den C++ Primer Plus 6th und mein Englisch ist ein wenig schlecht bin grade im 9 Kapitel bei den Aufgaben leider gibt es keine Offizielle Lösungen und in Internet hab ich nur eine Lösung gefunden wo aber ein Kommentar drunter stand das die Aufgabe falsch ist.</p>
<p>Hier die Aufgabe:</p>
<blockquote>
<p>3. Begin with the following structure declaration:<br />
struct chaff<br />
{<br />
char dross[20];<br />
int slag;<br />
};<br />
Write a program that uses placement new to place an array of two such structures in<br />
a buffer.Then assign values to the structure members (remembering to use<br />
strcpy() for the char array) and use a loop to display the contents. Option 1 is to<br />
use a static array, like that in Listing 9.10, for the buffer. Option 2 is to use regular<br />
new to allocate the buffer.</p>
</blockquote>
<p>Ausschnitt aus 9.10:</p>
<pre><code>const int BUF = 512;
char buffer[BUF]; // chunk of memory

int main(){
...
pd2 = new (buffer) double[N]; // use buffer array
...
return 0;
}
</code></pre>
<p>Ich habe jetzt einfach mal ein Programm erstellt bin mir jetzt aber nicht sicher ob es der Aufgabenstellung entspricht. Hoffe hier kann jemand besser Englisch.</p>
<pre><code>#include&lt;iostream&gt;

using namespace std;

struct chaff{ 
	char dross[20]; 
	int slag; 
};

char buffer[512];

int main(){

	int choice;
	chaff *p_chaff;
	char *p_buffer;

	cout &lt;&lt; &quot;1:Static Buffer\n2:By new createt buffer\nEvery other number to quit\nEnter: &quot;;

	while(!(cin &gt;&gt; choice)){
		system(&quot;cls&quot;);
		cin.clear();
		cin.ignore(numeric_limits&lt;streamsize&gt;::max(), '\n');
		cout &lt;&lt; &quot;Wrong Input!!!\n1:Static Buffer\n2:By new createt buffer\nEvery other key to quit\nEnter: &quot;;
	}

	switch(choice){
	case 1:
		p_chaff = new(buffer) chaff[2];
		break;
	case 2:
		p_buffer = new char[512];
		p_chaff = new(p_buffer) chaff[2];
		break;
	default:
		return 0;
		break;
	}

	strcpy_s(p_chaff[0].dross, &quot;Object1&quot;);
	p_chaff[0].slag = 1;

	strcpy_s(p_chaff[1].dross, &quot;Object2&quot;);
	p_chaff[1].slag = 2;

	for(int i = 0; i &lt; 2; i++)
		cout &lt;&lt; &amp;p_chaff[i] &lt;&lt; &quot; : &quot; &lt;&lt; p_chaff[i].dross &lt;&lt; &quot; : &quot; &lt;&lt; p_chaff[i].slag &lt;&lt; endl;

	cin.get();
	cin.get();
	return 0;
}
</code></pre>
<p>Verbesserung Vorschlage sind immer gerne gesehen <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f603.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--grinning_face_with_big_eyes"
      title=":D"
      alt="😃"
    /></p>
]]></description><link>https://www.c-plusplus.net/forum/topic/321275/anfänger-problem-verstehe-aufgabenstellung-nicht-richtig</link><generator>RSS for Node</generator><lastBuildDate>Tue, 21 Jul 2026 17:30:10 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/321275.rss" rel="self" type="application/rss+xml"/><pubDate>Wed, 30 Oct 2013 15:56:41 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to Anfänger Problem(verstehe Aufgabenstellung nicht richtig) on Wed, 30 Oct 2013 16:10:45 GMT]]></title><description><![CDATA[<p>Hallo,<br />
ich lese grade den C++ Primer Plus 6th und mein Englisch ist ein wenig schlecht bin grade im 9 Kapitel bei den Aufgaben leider gibt es keine Offizielle Lösungen und in Internet hab ich nur eine Lösung gefunden wo aber ein Kommentar drunter stand das die Aufgabe falsch ist.</p>
<p>Hier die Aufgabe:</p>
<blockquote>
<p>3. Begin with the following structure declaration:<br />
struct chaff<br />
{<br />
char dross[20];<br />
int slag;<br />
};<br />
Write a program that uses placement new to place an array of two such structures in<br />
a buffer.Then assign values to the structure members (remembering to use<br />
strcpy() for the char array) and use a loop to display the contents. Option 1 is to<br />
use a static array, like that in Listing 9.10, for the buffer. Option 2 is to use regular<br />
new to allocate the buffer.</p>
</blockquote>
<p>Ausschnitt aus 9.10:</p>
<pre><code>const int BUF = 512;
char buffer[BUF]; // chunk of memory

int main(){
...
pd2 = new (buffer) double[N]; // use buffer array
...
return 0;
}
</code></pre>
<p>Ich habe jetzt einfach mal ein Programm erstellt bin mir jetzt aber nicht sicher ob es der Aufgabenstellung entspricht. Hoffe hier kann jemand besser Englisch.</p>
<pre><code>#include&lt;iostream&gt;

using namespace std;

struct chaff{ 
	char dross[20]; 
	int slag; 
};

char buffer[512];

int main(){

	int choice;
	chaff *p_chaff;
	char *p_buffer;

	cout &lt;&lt; &quot;1:Static Buffer\n2:By new createt buffer\nEvery other number to quit\nEnter: &quot;;

	while(!(cin &gt;&gt; choice)){
		system(&quot;cls&quot;);
		cin.clear();
		cin.ignore(numeric_limits&lt;streamsize&gt;::max(), '\n');
		cout &lt;&lt; &quot;Wrong Input!!!\n1:Static Buffer\n2:By new createt buffer\nEvery other key to quit\nEnter: &quot;;
	}

	switch(choice){
	case 1:
		p_chaff = new(buffer) chaff[2];
		break;
	case 2:
		p_buffer = new char[512];
		p_chaff = new(p_buffer) chaff[2];
		break;
	default:
		return 0;
		break;
	}

	strcpy_s(p_chaff[0].dross, &quot;Object1&quot;);
	p_chaff[0].slag = 1;

	strcpy_s(p_chaff[1].dross, &quot;Object2&quot;);
	p_chaff[1].slag = 2;

	for(int i = 0; i &lt; 2; i++)
		cout &lt;&lt; &amp;p_chaff[i] &lt;&lt; &quot; : &quot; &lt;&lt; p_chaff[i].dross &lt;&lt; &quot; : &quot; &lt;&lt; p_chaff[i].slag &lt;&lt; endl;

	cin.get();
	cin.get();
	return 0;
}
</code></pre>
<p>Verbesserung Vorschlage sind immer gerne gesehen <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f603.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--grinning_face_with_big_eyes"
      title=":D"
      alt="😃"
    /></p>
]]></description><link>https://www.c-plusplus.net/forum/post/2363919</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2363919</guid><dc:creator><![CDATA[CraftPlorer]]></dc:creator><pubDate>Wed, 30 Oct 2013 16:10:45 GMT</pubDate></item><item><title><![CDATA[Reply to Anfänger Problem(verstehe Aufgabenstellung nicht richtig) on Wed, 30 Oct 2013 17:10:23 GMT]]></title><description><![CDATA[<p>Option meint: Tu es entweder auf die eine Weise oder auf die andere. Nicht auf beide. Darueber hinaus allokierst du Speicher und Objekte, gibst sie aber nicht wieder frei.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2363948</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2363948</guid><dc:creator><![CDATA[knivil]]></dc:creator><pubDate>Wed, 30 Oct 2013 17:10:23 GMT</pubDate></item><item><title><![CDATA[Reply to Anfänger Problem(verstehe Aufgabenstellung nicht richtig) on Wed, 30 Oct 2013 17:46:11 GMT]]></title><description><![CDATA[<p>placement new erfodert die Einbindung von &lt;new&gt;</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2363960</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2363960</guid><dc:creator><![CDATA[camper]]></dc:creator><pubDate>Wed, 30 Oct 2013 17:46:11 GMT</pubDate></item><item><title><![CDATA[Reply to Anfänger Problem(verstehe Aufgabenstellung nicht richtig) on Wed, 30 Oct 2013 20:14:43 GMT]]></title><description><![CDATA[<p>Ok Danke. Das heißt ich hatte mir das mit der Abfrage sparen können und mir einfach beim Schreiben des Programm eine Art aussuchen können oder?</p>
<p><a class="plugin-mentions-user plugin-mentions-a" href="https://www.c-plusplus.net/forum/uid/6642">@camper</a></p>
<blockquote>
<p>placement new erfodert die Einbindung von &lt;new&gt;</p>
</blockquote>
<p>Was meinst du damit?</p>
<p>Geb ich den Speicher so wieder frei?</p>
<pre><code>delete [] p_buffer;
</code></pre>
<p>Da stand nichts im Buch ob und wie man placement new frei gibt. Muss ich das überhaupt sehe da nicht so den sinn...</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2363984</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2363984</guid><dc:creator><![CDATA[CraftPlorer]]></dc:creator><pubDate>Wed, 30 Oct 2013 20:14:43 GMT</pubDate></item><item><title><![CDATA[Reply to Anfänger Problem(verstehe Aufgabenstellung nicht richtig) on Wed, 30 Oct 2013 20:53:56 GMT]]></title><description><![CDATA[<p>Du musst dafür sagen, dass der Destruktor der Objekte aufgerufen wird.<br />
Das machst du für einen Zeiger auf T wie folgt: T-&gt;~T();<br />
Bei deiner struct ist das jetzt nicht notwendig, weil der Destruktor nichts tut (die struct ist trivially destructible), aber generell sollte man das beachten.<br />
Was geleakt wird ist der Speicher vom dynamischen Array, den musst du freigeben.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2363997</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2363997</guid><dc:creator><![CDATA[Nathan]]></dc:creator><pubDate>Wed, 30 Oct 2013 20:53:56 GMT</pubDate></item><item><title><![CDATA[Reply to Anfänger Problem(verstehe Aufgabenstellung nicht richtig) on Wed, 30 Oct 2013 22:41:33 GMT]]></title><description><![CDATA[<p>CraftPlorer schrieb:</p>
<blockquote>
<p><a class="plugin-mentions-user plugin-mentions-a" href="https://www.c-plusplus.net/forum/uid/6642">@camper</a></p>
<blockquote>
<p>placement new erfodert die Einbindung von &lt;new&gt;</p>
</blockquote>
<p>Was meinst du damit?</p>
</blockquote>
<p>Die Headerdatei. Du musst <code>#include &lt;new&gt;</code> schreiben, um standardkonform zu bleiben.</p>
<p>Der übliche Zyklus ist übrigens:</p>
<pre><code>void* address = operator new(elements * sizeof(T)); // rohe Speicherallokation
new (address) T(args);                              // Konstruktion über placement new (1 Element)
new (address) T[elements];                          // oder alternativ: Konstruktion (Array)
...
T-&gt;~T();                                            // Zerstörung (bei Array in Schleife)
operator delete(address);                           // rohe Speicherdeallokation
</code></pre>
<p>Für die Array-Zerstörung siehe z.B. <code>destroy_range()</code> <a href="http://www.c-plusplus.net/forum/p1704431#1704431" rel="nofollow">hier</a>. Wikipedia hat über die ganze Thematik auch einen <a href="https://en.wikipedia.org/wiki/Placement_syntax" rel="nofollow">Artikel</a>.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2364020</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2364020</guid><dc:creator><![CDATA[Nexus]]></dc:creator><pubDate>Wed, 30 Oct 2013 22:41:33 GMT</pubDate></item><item><title><![CDATA[Reply to Anfänger Problem(verstehe Aufgabenstellung nicht richtig) on Thu, 31 Oct 2013 19:00:15 GMT]]></title><description><![CDATA[<p>Ok Danke. Das macht natürlich auch mehr sinn als so ein Buffer zu erstellen. Denke das Buch wird das später auch wieder aufgreifen.<br />
Die einzige kleine Frage die ich noch habe ist zu dieser Zeile:</p>
<pre><code>new (address) T(args);
</code></pre>
<p>Wofür das &quot;(args)&quot; ? Also was kann ich da noch machen?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2364143</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2364143</guid><dc:creator><![CDATA[CraftPlorer]]></dc:creator><pubDate>Thu, 31 Oct 2013 19:00:15 GMT</pubDate></item><item><title><![CDATA[Reply to Anfänger Problem(verstehe Aufgabenstellung nicht richtig) on Thu, 31 Oct 2013 19:17:16 GMT]]></title><description><![CDATA[<p>CraftPlorer schrieb:</p>
<blockquote>
<p>Wofür das &quot;(args)&quot; ? Also was kann ich da noch machen?</p>
</blockquote>
<p>Ein Konstruktorargument angeben, z.B:</p>
<pre><code>new (address) std::string(&quot;foo&quot;);
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/2364152</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2364152</guid><dc:creator><![CDATA[Nathan]]></dc:creator><pubDate>Thu, 31 Oct 2013 19:17:16 GMT</pubDate></item><item><title><![CDATA[Reply to Anfänger Problem(verstehe Aufgabenstellung nicht richtig) on Thu, 31 Oct 2013 20:41:21 GMT]]></title><description><![CDATA[<p>Ahhh ok. Danke. Sollte mal weiter lesen war noch nicht bei Klassen und so <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f603.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--grinning_face_with_big_eyes"
      title=":D"
      alt="😃"
    /><br />
Muss/Kann ich den Beitrag als erledigt abstempeln oder schließen oder so was?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2364173</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2364173</guid><dc:creator><![CDATA[CraftPlorer]]></dc:creator><pubDate>Thu, 31 Oct 2013 20:41:21 GMT</pubDate></item></channel></rss>