<?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: Efficient C Tips #5 &#8211; Make &#8216;local&#8217; functions &#8216;static&#8217;</title>
	<atom:link href="http://embeddedgurus.com/stack-overflow/2008/12/efficient-c-tips-5-make-local-functions-static/feed/" rel="self" type="application/rss+xml" />
	<link>http://embeddedgurus.com/stack-overflow/2008/12/efficient-c-tips-5-make-local-functions-static/</link>
	<description>Thoughts on embedded systems by Nigel Jones</description>
	<lastBuildDate>Sun, 06 May 2012 10:34:02 +0000</lastBuildDate>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
	<item>
		<title>By: C language do structures always have to be defined globally ?</title>
		<link>http://embeddedgurus.com/stack-overflow/2008/12/efficient-c-tips-5-make-local-functions-static/comment-page-1/#comment-14504</link>
		<dc:creator>C language do structures always have to be defined globally ?</dc:creator>
		<pubDate>Fri, 17 Feb 2012 22:31:35 +0000</pubDate>
		<guid isPermaLink="false">http://www.gfcdev.org/test-stack/2008/12/13/efficient-c-tips-5-make-local-functions-static/#comment-14504</guid>
		<description>[...] Originally Posted by millgates   As I said, if you define structure locally, you can only use it in the function in which it is defined, so unless you are defining the function inside the function where the struct is defined (which in C is not possible), you can not use that struct as an argument or return type of any function. If you want to do that, declare the struct globally.    About nested functions in C: link.  To my knowledge, defining local structures and/or local (nested) functions is not the preferred way of programming in C (as opposed to, say, scheme or ruby). You can make a function local to a compilation unit by defining is as static. Such a function is not visible outside of the file it is defined in. Static functions has some other advantages. [...]</description>
		<content:encoded><![CDATA[<p>[...] Originally Posted by millgates   As I said, if you define structure locally, you can only use it in the function in which it is defined, so unless you are defining the function inside the function where the struct is defined (which in C is not possible), you can not use that struct as an argument or return type of any function. If you want to do that, declare the struct globally.    About nested functions in C: link.  To my knowledge, defining local structures and/or local (nested) functions is not the preferred way of programming in C (as opposed to, say, scheme or ruby). You can make a function local to a compilation unit by defining is as static. Such a function is not visible outside of the file it is defined in. Static functions has some other advantages. [...]</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Krzysztof Wesołowski</title>
		<link>http://embeddedgurus.com/stack-overflow/2008/12/efficient-c-tips-5-make-local-functions-static/comment-page-1/#comment-7399</link>
		<dc:creator>Krzysztof Wesołowski</dc:creator>
		<pubDate>Mon, 10 Oct 2011 10:07:42 +0000</pubDate>
		<guid isPermaLink="false">http://www.gfcdev.org/test-stack/2008/12/13/efficient-c-tips-5-make-local-functions-static/#comment-7399</guid>
		<description>In C++ you use this to realize Singleton Pattern.</description>
		<content:encoded><![CDATA[<p>In C++ you use this to realize Singleton Pattern.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Gauthier</title>
		<link>http://embeddedgurus.com/stack-overflow/2008/12/efficient-c-tips-5-make-local-functions-static/comment-page-1/#comment-2979</link>
		<dc:creator>Gauthier</dc:creator>
		<pubDate>Wed, 22 Dec 2010 14:27:41 +0000</pubDate>
		<guid isPermaLink="false">http://www.gfcdev.org/test-stack/2008/12/13/efficient-c-tips-5-make-local-functions-static/#comment-2979</guid>
		<description>Ah, I read now your reply, which makes mine a pale copy.</description>
		<content:encoded><![CDATA[<p>Ah, I read now your reply, which makes mine a pale copy.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Gauthier</title>
		<link>http://embeddedgurus.com/stack-overflow/2008/12/efficient-c-tips-5-make-local-functions-static/comment-page-1/#comment-2978</link>
		<dc:creator>Gauthier</dc:creator>
		<pubDate>Wed, 22 Dec 2010 14:25:04 +0000</pubDate>
		<guid isPermaLink="false">http://www.gfcdev.org/test-stack/2008/12/13/efficient-c-tips-5-make-local-functions-static/#comment-2978</guid>
		<description>Actually, I like to think of static as always having the same meaning: &quot;make that scope-limited, but with lifespan as long as the whole program&quot;.
This definition applies to all the three common uses of static (local var in a function, global var in a module, function in a module).

I understand though, that functions already have all max lifespan, and that local vars are already scope-limited. But I think that this definition helps understand why the same keyword is used in all cases. 

I have very little insight in compilers, but I suspect that the parser does just that when encountering static?</description>
		<content:encoded><![CDATA[<p>Actually, I like to think of static as always having the same meaning: &#8220;make that scope-limited, but with lifespan as long as the whole program&#8221;.<br />
This definition applies to all the three common uses of static (local var in a function, global var in a module, function in a module).</p>
<p>I understand though, that functions already have all max lifespan, and that local vars are already scope-limited. But I think that this definition helps understand why the same keyword is used in all cases. </p>
<p>I have very little insight in compilers, but I suspect that the parser does just that when encountering static?</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Matthias Arndt</title>
		<link>http://embeddedgurus.com/stack-overflow/2008/12/efficient-c-tips-5-make-local-functions-static/comment-page-1/#comment-2965</link>
		<dc:creator>Matthias Arndt</dc:creator>
		<pubDate>Sat, 18 Dec 2010 14:39:41 +0000</pubDate>
		<guid isPermaLink="false">http://www.gfcdev.org/test-stack/2008/12/13/efficient-c-tips-5-make-local-functions-static/#comment-2965</guid>
		<description>That is a useful workaround. I trapped myself in the same way in the past. And I openly admit that I simply never knew that static functions in C are module local until recently.

By that little define one adds quite a bit of readability to the code, yet complies to the standard.

Something valuable learned here!

Matthias</description>
		<content:encoded><![CDATA[<p>That is a useful workaround. I trapped myself in the same way in the past. And I openly admit that I simply never knew that static functions in C are module local until recently.</p>
<p>By that little define one adds quite a bit of readability to the code, yet complies to the standard.</p>
<p>Something valuable learned here!</p>
<p>Matthias</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: MarkM</title>
		<link>http://embeddedgurus.com/stack-overflow/2008/12/efficient-c-tips-5-make-local-functions-static/comment-page-1/#comment-2870</link>
		<dc:creator>MarkM</dc:creator>
		<pubDate>Fri, 03 Dec 2010 15:15:22 +0000</pubDate>
		<guid isPermaLink="false">http://www.gfcdev.org/test-stack/2008/12/13/efficient-c-tips-5-make-local-functions-static/#comment-2870</guid>
		<description>C++ retains the C semantics of &#039;static&#039; and adds an additional use for it in support of object-oriented programming:  Within a class/struct, a static member of the class/struct has a single global instance, can be referred to without dereferencing an object of the class/struct&#039;s type, and cannot implicitly access members of any such object.  A static class/struct method (function) does not have a &#039;this&#039; pointer, as it cannot be invoked on an object of the class/struct.

I think OO programming is not widely used within embedded programming, so I&#039;ll clarify:  Within C++, a &#039;class&#039; is basically the same thing as a &#039;struct&#039;.  The ONLY difference, and I truly mean ONLY, is that in a struct, members are by default public (anyone who has a pointer to an instance of a struct has free access to its members), while in a class, members are by default private and must be declared public in order for outsiders to access them.  In C++, a class/struct can &quot;contain&quot; functions, which in OO parlance are usually called &#039;methods&#039;, though it means the same thing.  Instead of having bits &amp; pieces of code scattered throughout various functions all over your codebase that operate on data in a class/struct, in OOP, you group the data and the bits &amp; pieces of code that operate on that data together inside the class/struct.  And generally, if a you don&#039;t put any functions in the class/struct, you call it a struct, otherwise you call it a class.  Now when you create objects of these classes, they don&#039;t get extra copies of the code for the functions inside the class, there&#039;s really only one copy of the functions, but conceptually you can just go ahead and think of it as if they all have their own copies.  But &#039;static&#039; members are different: conceptually, you should (because it&#039;s correct) think of a static member as having only a single instance regardless of how many objects of that type are created.  So, for example, you could have a single static bool flag to flip all objects of that type between debug &amp; normal modes.  When you set the static bool debug flag true, it&#039;s true for all objects of that type.  In this sense, it&#039;s sort of like a global variable, but it’s grouped within the class definition because OO programmers tend to shudder at the thought of anything other than main() being global.  In addition to static class variables, a class can have static methods.  Static methods (functions) can be called directly; in a sense, they&#039;re sort of like global functions, but, ah, no, forget I said that.  If you have non-static class methods, these can only be &quot;called on&quot; an object of that type that already exists.  So, for example, if you have a class that contains a set of numbers, you might want to sort them.  So you would have a member method to tell an object of that type to sort itself.  If you have an object of your class numberList that you&#039;ve named &quot;myPhoneList&quot;, you could call myPhoneList-&gt;sort(), and it would sort itself.  But sometimes the purpose of a method doesn&#039;t exactly apply to a specific object of that type.  For example, you might want to know how many numberLists objects are in existence.  For that purpose, you don’t need any specific object of that type to find this out; your numberList class probably has a static member variable named instanceCount, but like all good OO programmers you made it private so nobody can see it.  But you made a method so folks can find out its value without accessing it directly, and of course it’s a static method because it doesn’t deal with any data related to any specific numberList.  Now anybody who knows that the numberList class exists can find out how many numberLists are out there floating  around in memory without actually knowing where any of them are.</description>
		<content:encoded><![CDATA[<p>C++ retains the C semantics of &#8216;static&#8217; and adds an additional use for it in support of object-oriented programming:  Within a class/struct, a static member of the class/struct has a single global instance, can be referred to without dereferencing an object of the class/struct&#8217;s type, and cannot implicitly access members of any such object.  A static class/struct method (function) does not have a &#8216;this&#8217; pointer, as it cannot be invoked on an object of the class/struct.</p>
<p>I think OO programming is not widely used within embedded programming, so I&#8217;ll clarify:  Within C++, a &#8216;class&#8217; is basically the same thing as a &#8216;struct&#8217;.  The ONLY difference, and I truly mean ONLY, is that in a struct, members are by default public (anyone who has a pointer to an instance of a struct has free access to its members), while in a class, members are by default private and must be declared public in order for outsiders to access them.  In C++, a class/struct can &#8220;contain&#8221; functions, which in OO parlance are usually called &#8216;methods&#8217;, though it means the same thing.  Instead of having bits &amp; pieces of code scattered throughout various functions all over your codebase that operate on data in a class/struct, in OOP, you group the data and the bits &amp; pieces of code that operate on that data together inside the class/struct.  And generally, if a you don&#8217;t put any functions in the class/struct, you call it a struct, otherwise you call it a class.  Now when you create objects of these classes, they don&#8217;t get extra copies of the code for the functions inside the class, there&#8217;s really only one copy of the functions, but conceptually you can just go ahead and think of it as if they all have their own copies.  But &#8216;static&#8217; members are different: conceptually, you should (because it&#8217;s correct) think of a static member as having only a single instance regardless of how many objects of that type are created.  So, for example, you could have a single static bool flag to flip all objects of that type between debug &amp; normal modes.  When you set the static bool debug flag true, it&#8217;s true for all objects of that type.  In this sense, it&#8217;s sort of like a global variable, but it’s grouped within the class definition because OO programmers tend to shudder at the thought of anything other than main() being global.  In addition to static class variables, a class can have static methods.  Static methods (functions) can be called directly; in a sense, they&#8217;re sort of like global functions, but, ah, no, forget I said that.  If you have non-static class methods, these can only be &#8220;called on&#8221; an object of that type that already exists.  So, for example, if you have a class that contains a set of numbers, you might want to sort them.  So you would have a member method to tell an object of that type to sort itself.  If you have an object of your class numberList that you&#8217;ve named &#8220;myPhoneList&#8221;, you could call myPhoneList-&gt;sort(), and it would sort itself.  But sometimes the purpose of a method doesn&#8217;t exactly apply to a specific object of that type.  For example, you might want to know how many numberLists objects are in existence.  For that purpose, you don’t need any specific object of that type to find this out; your numberList class probably has a static member variable named instanceCount, but like all good OO programmers you made it private so nobody can see it.  But you made a method so folks can find out its value without accessing it directly, and of course it’s a static method because it doesn’t deal with any data related to any specific numberList.  Now anybody who knows that the numberList class exists can find out how many numberLists are out there floating  around in memory without actually knowing where any of them are.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Gauthier</title>
		<link>http://embeddedgurus.com/stack-overflow/2008/12/efficient-c-tips-5-make-local-functions-static/comment-page-1/#comment-2167</link>
		<dc:creator>Gauthier</dc:creator>
		<pubDate>Tue, 28 Sep 2010 08:16:04 +0000</pubDate>
		<guid isPermaLink="false">http://www.gfcdev.org/test-stack/2008/12/13/efficient-c-tips-5-make-local-functions-static/#comment-2167</guid>
		<description>Could you please give an example where passing the address of a function static variable would be a good thing?</description>
		<content:encoded><![CDATA[<p>Could you please give an example where passing the address of a function static variable would be a good thing?</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: David Brown</title>
		<link>http://embeddedgurus.com/stack-overflow/2008/12/efficient-c-tips-5-make-local-functions-static/comment-page-1/#comment-2162</link>
		<dc:creator>David Brown</dc:creator>
		<pubDate>Mon, 27 Sep 2010 09:44:01 +0000</pubDate>
		<guid isPermaLink="false">http://www.gfcdev.org/test-stack/2008/12/13/efficient-c-tips-5-make-local-functions-static/#comment-2162</guid>
		<description>C and C++ use &quot;static&quot; in the same way, at least unless you are doing something really obscure as a challenge to the language-lawyers.

If a function is declared &quot;static&quot;, and you don&#039;t take its address, then the compiler knows it cannot be called outside the module.  It is therefore free to inline it, remove it, simplify it, change its calling conventions, or otherwise optimise it as it sees fit.  Whether this will reduce code size or not will depend on the compiler and your optimisation settings, but you are giving the compiler the best chance to do a better job.

With modern compilers, you should not have to explicitly declare a static function as &quot;inline&quot; unless you believe the compiler will do a poor job on its own (compiler heuristics are normally good, but not perfect).  In those cases, you may need something additional such as __attribute__((always_inline)) to force the behaviour you want.  Of course, sometimes adding &quot;inline&quot; makes your code clearer, in which case it&#039;s a good thing.  But otherwise let the compiler make the decision.

There are occasions when you want to use &quot;static&quot; in header files.  But remember that the data or functions are then independent for each module that includes the header.  Thus the main uses for &quot;static&quot; in header is for a modern and type-safe (at least, as type-safe as it gets in C/C++) replacements for pre-processor defines.  For example, you can replace:

#define magicNumber 100
#define magicFormula(x) ((x) * (x) + magicNumber)

with 

static const int magicNumber = 100;
static inline int magicFormula(int x) { return x * x * magicNumber; }</description>
		<content:encoded><![CDATA[<p>C and C++ use &#8220;static&#8221; in the same way, at least unless you are doing something really obscure as a challenge to the language-lawyers.</p>
<p>If a function is declared &#8220;static&#8221;, and you don&#8217;t take its address, then the compiler knows it cannot be called outside the module.  It is therefore free to inline it, remove it, simplify it, change its calling conventions, or otherwise optimise it as it sees fit.  Whether this will reduce code size or not will depend on the compiler and your optimisation settings, but you are giving the compiler the best chance to do a better job.</p>
<p>With modern compilers, you should not have to explicitly declare a static function as &#8220;inline&#8221; unless you believe the compiler will do a poor job on its own (compiler heuristics are normally good, but not perfect).  In those cases, you may need something additional such as __attribute__((always_inline)) to force the behaviour you want.  Of course, sometimes adding &#8220;inline&#8221; makes your code clearer, in which case it&#8217;s a good thing.  But otherwise let the compiler make the decision.</p>
<p>There are occasions when you want to use &#8220;static&#8221; in header files.  But remember that the data or functions are then independent for each module that includes the header.  Thus the main uses for &#8220;static&#8221; in header is for a modern and type-safe (at least, as type-safe as it gets in C/C++) replacements for pre-processor defines.  For example, you can replace:</p>
<p>#define magicNumber 100<br />
#define magicFormula(x) ((x) * (x) + magicNumber)</p>
<p>with </p>
<p>static const int magicNumber = 100;<br />
static inline int magicFormula(int x) { return x * x * magicNumber; }</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: David Brown</title>
		<link>http://embeddedgurus.com/stack-overflow/2008/12/efficient-c-tips-5-make-local-functions-static/comment-page-1/#comment-2161</link>
		<dc:creator>David Brown</dc:creator>
		<pubDate>Mon, 27 Sep 2010 09:31:22 +0000</pubDate>
		<guid isPermaLink="false">http://www.gfcdev.org/test-stack/2008/12/13/efficient-c-tips-5-make-local-functions-static/#comment-2161</guid>
		<description>&quot;static&quot; functions and data (whether the data is file-scope static or function-scope static) are the same as any other global functions or data, but with limited visibility.  Their lifetime scope is global - they exist from just before main() starts and live until just after main() ends.  But their name scope is limited to the enclosing scope (compilation unit or function).  This simply means that you can&#039;t access them by name outside that scope.

But since they still exist, you are free to take their address and pass it around the rest of the program.  Passing the address of a function-local static variable, for example, is perfectly valid and perfectly safe.

Of course, the optimisation advantages of statics are mostly lost if you take its address and pass it outside the current compilation unit.  For example, the compiler will generate the full code for a static function rather than inlining it if you&#039;ve taken its address.  And a &quot;static const int&quot; might be optimised away entirely by the compiler if you don&#039;t take its address.</description>
		<content:encoded><![CDATA[<p>&#8220;static&#8221; functions and data (whether the data is file-scope static or function-scope static) are the same as any other global functions or data, but with limited visibility.  Their lifetime scope is global &#8211; they exist from just before main() starts and live until just after main() ends.  But their name scope is limited to the enclosing scope (compilation unit or function).  This simply means that you can&#8217;t access them by name outside that scope.</p>
<p>But since they still exist, you are free to take their address and pass it around the rest of the program.  Passing the address of a function-local static variable, for example, is perfectly valid and perfectly safe.</p>
<p>Of course, the optimisation advantages of statics are mostly lost if you take its address and pass it outside the current compilation unit.  For example, the compiler will generate the full code for a static function rather than inlining it if you&#8217;ve taken its address.  And a &#8220;static const int&#8221; might be optimised away entirely by the compiler if you don&#8217;t take its address.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: David Brown</title>
		<link>http://embeddedgurus.com/stack-overflow/2008/12/efficient-c-tips-5-make-local-functions-static/comment-page-1/#comment-2160</link>
		<dc:creator>David Brown</dc:creator>
		<pubDate>Mon, 27 Sep 2010 09:23:30 +0000</pubDate>
		<guid isPermaLink="false">http://www.gfcdev.org/test-stack/2008/12/13/efficient-c-tips-5-make-local-functions-static/#comment-2160</guid>
		<description>If you think of static as meaning &quot;exists for the lifetime of the program, but only visible within the enclosing scope&quot;, then&quot;static&quot; means the same thing everywhere.</description>
		<content:encoded><![CDATA[<p>If you think of static as meaning &#8220;exists for the lifetime of the program, but only visible within the enclosing scope&#8221;, then&#8221;static&#8221; means the same thing everywhere.</p>
]]></content:encoded>
	</item>
</channel>
</rss>

