<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	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:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Linux Game Publishing Blog &#187; warnings</title>
	<atom:link href="http://blog.linuxgamepublishing.com/tag/warnings/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.linuxgamepublishing.com</link>
	<description>Commercial gaming for Linux</description>
	<lastBuildDate>Mon, 05 Apr 2010 14:50:32 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.4</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Getting gcc to warn you when you mess up stdargs</title>
		<link>http://blog.linuxgamepublishing.com/2010/01/20/getting-gcc-to-warn-you-when-you-mess-up-stdargs/</link>
		<comments>http://blog.linuxgamepublishing.com/2010/01/20/getting-gcc-to-warn-you-when-you-mess-up-stdargs/#comments</comments>
		<pubDate>Wed, 20 Jan 2010 12:02:48 +0000</pubDate>
		<dc:creator>Michael Simms (CEO and head of Development)</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[C]]></category>
		<category><![CDATA[compiler]]></category>
		<category><![CDATA[gcc]]></category>
		<category><![CDATA[printf]]></category>
		<category><![CDATA[stdargs]]></category>
		<category><![CDATA[warnings]]></category>

		<guid isPermaLink="false">http://blog.linuxgamepublishing.com/?p=405</guid>
		<description><![CDATA[Sometimes, you may write functions in C that do things in the same way as printf, using stdargs.
An example of this would be something like this short debug function
int ptf(const char *fmt,...)
{
  va_list varlist;
  FILE *fp=fopen("/tmp/debug","a");
  va_start(varlist,fmt);
  vfprintf(fp,fmt,varlist);
  va_end(varlist);
  fclose(fp);
}
This function isn&#8217;t rocket science, it just simply appends your [...]]]></description>
			<content:encoded><![CDATA[<p>Sometimes, you may write functions in C that do things in the same way as printf, using stdargs.</p>
<p>An example of this would be something like this short debug function</p>
<pre>int ptf(const char *fmt,...)
{
  va_list varlist;
  FILE *fp=fopen("/tmp/debug","a");
  va_start(varlist,fmt);
  vfprintf(fp,fmt,varlist);
  va_end(varlist);
  fclose(fp);
}</pre>
<p>This function isn&#8217;t rocket science, it just simply appends your string into a file. It is a simple time saver utility.</p>
<p>However, using it can be a problem. You can do something like this</p>
<pre>int x=1;
ptf("Error %s\n",x);</pre>
<p>And gcc will say &#8217;sure, no problem&#8217;.</p>
<p>But running the code will always crash. It tries to interpret the integer as a string.</p>
<p>This is the kind of thing that should be picked up on by the compiler. And in fact it can be, quite easily.</p>
<p>In your prototype for the function, you would have something like</p>
<pre>extern int ptf(const char *,...);</pre>
<p>This is pretty standard, and no surprises there. However, gcc has the capability to be given a hint as to how this function should be handled. You can instead prototype the function using</p>
<pre>extern int ptf(const char *,...) __attribute__ ((format (printf, 1, 2)));</pre>
<p>This tells gcc to treat the parameters 1 and 2 as the parameters to printf (which it knows how to check for errors). It will then check parameter 1 (the format string) against what is passed in starting at parameter 2 (the &#8230;). If an incorrect data type is used, this will now be detected and flagged up as a warning, in exactly the same way as an incorrect type used in a printf.</p>
<a class="a2a_dd addtoany_share_save" href="http://www.addtoany.com/share_save?linkurl=http%3A%2F%2Fblog.linuxgamepublishing.com%2F2010%2F01%2F20%2Fgetting-gcc-to-warn-you-when-you-mess-up-stdargs%2F&amp;linkname=Getting%20gcc%20to%20warn%20you%20when%20you%20mess%20up%20stdargs"><img src="http://blog.linuxgamepublishing.com/wp-content/plugins/add-to-any/share_save_171_16.png" width="171" height="16" alt="Share/Bookmark"/></a>]]></content:encoded>
			<wfw:commentRss>http://blog.linuxgamepublishing.com/2010/01/20/getting-gcc-to-warn-you-when-you-mess-up-stdargs/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>
