<?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/"
		>
<channel>
	<title>Comments on: C Structures</title>
	<atom:link href="http://iPhoneDeveloperTips.com/c/c-structures.html/feed" rel="self" type="application/rss+xml" />
	<link>http://iPhoneDeveloperTips.com/c/c-structures.html</link>
	<description>Tips and Tricks for iPhone developers</description>
	<lastBuildDate>Wed, 08 Sep 2010 06:15:39 +0000</lastBuildDate>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
	<item>
		<title>By: pat</title>
		<link>http://iPhoneDeveloperTips.com/c/c-structures.html/comment-page-1#comment-149</link>
		<dc:creator>pat</dc:creator>
		<pubDate>Mon, 15 Dec 2008 23:30:01 +0000</pubDate>
		<guid isPermaLink="false">http://iPhoneDeveloperTips.com/?p=955#comment-149</guid>
		<description>@Sven:  With structs you have better control over memory packing and allocation alignment, which comes in handy with large arrays.

If you have an array of structs vs an array of objects,  you can do things like allocate your struct array memory aligned to a page boundry, then pack your structs to take advantage of the cache line size.   This helps speed things up memory access - like if you have a large number of elements in a struct or object array that you have to walk through.

With a struct, you can order your variables (and pointers to functions) based on frequency of use.  If you have a variable in your struct that is accessed much more than the rest, if it is first you can avoid one addition operation each time it is accessed.  

Because you can pack your stuct as you see fit (with some limitations), you can process it like any other memory field (sort of like a union).  For example:

- - -

typedef struct pixel { uchar8 r,b,g,a };  /* 4 bytes, one for each color + alpha */
unsigned int *screenAsInt;

pixel screen[20*20]; /* allocate a 20x20 bitmap... (questionable to put on the stack) */
screenAsInt = (uint32 *)screen;

/* roll our own SIMD to set each pixel to 0x00 (black) */
f=20*20;  /* counter */
do {
   f--;
   *screenAsInt=0x00; /* this sets all four chars to 0x00 */
   screenAsInt++;  /* go to the next pixel */
while (f&gt;0);

/* set first pixel to red */
screen[0].red=255;

- - -

This is really nice for speeding up code.  You can get huge performance gains for graphics and audio processing, while still having the convenience of element access with stucts.

Also, (at least with C++, I&#039;m just learning ObjC, but think it&#039;s the same), every method automatically has at least one more calling argument added for &#039;this&#039;.  CPUs have a limited number of registers for arguments, and when they run out the compiler allocates and passes arguments on the stack (which is much slower).   This is only a problem when the object pointer is passed and not used.


Another trick you can do with structs:  You can have pointers to functions in stucts, but unlike method pointers in classes, you can change those pointers dynamically (though you can have the same thing in C++ and probably ObjC, it&#039;s less efficient.)  For Example:

- - -

typedef struct shape {
    ...
    (void *) renderMe(void *this);
    ...
}

shape transformer;

transformer.renderMe = &amp;drawCar;
transformer.renderMe(&amp;transformer);  /* draw as car */

transformer.renderMe = &amp;drawRobot;
transformer.renderMe(&amp;transformer); /* draw as robot (same function call) */

- - -  

Also, it is easier to share struct data between programs compiled with different compilers, as long as you pack your struct correctly.</description>
		<content:encoded><![CDATA[<p>@Sven:  With structs you have better control over memory packing and allocation alignment, which comes in handy with large arrays.</p>
<p>If you have an array of structs vs an array of objects,  you can do things like allocate your struct array memory aligned to a page boundry, then pack your structs to take advantage of the cache line size.   This helps speed things up memory access &#8211; like if you have a large number of elements in a struct or object array that you have to walk through.</p>
<p>With a struct, you can order your variables (and pointers to functions) based on frequency of use.  If you have a variable in your struct that is accessed much more than the rest, if it is first you can avoid one addition operation each time it is accessed.  </p>
<p>Because you can pack your stuct as you see fit (with some limitations), you can process it like any other memory field (sort of like a union).  For example:</p>
<p>- &#8211; -</p>
<p>typedef struct pixel { uchar8 r,b,g,a };  /* 4 bytes, one for each color + alpha */<br />
unsigned int *screenAsInt;</p>
<p>pixel screen[20*20]; /* allocate a 20&#215;20 bitmap&#8230; (questionable to put on the stack) */<br />
screenAsInt = (uint32 *)screen;</p>
<p>/* roll our own SIMD to set each pixel to 0&#215;00 (black) */<br />
f=20*20;  /* counter */<br />
do {<br />
   f&#8211;;<br />
   *screenAsInt=0&#215;00; /* this sets all four chars to 0&#215;00 */<br />
   screenAsInt++;  /* go to the next pixel */<br />
while (f&gt;0);</p>
<p>/* set first pixel to red */<br />
screen[0].red=255;</p>
<p>- &#8211; -</p>
<p>This is really nice for speeding up code.  You can get huge performance gains for graphics and audio processing, while still having the convenience of element access with stucts.</p>
<p>Also, (at least with C++, I&#8217;m just learning ObjC, but think it&#8217;s the same), every method automatically has at least one more calling argument added for &#8216;this&#8217;.  CPUs have a limited number of registers for arguments, and when they run out the compiler allocates and passes arguments on the stack (which is much slower).   This is only a problem when the object pointer is passed and not used.</p>
<p>Another trick you can do with structs:  You can have pointers to functions in stucts, but unlike method pointers in classes, you can change those pointers dynamically (though you can have the same thing in C++ and probably ObjC, it&#8217;s less efficient.)  For Example:</p>
<p>- &#8211; -</p>
<p>typedef struct shape {<br />
    &#8230;<br />
    (void *) renderMe(void *this);<br />
    &#8230;<br />
}</p>
<p>shape transformer;</p>
<p>transformer.renderMe = &drawCar;<br />
transformer.renderMe(&amp;transformer);  /* draw as car */</p>
<p>transformer.renderMe = &drawRobot;<br />
transformer.renderMe(&amp;transformer); /* draw as robot (same function call) */</p>
<p>- &#8211; -  </p>
<p>Also, it is easier to share struct data between programs compiled with different compilers, as long as you pack your struct correctly.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Sven</title>
		<link>http://iPhoneDeveloperTips.com/c/c-structures.html/comment-page-1#comment-113</link>
		<dc:creator>Sven</dc:creator>
		<pubDate>Fri, 28 Nov 2008 18:09:31 +0000</pubDate>
		<guid isPermaLink="false">http://iPhoneDeveloperTips.com/?p=955#comment-113</guid>
		<description>Using a struct looks very convenient. Interesting how Steve points out some potential gains with respect to memory utilization.

Are there any other memory related advantages though in comparison to declaring an Objective-C class with a .m and .h file?</description>
		<content:encoded><![CDATA[<p>Using a struct looks very convenient. Interesting how Steve points out some potential gains with respect to memory utilization.</p>
<p>Are there any other memory related advantages though in comparison to declaring an Objective-C class with a .m and .h file?</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Shawn Craver</title>
		<link>http://iPhoneDeveloperTips.com/c/c-structures.html/comment-page-1#comment-97</link>
		<dc:creator>Shawn Craver</dc:creator>
		<pubDate>Wed, 05 Nov 2008 15:31:24 +0000</pubDate>
		<guid isPermaLink="false">http://iPhoneDeveloperTips.com/?p=955#comment-97</guid>
		<description>Glad to see this. It&#039;s a nice refresher now that I&#039;m getting into Objective-C, seeing as I haven&#039;t done any serious C work in 10 years or more.</description>
		<content:encoded><![CDATA[<p>Glad to see this. It&#8217;s a nice refresher now that I&#8217;m getting into Objective-C, seeing as I haven&#8217;t done any serious C work in 10 years or more.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: John</title>
		<link>http://iPhoneDeveloperTips.com/c/c-structures.html/comment-page-1#comment-95</link>
		<dc:creator>John</dc:creator>
		<pubDate>Mon, 03 Nov 2008 16:45:33 +0000</pubDate>
		<guid isPermaLink="false">http://iPhoneDeveloperTips.com/?p=955#comment-95</guid>
		<description>Great addendum Steve, thanks for the additional information.</description>
		<content:encoded><![CDATA[<p>Great addendum Steve, thanks for the additional information.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Steve</title>
		<link>http://iPhoneDeveloperTips.com/c/c-structures.html/comment-page-1#comment-94</link>
		<dc:creator>Steve</dc:creator>
		<pubDate>Mon, 03 Nov 2008 16:21:05 +0000</pubDate>
		<guid isPermaLink="false">http://iPhoneDeveloperTips.com/?p=955#comment-94</guid>
		<description>Good information John.

There are a couple other things that are good to know with structs. A struct can have one or more &quot;declarators&quot; which allow you to define and declare in one step, as follows:

struct MyStruct
{
    int a;
    int b;
} myStruct1, myStruct2, myStruct3;

The declarators declare instances of the struct which are &quot;declared&quot; at the point of definition of the struct itself. You can also declare pointers and arrays, or even mix and match, as follows:

struct MyStruct
{
    int a;
    int b;
} *myStructPointer1, myStructArray[10], **myStructPointerToPointer;

You may also add initializers to the declarators:

struct MyStruct
{
    int a;
    int b;
} myStruct1 = { 256, 128};

Which will initialize a to 256 and b to 128.

It is also useful to consider how structs are passed to functions. There are basically two ways to do this, by value or by pointer. Consider the following method signature:

void someMethod(struct MyStruct aStruct);

If this method is called as follows:

    MyStruct myStruct = {0, 0};
    someMethod(myStruct);

This declares and initializes myStruct, and then passes it by value to someMethod(). Essentially what will happen is that the compiler will generate code to ensure that a copy of the original myStruct is made (typically on the stack), and then this copy will be passed to someMethod. Note that if someMethod() makes any modifications to the date in the struct, those changes will (in this case) be lost when the method returns because someMethod() is working on a copy. 
An alternative is to pass a pointer to the struct. Consider this alternative method signature:

void someMethod2(struct MyStruct *myStructPointer);

This takes a pointer to a MyStruct. We might then use this sequence:

    MyStruct myStruct = { 0, 0 };
    someMethod2(&amp;myStruct);

Notice that we&#039;ve used the &quot;address of&quot; operator &quot;&amp;&quot; to take the address of our struct and pass this to the method. This skips the stack copy (which could be unwanted overhead if the struct is large) and also has the side effect that someMethod2() can now modify the original struct (which may or may not be what you want - you can use const modifiers on the parameters to provide &quot;read only&quot; pointers, in this case const struct MyStruct*).
It is also worth noting that the syntax to dereference a pointer to a struct would be as follows:

void someMethod2(struct MyStruct *myStructPointer)
{
    int a = myStructPointer-&gt;a;
    int b = myStructPointer-&gt;b;
    myStructPointer-&gt;a = 1;
    myStructPointer-&gt;b = 2;
}
</description>
		<content:encoded><![CDATA[<p>Good information John.</p>
<p>There are a couple other things that are good to know with structs. A struct can have one or more &#8220;declarators&#8221; which allow you to define and declare in one step, as follows:</p>
<p>struct MyStruct<br />
{<br />
    int a;<br />
    int b;<br />
} myStruct1, myStruct2, myStruct3;</p>
<p>The declarators declare instances of the struct which are &#8220;declared&#8221; at the point of definition of the struct itself. You can also declare pointers and arrays, or even mix and match, as follows:</p>
<p>struct MyStruct<br />
{<br />
    int a;<br />
    int b;<br />
} *myStructPointer1, myStructArray[10], **myStructPointerToPointer;</p>
<p>You may also add initializers to the declarators:</p>
<p>struct MyStruct<br />
{<br />
    int a;<br />
    int b;<br />
} myStruct1 = { 256, 128};</p>
<p>Which will initialize a to 256 and b to 128.</p>
<p>It is also useful to consider how structs are passed to functions. There are basically two ways to do this, by value or by pointer. Consider the following method signature:</p>
<p>void someMethod(struct MyStruct aStruct);</p>
<p>If this method is called as follows:</p>
<p>    MyStruct myStruct = {0, 0};<br />
    someMethod(myStruct);</p>
<p>This declares and initializes myStruct, and then passes it by value to someMethod(). Essentially what will happen is that the compiler will generate code to ensure that a copy of the original myStruct is made (typically on the stack), and then this copy will be passed to someMethod. Note that if someMethod() makes any modifications to the date in the struct, those changes will (in this case) be lost when the method returns because someMethod() is working on a copy.<br />
An alternative is to pass a pointer to the struct. Consider this alternative method signature:</p>
<p>void someMethod2(struct MyStruct *myStructPointer);</p>
<p>This takes a pointer to a MyStruct. We might then use this sequence:</p>
<p>    MyStruct myStruct = { 0, 0 };<br />
    someMethod2(&amp;myStruct);</p>
<p>Notice that we&#8217;ve used the &#8220;address of&#8221; operator &#8220;&amp;&#8221; to take the address of our struct and pass this to the method. This skips the stack copy (which could be unwanted overhead if the struct is large) and also has the side effect that someMethod2() can now modify the original struct (which may or may not be what you want &#8211; you can use const modifiers on the parameters to provide &#8220;read only&#8221; pointers, in this case const struct MyStruct*).<br />
It is also worth noting that the syntax to dereference a pointer to a struct would be as follows:</p>
<p>void someMethod2(struct MyStruct *myStructPointer)<br />
{<br />
    int a = myStructPointer-&gt;a;<br />
    int b = myStructPointer-&gt;b;<br />
    myStructPointer-&gt;a = 1;<br />
    myStructPointer-&gt;b = 2;<br />
}</p>
]]></content:encoded>
	</item>
</channel>
</rss>
