<?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[Frage zu placement new und bitwise Copy]]></title><description><![CDATA[<p>Ist folgender Code legal?</p>
<pre><code>std::aligned_storage_t&lt;sizeof(T), alignof(T)&gt; a, b;
::new(static_cast&lt;void*&gt;(a)) T; // erzeuge Objekt in a
b = a; // kopiere die Bits von T nach a, a wird danach nicht mehr verwendet
reinterpret_cast&lt;T*&gt;(&amp;b)-&gt;~T(); // zerstöre Objekt in b
// zerstöre Objekt nicht in a
</code></pre>
<p>Logisch gesehen, sollte dieser Code legal sein. Man macht zwar keine tiefe Kopie, es wird aber nur ein Objekt zerstört und der Ursprungsspeicher nicht mehr angefasst, weswegen das egal sein sollte.<br />
Mein Gefühl sagt mir aber, dass das UB sein könnte. Ich konnte das jetzt auch nicht im Standard herausfinden.<br />
Die Lifetime von T beginnt beim placement new, klar. Die Frage ist nur, ob es definiert ist, den Speicher für das Objekt zu verschieben.</p>
]]></description><link>https://www.c-plusplus.net/forum/topic/328013/frage-zu-placement-new-und-bitwise-copy</link><generator>RSS for Node</generator><lastBuildDate>Wed, 08 Jul 2026 16:09:33 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/328013.rss" rel="self" type="application/rss+xml"/><pubDate>Tue, 16 Sep 2014 17:22:25 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to Frage zu placement new und bitwise Copy on Tue, 16 Sep 2014 17:22:25 GMT]]></title><description><![CDATA[<p>Ist folgender Code legal?</p>
<pre><code>std::aligned_storage_t&lt;sizeof(T), alignof(T)&gt; a, b;
::new(static_cast&lt;void*&gt;(a)) T; // erzeuge Objekt in a
b = a; // kopiere die Bits von T nach a, a wird danach nicht mehr verwendet
reinterpret_cast&lt;T*&gt;(&amp;b)-&gt;~T(); // zerstöre Objekt in b
// zerstöre Objekt nicht in a
</code></pre>
<p>Logisch gesehen, sollte dieser Code legal sein. Man macht zwar keine tiefe Kopie, es wird aber nur ein Objekt zerstört und der Ursprungsspeicher nicht mehr angefasst, weswegen das egal sein sollte.<br />
Mein Gefühl sagt mir aber, dass das UB sein könnte. Ich konnte das jetzt auch nicht im Standard herausfinden.<br />
Die Lifetime von T beginnt beim placement new, klar. Die Frage ist nur, ob es definiert ist, den Speicher für das Objekt zu verschieben.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2417857</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2417857</guid><dc:creator><![CDATA[Nathan]]></dc:creator><pubDate>Tue, 16 Sep 2014 17:22:25 GMT</pubDate></item><item><title><![CDATA[Reply to Frage zu placement new und bitwise Copy on Tue, 16 Sep 2014 17:30:40 GMT]]></title><description><![CDATA[<p>Ist definiert solange T trivial kopierbar ist. Damit wird auch nicht der Speicher verschoben sondern kopiert, du hast dann also zwei Objekte.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2417859</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2417859</guid><dc:creator><![CDATA[Columbo]]></dc:creator><pubDate>Tue, 16 Sep 2014 17:30:40 GMT</pubDate></item><item><title><![CDATA[Reply to Frage zu placement new und bitwise Copy on Tue, 16 Sep 2014 17:30:03 GMT]]></title><description><![CDATA[<p>Arcoth schrieb:</p>
<blockquote>
<p>Ist definiert solange T trivial kopierbar ist.</p>
</blockquote>
<p>Das war mir klar.<br />
Und wenns nicht ist? Rein theoretisch sollte das dem T doch egal sein, dass es nun in b ist, anstatt in a.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2417860</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2417860</guid><dc:creator><![CDATA[Nathan]]></dc:creator><pubDate>Tue, 16 Sep 2014 17:30:03 GMT</pubDate></item><item><title><![CDATA[Reply to Frage zu placement new und bitwise Copy on Tue, 16 Sep 2014 17:34:11 GMT]]></title><description><![CDATA[<p>Der Edit kam etwas spät.</p>
<p>Nathan schrieb:</p>
<blockquote>
<p>Und wenns nicht ist?</p>
</blockquote>
<p>Dann ist es Unsinn? Was wenn der Konstruktor <code>this</code> speichert, bpsw. in einer statischen Map? Das zerstört doch nur die Programmlogik. Auch theoretisch.</p>
<p>Wofür brauchst du das überhaupt? Willst du dir den Move-Konstruktor sparen und gleich zum bitwise copy übergehen? <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/2417861</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2417861</guid><dc:creator><![CDATA[Columbo]]></dc:creator><pubDate>Tue, 16 Sep 2014 17:34:11 GMT</pubDate></item><item><title><![CDATA[Reply to Frage zu placement new und bitwise Copy on Tue, 16 Sep 2014 17:37:46 GMT]]></title><description><![CDATA[<p>Arcoth schrieb:</p>
<blockquote>
<p>Der Edit kam etwas spät.</p>
<p>Nathan schrieb:</p>
<blockquote>
<p>Und wenns nicht ist?</p>
</blockquote>
<p>Dann ist es Unsinn? Was wenn der Konstruktor <code>this</code> speichert, bpsw. in einer statischen Map? Das zerstört doch nur die Programmlogik. Auch theoretisch.</p>
</blockquote>
<p>Wer macht denn bitte sowas?</p>
<blockquote>
<p>Wofür brauchst du das überhaupt? Willst du dir den Move-Konstruktor sparen und gleich zum bitwise copy übergehen? <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>
</blockquote>
<p>Implementierung von variant für exception safety.<br />
Genau genommen assign. Das alte Objekt muss zerstört werden und das neue erstellt. Wenn move nicht throwed kann man einfach ein Dummy Neu erstellen, das alte zerstören und das dummy in den Speicher moven. Aber wenn move throwed gibt es sonst keine schöne Lösung für strong exception safety. Boosts Implementierung erstellt dann da ein default-constructed Fallback Objekt nach einem try-catch, das ist aber auch nicht schön...</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2417864</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2417864</guid><dc:creator><![CDATA[Nathan]]></dc:creator><pubDate>Tue, 16 Sep 2014 17:37:46 GMT</pubDate></item><item><title><![CDATA[Reply to Frage zu placement new und bitwise Copy on Tue, 16 Sep 2014 17:43:16 GMT]]></title><description><![CDATA[<p>Nathan schrieb:</p>
<blockquote>
<p>Arcoth schrieb:</p>
<blockquote>
<p>Der Edit kam etwas spät.</p>
<p>Nathan schrieb:</p>
<blockquote>
<p>Und wenns nicht ist?</p>
</blockquote>
<p>Dann ist es Unsinn? Was wenn der Konstruktor <code>this</code> speichert, bpsw. in einer statischen Map? Das zerstört doch nur die Programmlogik. Auch theoretisch.</p>
</blockquote>
<p>Wer macht denn bitte sowas?</p>
</blockquote>
<p>Klassen, die nicht trivial kopierbar sind. Konkret die Speicherung des this in einer statischen Map könnte bei virtuellen Membern erfolgen.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2417866</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2417866</guid><dc:creator><![CDATA[SeppJ]]></dc:creator><pubDate>Tue, 16 Sep 2014 17:43:16 GMT</pubDate></item><item><title><![CDATA[Reply to Frage zu placement new und bitwise Copy on Tue, 16 Sep 2014 17:47:26 GMT]]></title><description><![CDATA[<blockquote>
<p>Ich konnte das jetzt auch nicht im Standard herausfinden.</p>
</blockquote>
<p>Natürlich nicht. Es ist ja auch nicht <em>definiert</em>. Das heißt, es steht nirgends dass es UB ist - es steht aber auch nirgends, <em>dass und wie</em> es definiert ist. Also nicht vom Standard definiert.</p>
<blockquote>
<p>Wer macht denn bitte sowas?</p>
</blockquote>
<p>Darum geht es gar nicht direkt. (SeppJ hat es aber schon zusammengefasst)<br />
Eine Memberfunktion (wie einen Destruktor) auf ein nicht existierendes Objekt anzuwenden erzeugt undefiniertes Verhalten. Und wenn du ein nicht trivial kopierbares Objekt so &quot;kopierst&quot;, bzw. &quot;movest&quot;, erzeugst du dabei kein neues Objekt oder verschiebst ein bestehendes. Das alte bleibt wo es ist. Mann kann die Abstraktionsmechanismen nicht immer mit low-level Gefummel vermischen, da steigt der Standard schnell aus und du musst unportable Sachen machen.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2417869</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2417869</guid><dc:creator><![CDATA[Columbo]]></dc:creator><pubDate>Tue, 16 Sep 2014 17:47:26 GMT</pubDate></item><item><title><![CDATA[Reply to Frage zu placement new und bitwise Copy on Tue, 16 Sep 2014 17:48:05 GMT]]></title><description><![CDATA[<p>Ja, ich sehe das mittlerweile auch ein.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2417870</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2417870</guid><dc:creator><![CDATA[Nathan]]></dc:creator><pubDate>Tue, 16 Sep 2014 17:48:05 GMT</pubDate></item><item><title><![CDATA[Reply to Frage zu placement new und bitwise Copy on Tue, 16 Sep 2014 18:41:29 GMT]]></title><description><![CDATA[<p>Muss ja nichtmal ne statische Map sein.<br />
Reicht doch schon wenn das Objekt sich im Ctor irgendwo als Listener dranhängt und im Dtor wieder entfernt.<br />
Oder 100 andere Dinge die man mit <code>this</code> anfangen kann.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2417881</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2417881</guid><dc:creator><![CDATA[hustbaer]]></dc:creator><pubDate>Tue, 16 Sep 2014 18:41:29 GMT</pubDate></item></channel></rss>