<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:georss="http://www.georss.org/georss" xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#" xmlns:media="http://search.yahoo.com/mrss/"
		>
<channel>
	<title>Comments on: Be careful with packed structures!</title>
	<atom:link href="http://axelio.wordpress.com/2007/07/24/be-careful-with-packed-structures/feed/" rel="self" type="application/rss+xml" />
	<link>http://axelio.wordpress.com/2007/07/24/be-careful-with-packed-structures/</link>
	<description>... and the art of debugging a blank sheet of paper.</description>
	<lastBuildDate>Sat, 24 Oct 2009 11:00:04 +0000</lastBuildDate>
	<generator>http://wordpress.com/</generator>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
		<item>
		<title>By: aleix</title>
		<link>http://axelio.wordpress.com/2007/07/24/be-careful-with-packed-structures/#comment-323</link>
		<dc:creator>aleix</dc:creator>
		<pubDate>Sun, 05 Aug 2007 23:24:53 +0000</pubDate>
		<guid isPermaLink="false">http://axelio.wordpress.com/2007/07/24/be-careful-with-packed-structures/#comment-323</guid>
		<description>Yes, it&#039;s true that packed structures are not really recommended because of portability. I&#039;m using them on an embedded system with an SPARC CPU and they work fine (in this project), I don&#039;t really see why your code doesn&#039;t work (what is the read_from_net function doing?). If you have a buffer with the data coming from the network and just memcpy it to the address of your packed structure it works fine, you only need to take into account the endianess, all the rest (field alignment) is automatically done by the compiler.</description>
		<content:encoded><![CDATA[<p>Yes, it&#8217;s true that packed structures are not really recommended because of portability. I&#8217;m using them on an embedded system with an SPARC CPU and they work fine (in this project), I don&#8217;t really see why your code doesn&#8217;t work (what is the read_from_net function doing?). If you have a buffer with the data coming from the network and just memcpy it to the address of your packed structure it works fine, you only need to take into account the endianess, all the rest (field alignment) is automatically done by the compiler.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: nico</title>
		<link>http://axelio.wordpress.com/2007/07/24/be-careful-with-packed-structures/#comment-317</link>
		<dc:creator>nico</dc:creator>
		<pubDate>Sun, 05 Aug 2007 05:36:58 +0000</pubDate>
		<guid isPermaLink="false">http://axelio.wordpress.com/2007/07/24/be-careful-with-packed-structures/#comment-317</guid>
		<description>So you&#039;re doing something like that:
struct my_packed_struct s;
read_from_net(&amp;s, sizeof(s));
/* ... */
do_stuff(s.i)
? That will crash on everything but on x86 architectures, because usually you can&#039;t read integers on non-word boundaries. Packed structures are very unportable.</description>
		<content:encoded><![CDATA[<p>So you&#8217;re doing something like that:<br />
struct my_packed_struct s;<br />
read_from_net(&amp;s, sizeof(s));<br />
/* &#8230; */<br />
do_stuff(s.i)<br />
? That will crash on everything but on x86 architectures, because usually you can&#8217;t read integers on non-word boundaries. Packed structures are very unportable.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: aleix</title>
		<link>http://axelio.wordpress.com/2007/07/24/be-careful-with-packed-structures/#comment-271</link>
		<dc:creator>aleix</dc:creator>
		<pubDate>Wed, 25 Jul 2007 09:06:37 +0000</pubDate>
		<guid isPermaLink="false">http://axelio.wordpress.com/2007/07/24/be-careful-with-packed-structures/#comment-271</guid>
		<description>I see... this is why variable attribute is not working for the internal structure, so you are forced to pack the type if you want it to be packed.

As you say, this is a non-standard feature (even most compilers support it), and of course it has a lot of performance penalties as the compiler needs to generate code to unpack unaligned data. We&#039;re using it to easily represent network packets in C, and we found that it was a pretty good way (at list for writing the code to manage packet fields).

Thanks!</description>
		<content:encoded><![CDATA[<p>I see&#8230; this is why variable attribute is not working for the internal structure, so you are forced to pack the type if you want it to be packed.</p>
<p>As you say, this is a non-standard feature (even most compilers support it), and of course it has a lot of performance penalties as the compiler needs to generate code to unpack unaligned data. We&#8217;re using it to easily represent network packets in C, and we found that it was a pretty good way (at list for writing the code to manage packet fields).</p>
<p>Thanks!</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: drj11</title>
		<link>http://axelio.wordpress.com/2007/07/24/be-careful-with-packed-structures/#comment-270</link>
		<dc:creator>drj11</dc:creator>
		<pubDate>Wed, 25 Jul 2007 08:39:32 +0000</pubDate>
		<guid isPermaLink="false">http://axelio.wordpress.com/2007/07/24/be-careful-with-packed-structures/#comment-270</guid>
		<description>It pretty much has to work this way.  Consider what happens when I have a random pointer to the unpacked struct:

struct my_unpacked_struct *p;
p = ...;
printf(&quot;%d\n&quot;, p-&gt;i);

When the compiler is generating code for «p-&gt;i» it has to know what the offset is to get from the beginning of the struct to the &quot;i&quot; field.  This is a fact about the struct, not about where the struct is.  The compiler can&#039;t tell whether the pointer p points to an &quot;ordinary&quot; struct my_unpacked_struct or one that is embedded inside another (packed) struct.  There&#039;s no difference.  The pointer doesn&#039;t stored any information that will allow it tell.

That&#039;s why embedding a struct can&#039;t change how the embedded struct is packed.

There&#039;s another reason you should be careful with packed structures: they&#039;re non-standard.</description>
		<content:encoded><![CDATA[<p>It pretty much has to work this way.  Consider what happens when I have a random pointer to the unpacked struct:</p>
<p>struct my_unpacked_struct *p;<br />
p = &#8230;;<br />
printf(&#8220;%d\n&#8221;, p-&gt;i);</p>
<p>When the compiler is generating code for «p-&gt;i» it has to know what the offset is to get from the beginning of the struct to the &#8220;i&#8221; field.  This is a fact about the struct, not about where the struct is.  The compiler can&#8217;t tell whether the pointer p points to an &#8220;ordinary&#8221; struct my_unpacked_struct or one that is embedded inside another (packed) struct.  There&#8217;s no difference.  The pointer doesn&#8217;t stored any information that will allow it tell.</p>
<p>That&#8217;s why embedding a struct can&#8217;t change how the embedded struct is packed.</p>
<p>There&#8217;s another reason you should be careful with packed structures: they&#8217;re non-standard.</p>
]]></content:encoded>
	</item>
</channel>
</rss>
