<?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[memcpy und alignment]]></title><description><![CDATA[<p>Hallo,</p>
<p>ich habe folgenden (nicht meinen) code vor mir:</p>
<pre><code class="language-cpp">struct blabla
{
  int    a;   
  int    b;   

  double c;   
  double d;

  anderes_struct e;
  anderes_struct f;

  blabla(){memset(this,0,sizeof(blabla));};
  blabla(const blabla&amp;other){memcpy(this,&amp;other,sizeof(blabla));};
  blabla&amp; operator=(const blabla&amp;other){memcpy(this,&amp;other,sizeof(blabla));return *this;};
}
</code></pre>
<p>Ist die Verwendung von memcpy/memset für structs bestehen nur aus POD okay? Mir persönlich fällt da memory alignment im Release Modus als Mögliches Problem auf.</p>
<p>Wie sieht es aus wie in diesem Fall, wenn auch andere structs, oder Klassen die nicht nur aus POD bestehen ein Teil der Klasse sind?</p>
]]></description><link>https://www.c-plusplus.net/forum/topic/325698/memcpy-und-alignment</link><generator>RSS for Node</generator><lastBuildDate>Sat, 11 Jul 2026 21:52:12 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/325698.rss" rel="self" type="application/rss+xml"/><pubDate>Wed, 14 May 2014 08:45:15 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to memcpy und alignment on Wed, 14 May 2014 08:45:15 GMT]]></title><description><![CDATA[<p>Hallo,</p>
<p>ich habe folgenden (nicht meinen) code vor mir:</p>
<pre><code class="language-cpp">struct blabla
{
  int    a;   
  int    b;   

  double c;   
  double d;

  anderes_struct e;
  anderes_struct f;

  blabla(){memset(this,0,sizeof(blabla));};
  blabla(const blabla&amp;other){memcpy(this,&amp;other,sizeof(blabla));};
  blabla&amp; operator=(const blabla&amp;other){memcpy(this,&amp;other,sizeof(blabla));return *this;};
}
</code></pre>
<p>Ist die Verwendung von memcpy/memset für structs bestehen nur aus POD okay? Mir persönlich fällt da memory alignment im Release Modus als Mögliches Problem auf.</p>
<p>Wie sieht es aus wie in diesem Fall, wenn auch andere structs, oder Klassen die nicht nur aus POD bestehen ein Teil der Klasse sind?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2399101</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2399101</guid><dc:creator><![CDATA[sdadasdsadsad]]></dc:creator><pubDate>Wed, 14 May 2014 08:45:15 GMT</pubDate></item><item><title><![CDATA[Reply to memcpy und alignment on Wed, 14 May 2014 08:56:38 GMT]]></title><description><![CDATA[<p>Nur als Anmerkung: Structs/Klassen mit einem Custom-Default-Constructor sind keine PODs <s>mehr</s>.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2399103</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2399103</guid><dc:creator><![CDATA[theta]]></dc:creator><pubDate>Wed, 14 May 2014 08:56:38 GMT</pubDate></item><item><title><![CDATA[Reply to memcpy und alignment on Wed, 14 May 2014 09:17:43 GMT]]></title><description><![CDATA[<p>Der implizit deklarierte Kopierkonstruktor und Zuweisungsoperator vom Compiler kopiert alles schon für dich, und wenn man alles ausnullen will dann muss man auch nicht <code>memset</code> verwenden, sondern kann die Klasse mit <code>{}</code> initialisieren - wenn diese ein POD wäre (was hier nicht der Fall ist), dann würde dieser Initializer das Objekt <em>zero-initialize</em>n, was soviel heißt wie: Padding ausnullen, alle Skalare auf Null setzen, und andere Member wiederum <em>zero-initialize</em>n.</p>
<blockquote>
<p>anderes_struct</p>
</blockquote>
<p>Was ist das für eine Struktur? Ein POD-Typ?</p>
<p>Du brauchst ja nach einer Kopie nicht auch das gleiche Padding, oder? <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>theta schrieb:</p>
<blockquote>
<p>Nur als Anmerkung: Structs/Klassen mit einem Custom-Default-Constructor sind keine PODs <s>mehr</s>.</p>
</blockquote>
<p><img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f44d.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--thumbs_up"
      title=":+1:"
      alt="👍"
    /><br />
Standardlayout hat es allerdings.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2399106</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2399106</guid><dc:creator><![CDATA[Columbo]]></dc:creator><pubDate>Wed, 14 May 2014 09:17:43 GMT</pubDate></item><item><title><![CDATA[Reply to memcpy und alignment on Wed, 14 May 2014 09:29:32 GMT]]></title><description><![CDATA[<p>Arcoth schrieb:</p>
<blockquote>
<p>Was ist das für eine Struktur? Ein POD-Typ?</p>
</blockquote>
<pre><code class="language-cpp">struct anderes_struct
{
    int row,col;
    unsigned char value;

	anderes_struct():row(0),col(0),value(0){};
	anderes_struct(int r,int c):row(r),col(c),value(0){};
	anderes_struct(int r,int c,unsigned char v):row(r),col(c),value(v){};
	anderes_struct(unsigned int r,unsigned int c):row((int)r),col((int)c),value(0){};
	anderes_struct(unsigned int r,unsigned int c,unsigned char v):row((int)r),col((int)c),value(v){};
	anderes_struct(double r,double c,unsigned char v);
	anderes_struct(double r,double c);
	anderes_struct( const anderes_struct &amp;other);
	anderes_struct(const anderes_struct2 &amp;other);
	anderes_struct&amp; operator=(const anderes_struct&amp; other);
	anderes_struct&amp; operator=(const anderes_struct2&amp; other);
	anderes_struct&amp; operator+=(const anderes_struct&amp;other);
	anderes_struct&amp; operator-=(const anderes_struct&amp;other);
	anderes_struct   operator+ (const anderes_struct&amp;other);
	anderes_struct   operator- (const anderes_struct&amp;other);
	anderes_struct2  operator+ (const anderes_struct2&amp;other);
	anderes_struct2  operator- (const anderes_struct2&amp;other);

	anderes_struct operator* (int skalar);
	anderes_struct operator* (double skalar);
	anderes_struct operator/ (int skalar);
	anderes_struct operator/ (double skalar);
	anderes_struct&amp; operator/=(int skalar);
	anderes_struct&amp; operator/=(double skalar);
};
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/2399107</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2399107</guid><dc:creator><![CDATA[sdadas]]></dc:creator><pubDate>Wed, 14 May 2014 09:29:32 GMT</pubDate></item><item><title><![CDATA[Reply to memcpy und alignment on Wed, 14 May 2014 09:35:00 GMT]]></title><description><![CDATA[<p>Übrigens: §3.9/2 und /3 gelten auch nur für trivial kopierbare Typen. Deine Klasse ist offensichtlich nicht trivial kopierbar, also muss <code>*this</code> nach der Kopie in Zeilen 12 und 13 nicht denselben Wert haben wie <code>other</code> .<br />
Ob das UB ist, kann ich aber nicht mit Sicherheit sagen (sieht irgendwie nicht danach aus, halt nur danach, dass das Ergebnis nicht festgelegt ist), das weiß bestimmt camper.</p>
<p><code>anderes_struct</code> ist nach deinem Code-Schnipsel kein POD, und auch nicht trivial kopierbar.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2399110</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2399110</guid><dc:creator><![CDATA[Columbo]]></dc:creator><pubDate>Wed, 14 May 2014 09:35:00 GMT</pubDate></item><item><title><![CDATA[Reply to memcpy und alignment on Wed, 14 May 2014 09:40:59 GMT]]></title><description><![CDATA[<p>Arcoth schrieb:</p>
<blockquote>
<p>Übrigens: §3.9/2 und /3 gelten auch nur für trivial kopierbare Typen.</p>
</blockquote>
<p>Was ist mit &quot; §3.9/2 und /3&quot; gemeint? Wäre dir für einen Link dankbar.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2399111</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2399111</guid><dc:creator><![CDATA[dasdsad]]></dc:creator><pubDate>Wed, 14 May 2014 09:40:59 GMT</pubDate></item><item><title><![CDATA[Reply to memcpy und alignment on Wed, 14 May 2014 09:54:14 GMT]]></title><description><![CDATA[<p>dasdsad schrieb:</p>
<blockquote>
<p>Arcoth schrieb:</p>
<blockquote>
<p>Übrigens: §3.9/2 und /3 gelten auch nur für trivial kopierbare Typen.</p>
</blockquote>
<p>Was ist mit &quot; §3.9/2 und /3&quot; gemeint? Wäre dir für einen Link dankbar.</p>
</blockquote>
<p>Das sind Standardparagraphen:</p>
<p>§3.9/2 schrieb:</p>
<blockquote>
<p>For any object (other than a base-class subobject) of trivially copyable type T, whether or not the object holds a valid value of type T, the underlying bytes (1.7) making up the object can be copied into an array of <code>char</code> or <code>unsigned char</code> . If the content of the array of char or unsigned char is copied back into the<br />
object, the object shall subsequently hold its original value.</p>
<pre><code>#define N sizeof(T)
char buf[N];
T obj;                        // obj initialized to its original value
std::memcpy(buf, &amp;obj, N);    // between these two calls to std::memcpy,
                              // obj might be modified
std::memcpy(&amp;obj, buf, N);    // at this point, each subobject of obj of scalar type holds its original value
</code></pre>
</blockquote>
<p>§3.9/3 schrieb:</p>
<blockquote>
<p>For any trivially copyable type <code>T</code> , if two pointers to <code>T</code> point to distinct <code>T</code> objects <code>obj1</code> and <code>obj2</code> , where<br />
neither <code>obj1</code> nor <code>obj2</code> is a base-class subobject, if the underlying bytes (1.7) making up <code>obj1</code> are copied<br />
into <code>obj2</code> , <code>obj2</code> shall subsequently hold the same value as <code>obj1</code> .</p>
<pre><code>T* t1p;
T* t2p;
// provided that t2p points to an initialized object ...
std::memcpy(t1p, t2p, sizeof(T));
// at this point, every subobject of trivially copyable type in *t1p contains
// the same value as the corresponding subobject in *t2p
</code></pre>
</blockquote>
]]></description><link>https://www.c-plusplus.net/forum/post/2399112</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2399112</guid><dc:creator><![CDATA[Columbo]]></dc:creator><pubDate>Wed, 14 May 2014 09:54:14 GMT</pubDate></item></channel></rss>