<?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[Char und Float im Struct 8 Byte groß?]]></title><description><![CDATA[<p>Hallo,</p>
<p>kann mir mal einer erklären, warum ein float und ein char im Struct 8 Byte groß sind?</p>
<pre><code class="language-cpp">struct Struct {
    float f;
    char c;
};
</code></pre>
<p>sizeof(Struct) -&gt; 8, es sollte doch aber eigentlich 5 sein. Hat das mit den CPU-Registern zu tun, die auf meinem 64 bit System entsprechend 4 Byte groß sind?</p>
]]></description><link>https://www.c-plusplus.net/forum/topic/295290/char-und-float-im-struct-8-byte-groß</link><generator>RSS for Node</generator><lastBuildDate>Sat, 15 Aug 2026 12:04:34 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/295290.rss" rel="self" type="application/rss+xml"/><pubDate>Thu, 10 Nov 2011 17:49:21 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to Char und Float im Struct 8 Byte groß? on Thu, 10 Nov 2011 17:49:21 GMT]]></title><description><![CDATA[<p>Hallo,</p>
<p>kann mir mal einer erklären, warum ein float und ein char im Struct 8 Byte groß sind?</p>
<pre><code class="language-cpp">struct Struct {
    float f;
    char c;
};
</code></pre>
<p>sizeof(Struct) -&gt; 8, es sollte doch aber eigentlich 5 sein. Hat das mit den CPU-Registern zu tun, die auf meinem 64 bit System entsprechend 4 Byte groß sind?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2142992</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2142992</guid><dc:creator><![CDATA[Sinnfrei??]]></dc:creator><pubDate>Thu, 10 Nov 2011 17:49:21 GMT</pubDate></item><item><title><![CDATA[Reply to Char und Float im Struct 8 Byte groß? on Thu, 10 Nov 2011 17:54:32 GMT]]></title><description><![CDATA[<p><a href="http://de.wikipedia.org/wiki/Speicherausrichtung" rel="nofollow">http://de.wikipedia.org/wiki/Speicherausrichtung</a></p>
<p>Edit: Ok, Draveres link ist besser^^</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2142993</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2142993</guid><dc:creator><![CDATA[*Q* 1]]></dc:creator><pubDate>Thu, 10 Nov 2011 17:54:32 GMT</pubDate></item><item><title><![CDATA[Reply to Char und Float im Struct 8 Byte groß? on Thu, 10 Nov 2011 17:53:28 GMT]]></title><description><![CDATA[<p>Nennt man Padding: <a href="http://en.wikipedia.org/wiki/Data_padding" rel="nofollow">http://en.wikipedia.org/wiki/Data_padding</a></p>
<p>Grüssli</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2142994</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2142994</guid><dc:creator><![CDATA[Dravere]]></dc:creator><pubDate>Thu, 10 Nov 2011 17:53:28 GMT</pubDate></item><item><title><![CDATA[Reply to Char und Float im Struct 8 Byte groß? on Thu, 10 Nov 2011 18:49:25 GMT]]></title><description><![CDATA[<p>Okay, und warum hat ein Struct eine Größe von 2 Byte, wenn er seine leere Basis nochmal als Member definiert? Eigentlich müsste er doch nur eine Größe von einem Byte haben, denn wenn der Struct statt der Basis ein anderes leeres Member definiert, hat er auch nur eine Größe von eins.</p>
<pre><code class="language-cpp">struct Base { };

struct Derived : public Base { // -&gt; 2 Byte
    Base a;
};

struct Another { };

struct AnotherDerived : public Base { // -&gt; 1 Byte
    Another a;
};
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/2143028</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2143028</guid><dc:creator><![CDATA[Sinnfrei??]]></dc:creator><pubDate>Thu, 10 Nov 2011 18:49:25 GMT</pubDate></item><item><title><![CDATA[Reply to Char und Float im Struct 8 Byte groß? on Thu, 10 Nov 2011 19:25:59 GMT]]></title><description><![CDATA[<p>Eine Klasse kann nie die Größe 0 haben, aber eine leere Basisklasse kann zu einer Größe von 0 optimiert werden, wenn dadurch die Gesamtgröße der erbenden Klasse nicht 0 wird. Das nennt man dann empty base optimization.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2143044</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2143044</guid><dc:creator><![CDATA[SeppJ]]></dc:creator><pubDate>Thu, 10 Nov 2011 19:25:59 GMT</pubDate></item><item><title><![CDATA[Reply to Char und Float im Struct 8 Byte groß? on Thu, 10 Nov 2011 19:27:58 GMT]]></title><description><![CDATA[<p>Bei beiden varianten erbst du von leeren Basisklassen. Warum ist die eine dann größer als die andere?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2143052</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2143052</guid><dc:creator><![CDATA[*Q* 1]]></dc:creator><pubDate>Thu, 10 Nov 2011 19:27:58 GMT</pubDate></item><item><title><![CDATA[Reply to Char und Float im Struct 8 Byte groß? on Thu, 10 Nov 2011 19:28:48 GMT]]></title><description><![CDATA[<p>Das ist mir klar, nur warum hat der untere Struct dann eine Größe von eins? Warum kommt es darauf an, ob man den Basisstruct als Member definiert oder ein beliebigen anderen Struct, ob das ganze zwei oder ein Byte groß ist? Warum braucht Base im oberen Beispiel ein Byte mehr, obwohl beide leer sind?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2143054</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2143054</guid><dc:creator><![CDATA[Sinnfrei??]]></dc:creator><pubDate>Thu, 10 Nov 2011 19:28:48 GMT</pubDate></item><item><title><![CDATA[Reply to Char und Float im Struct 8 Byte groß? on Thu, 10 Nov 2011 19:31:01 GMT]]></title><description><![CDATA[<p><a class="plugin-mentions-user plugin-mentions-a" href="https://www.c-plusplus.net/forum/uid/18065">@Q</a>: GCC 4.6:<br />
sizeof(Derived) -&gt; 2 Byte<br />
sizeof(AnotherDerived) -&gt; 1 Byte</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2143055</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2143055</guid><dc:creator><![CDATA[Sinnfrei??]]></dc:creator><pubDate>Thu, 10 Nov 2011 19:31:01 GMT</pubDate></item><item><title><![CDATA[Reply to Char und Float im Struct 8 Byte groß? on Thu, 10 Nov 2011 19:36:11 GMT]]></title><description><![CDATA[<p>Sinnfrei?? schrieb:</p>
<blockquote>
<p><a class="plugin-mentions-user plugin-mentions-a" href="https://www.c-plusplus.net/forum/uid/18065">@Q</a>: GCC 4.6:<br />
sizeof(Derived) -&gt; 2 Byte<br />
sizeof(AnotherDerived) -&gt; 1 Byte</p>
</blockquote>
<p>Ich weiß. Genau das find ich ja so komisch. Beides mal erbst du von leeren basisklassen und hast einen leeren member.<br />
SeepJ's erklärung erklärt das irgendwie nicht vollständig^^</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2143058</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2143058</guid><dc:creator><![CDATA[*Q* 1]]></dc:creator><pubDate>Thu, 10 Nov 2011 19:36:11 GMT</pubDate></item><item><title><![CDATA[Reply to Char und Float im Struct 8 Byte groß? on Thu, 10 Nov 2011 20:20:35 GMT]]></title><description><![CDATA[<p>Btw, MSVC++ 10 sagt, dass beides 1 Byte groß ist. Also ein Compiler Bug?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2143070</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2143070</guid><dc:creator><![CDATA[Sinnfrei??]]></dc:creator><pubDate>Thu, 10 Nov 2011 20:20:35 GMT</pubDate></item><item><title><![CDATA[Reply to Char und Float im Struct 8 Byte groß? on Thu, 10 Nov 2011 21:58:48 GMT]]></title><description><![CDATA[<p>Sinnfrei?? schrieb:</p>
<blockquote>
<p>Btw, MSVC++ 10 sagt, dass beides 1 Byte groß ist. Also ein Compiler Bug?</p>
</blockquote>
<p>Es ist eine <em>kann</em> Optimierung, keine <em>muss</em> Optimierung.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2143092</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2143092</guid><dc:creator><![CDATA[SeppJ]]></dc:creator><pubDate>Thu, 10 Nov 2011 21:58:48 GMT</pubDate></item><item><title><![CDATA[Reply to Char und Float im Struct 8 Byte groß? on Fri, 11 Nov 2011 09:45:12 GMT]]></title><description><![CDATA[<p>Find' ich aber trotzdem komisch. Ich denke nicht, dass die GCC-Entwickler das mit Absicht so gemacht haben. Man sollte sie vielleicht mal drauf hinweisen...</p>
<p>EBCO (empty base class optimization) ist toll. Schaut man bei libstdc++ nach, wie sie unique_ptr implementiert haben, dann sieht das in etwa so aus:</p>
<pre><code class="language-cpp">template&lt;class T, class Deleter = default_deleter&lt;T&gt; &gt;
class unique_ptr {
  tuple&lt;T*,Deleter&gt; data_;
public:
  ...
};
</code></pre>
<p>und da der default_deleter zustandslos (leer) ist, tuple&lt;&gt; über Vererbung implementiert ist, und der GCC EBCO durchführt, wird sizeof(unique_ptr&lt;T*&gt;)==sizeof(T*) gelten. <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="🙂"
    /></p>
]]></description><link>https://www.c-plusplus.net/forum/post/2143167</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2143167</guid><dc:creator><![CDATA[krümelkacker]]></dc:creator><pubDate>Fri, 11 Nov 2011 09:45:12 GMT</pubDate></item><item><title><![CDATA[Reply to Char und Float im Struct 8 Byte groß? on Fri, 11 Nov 2011 09:57:44 GMT]]></title><description><![CDATA[<p>Tuple ist über vererbung implementiert?</p>
<p>Soll das heißen, dass<br />
Tuple&lt;int, int&gt; zweimal von int erbt?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2143173</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2143173</guid><dc:creator><![CDATA[*Q* 1]]></dc:creator><pubDate>Fri, 11 Nov 2011 09:57:44 GMT</pubDate></item><item><title><![CDATA[Reply to Char und Float im Struct 8 Byte groß? on Fri, 11 Nov 2011 09:59:00 GMT]]></title><description><![CDATA[<p>tuple&lt;A, B, C, D, E&gt; erbt von tuple&lt;B, C, D, E&gt; erbt von tuple&lt;C, D, E&gt; erbt von tuple&lt;D, E&gt; erbt von tuple&lt;D&gt;.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2143174</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2143174</guid><dc:creator><![CDATA[314159265358979]]></dc:creator><pubDate>Fri, 11 Nov 2011 09:59:00 GMT</pubDate></item><item><title><![CDATA[Reply to Char und Float im Struct 8 Byte groß? on Fri, 11 Nov 2011 12:50:47 GMT]]></title><description><![CDATA[<p>314159265358979 schrieb:</p>
<blockquote>
<p>tuple&lt;A, B, C, D, E&gt; erbt von tuple&lt;B, C, D, E&gt; erbt von tuple&lt;C, D, E&gt; erbt von tuple&lt;D, E&gt; erbt von tuple&lt;D&gt;.</p>
</blockquote>
<p>Das ist eine Möglichkeit, so macht es z.B. g++; Mehrfachvererbung wäre auch möglich:<br />
tuple&lt;A,B,C&gt; erbt dann von __tuple_element&lt;0,A&gt;, __tuple_element&lt;1,B&gt;, __tuple_element&lt;2,C&gt; usw.<br />
Möglicherweise kann das eine besser als das andere optimiert werden, das wäre dann allerdings ein reines QoI-Artefakt.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2143251</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2143251</guid><dc:creator><![CDATA[camper]]></dc:creator><pubDate>Fri, 11 Nov 2011 12:50:47 GMT</pubDate></item><item><title><![CDATA[Reply to Char und Float im Struct 8 Byte groß? on Fri, 11 Nov 2011 13:22:18 GMT]]></title><description><![CDATA[<p>Also lohnt es sich, einen &quot;Bug&quot;report zu schreiben?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2143263</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2143263</guid><dc:creator><![CDATA[Sinnfrei??]]></dc:creator><pubDate>Fri, 11 Nov 2011 13:22:18 GMT</pubDate></item><item><title><![CDATA[Reply to Char und Float im Struct 8 Byte groß? on Fri, 11 Nov 2011 14:42:57 GMT]]></title><description><![CDATA[<p>C++03 schrieb:</p>
<blockquote>
<p>1.8 The C+ + object model [intro.object]<br />
...<br />
5 Unless it is a bit-field (9.6), a most derived object shall have a non-zero size and shall occupy one or more bytes of storage. Base class sub-objects may have zero size. An object of POD4) type (3.9) shall occupy contiguous bytes of storage.</p>
</blockquote>
<p>C++11 schrieb:</p>
<blockquote>
<p>1.8 The C++ object model [intro.object]<br />
...<br />
6 Unless an object is a bit-field or a base class subobject of zero size, the address of that object is the address of the first byte it occupies. Two objects that are not bit-fields may have the same address if one is a subobject of the other, or if at least one is a base class subobject of zero size and they are of different types; otherwise, they shall have distinct addresses.4</p>
<ol start="4">
<li>Under the “as-if” rule an implementation is allowed to store two objects at the same machine address or not store an object at all if the program cannot observe the difference (1.9).</li>
</ol>
</blockquote>
<p>Das Verhalten von msvc ist möglicherweise beim alten Standard noch zulässig, einen Bug-Report halte ich hier nicht für besonders sinnvoll. Im Zweifelsfall ist das bei Microsoft sowieso eher ein Feature.<br />
<a href="http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_defects.html#1189" rel="nofollow">Hier</a> ist ein verwandter Report, mit Änderungsdatum März 2011. Trotzdem ist dieser Teil offenbar danach noch einmal geändert worden. Die gegenwärtigen Regeln sind also noch ziemlich neu.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2143297</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2143297</guid><dc:creator><![CDATA[camper]]></dc:creator><pubDate>Fri, 11 Nov 2011 14:42:57 GMT</pubDate></item></channel></rss>