<?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[Größe einer Struktur]]></title><description><![CDATA[<p>Hallo,<br />
wenn ich die Größe einer Struktur z.B.</p>
<pre><code>typedef struct Tmod0_param
	{
		unsigned short a;
		unsigned char b;
		unsigned char c;
		unsigned char d;
	} Tmod0_param;
</code></pre>
<p>mit</p>
<pre><code>sizeof(Tmod0_param);
</code></pre>
<p>auslese kommt 6 oder 7 raus und nicht 5. Wieso ist das so und vor allem was kann ich dagegen tun?</p>
<p>Danke für jeden Tip<br />
Gruß Dscho</p>
]]></description><link>https://www.c-plusplus.net/forum/topic/168728/größe-einer-struktur</link><generator>RSS for Node</generator><lastBuildDate>Wed, 16 Sep 2026 12:53:54 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/168728.rss" rel="self" type="application/rss+xml"/><pubDate>Wed, 27 Dec 2006 11:24:08 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to Größe einer Struktur on Wed, 27 Dec 2006 11:24:08 GMT]]></title><description><![CDATA[<p>Hallo,<br />
wenn ich die Größe einer Struktur z.B.</p>
<pre><code>typedef struct Tmod0_param
	{
		unsigned short a;
		unsigned char b;
		unsigned char c;
		unsigned char d;
	} Tmod0_param;
</code></pre>
<p>mit</p>
<pre><code>sizeof(Tmod0_param);
</code></pre>
<p>auslese kommt 6 oder 7 raus und nicht 5. Wieso ist das so und vor allem was kann ich dagegen tun?</p>
<p>Danke für jeden Tip<br />
Gruß Dscho</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1198357</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1198357</guid><dc:creator><![CDATA[dscho]]></dc:creator><pubDate>Wed, 27 Dec 2006 11:24:08 GMT</pubDate></item><item><title><![CDATA[Reply to Größe einer Struktur on Wed, 27 Dec 2006 11:36:01 GMT]]></title><description><![CDATA[<p>Das liegt am Byte Padding, der Compiler ordnet die Daten an geraden Bytegrenzen an.<br />
Siehe auch <a href="http://www.eventhelix.com/RealtimeMantra/ByteAlignmentAndOrdering.htm" rel="nofollow">http://www.eventhelix.com/RealtimeMantra/ByteAlignmentAndOrdering.htm</a></p>
]]></description><link>https://www.c-plusplus.net/forum/post/1198365</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1198365</guid><dc:creator><![CDATA[THX 1138]]></dc:creator><pubDate>Wed, 27 Dec 2006 11:36:01 GMT</pubDate></item><item><title><![CDATA[Reply to Größe einer Struktur on Wed, 27 Dec 2006 11:37:03 GMT]]></title><description><![CDATA[<p>short = 1 byte<br />
char = 2 byte</p>
<p>short + char + char + char = 7<br />
(no garanty #gg)</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1198368</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1198368</guid><dc:creator><![CDATA[EXDW]]></dc:creator><pubDate>Wed, 27 Dec 2006 11:37:03 GMT</pubDate></item><item><title><![CDATA[Reply to Größe einer Struktur on Wed, 27 Dec 2006 11:43:04 GMT]]></title><description><![CDATA[<p>sizeof(char) = 1 !!</p>
<blockquote>
<p>ISO/IEC 9899:1999(E)</p>
<p>6.5.3.4 The sizeof operator</p>
<p>3 When applied to an operand that has type char, unsigned char,<br />
or signed char, (or a qualified version thereof) the result<br />
is 1. When applied to an operand that has array type, the result<br />
is the total number of bytes in the array. When applied to an<br />
operand that has structure or union type, the result is the total<br />
number of bytes in such an object, including internal and trailing<br />
padding.</p>
</blockquote>
]]></description><link>https://www.c-plusplus.net/forum/post/1198371</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1198371</guid><dc:creator><![CDATA[THX 1138]]></dc:creator><pubDate>Wed, 27 Dec 2006 11:43:04 GMT</pubDate></item><item><title><![CDATA[Reply to Größe einer Struktur on Wed, 27 Dec 2006 12:10:17 GMT]]></title><description><![CDATA[<p>Mr Evil schrieb:</p>
<blockquote>
<p>short = 1 byte<br />
char = 2 byte</p>
<p>short + char + char + char = 7<br />
(no garanty #gg)</p>
</blockquote>
<p><img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f62e.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--face_with_open_mouth"
      title=":open_mouth:"
      alt="😮"
    /> Nette Ansicht. Eigentlich is das etwas anders. Wie bereits gesagt ist sizeof( char ) immer gleich 1. sizeof( short ) kann variieren, ich nehme aber fast an dass das in dem Fall 2 entspricht.<br />
D.h. 2 + 1 + 1 + 1 = 5, sizeof( Tmod0_param ) ist aber 6, was auf die sog. Paddingbytes zurückzuführen ist die vom Compiler hinzugefügt werden (-&gt; gibts genug Artikel in Google die das Thema behandeln). Im Grund dreht es sich um eine möglichst optimale Verteilung der Objekte im Speicher und optimalen Zugriffsmöglichkeiten.</p>
<p>Visual Studio bietet die Möglichkeit das Erzeugen von Paddingsbytes über pramga Direktiven zu unterbinden:</p>
<pre><code class="language-cpp">#pragma pack( push, 1 )

typedef struct Tmod0_param
{
	unsigned short a;
	unsigned char b;
	unsigned char c;
	unsigned char d;
} Tmod0_param;

#pragma pop
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/1198384</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1198384</guid><dc:creator><![CDATA[David_pb]]></dc:creator><pubDate>Wed, 27 Dec 2006 12:10:17 GMT</pubDate></item><item><title><![CDATA[Reply to Größe einer Struktur on Wed, 27 Dec 2006 12:34:05 GMT]]></title><description><![CDATA[<p>Vielen Dank für die vielen und schnellen Antworten</p>
<p>Gruß Dscho</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1198400</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1198400</guid><dc:creator><![CDATA[dscho]]></dc:creator><pubDate>Wed, 27 Dec 2006 12:34:05 GMT</pubDate></item></channel></rss>