<?xml version="1.0" encoding="utf-8"?>
<rss version="2.0"><channel><title>mikeash.com pyblog/friday-qa-2012-08-24-things-you-never-wanted-to-know-about-c.html comments</title><link>http://www.mikeash.com/?page=pyblog/friday-qa-2012-08-24-things-you-never-wanted-to-know-about-c.html#comments</link><description>mikeash.com Recent Comments</description><lastBuildDate>Mon, 17 Aug 2026 10:48:36 GMT</lastBuildDate><generator>PyRSS2Gen-1.0.0</generator><docs>http://blogs.law.harvard.edu/tech/rss</docs><item><title>Jeffrey Walton - 2014-07-26 02:15:33</title><link>http://www.mikeash.com/?page=pyblog/friday-qa-2012-08-24-things-you-never-wanted-to-know-about-c.html#comments</link><description>The nested functions are toxic in the case of no blocks. They are mutually exclusive with no-exec stacks (-Wl,-z,noexecstack).
&lt;br /&gt;
&lt;br /&gt;You can't use no-exec stacks because GCC puts a trampoline on the stack.
&lt;br /&gt;
&lt;br /&gt;The loss of no-exec stacks should be a big problem if an organization (or developer) have an SDLC in place.</description><guid isPermaLink="true">0c83a7f5a22b2169a2780490a8c695f7</guid><pubDate>Sat, 26 Jul 2014 02:15:33 GMT</pubDate></item><item><title>Gwynne Raskind - 2012-08-30 16:28:26</title><link>http://www.mikeash.com/?page=pyblog/friday-qa-2012-08-24-things-you-never-wanted-to-know-about-c.html#comments</link><description>&lt;b&gt;Peter:&lt;/b&gt; Nice catch! Ironically enough, in real code I always use &lt;code&gt;calloc&lt;/code&gt; instead of &lt;code&gt;malloc&lt;/code&gt; even in cases where I know for a fact it doesn't matter, just to avoid ever writing myself into corners exactly like that one; I only used &lt;code&gt;malloc&lt;/code&gt; for this contrived example because I have a habit of falling back to "beginner" habits for example code.
&lt;br /&gt;
&lt;br /&gt;Joke's on me, though; a beginner would almost certainly be using &lt;code&gt;fopen&lt;/code&gt; and &lt;code&gt;fread&lt;/code&gt; instead of &lt;code&gt;open&lt;/code&gt; and &lt;code&gt;read&lt;/code&gt;. I just have an aversion to stdio :).</description><guid isPermaLink="true">2586937f297247019e33e43635a4df10</guid><pubDate>Thu, 30 Aug 2012 16:28:26 GMT</pubDate></item><item><title>Peter N Lewis - 2012-08-30 12:13:29</title><link>http://www.mikeash.com/?page=pyblog/friday-qa-2012-08-24-things-you-never-wanted-to-know-about-c.html#comments</link><description>Not wanting to nitpick, but shouldn't that be calloc instead of malloc for all exept the first example?  Otherwise the returned block for brillig will contain garbage values which will potentially crash in the exception code for free(brillig-&amp;gt;gamma).
&lt;br /&gt;
&lt;br /&gt;Interestingly, because of this, despite the verbosity and repetitiveness of the first example, it's the only one that actually safely handles the errors!</description><guid isPermaLink="true">3064102acf54ae380ea8fb527b1b6349</guid><pubDate>Thu, 30 Aug 2012 12:13:29 GMT</pubDate></item><item><title>rog - 2012-08-30 10:25:22</title><link>http://www.mikeash.com/?page=pyblog/friday-qa-2012-08-24-things-you-never-wanted-to-know-about-c.html#comments</link><description>I quite liked the way that the Plan 9 kernel solved this problem with setjmp/longjmp.
&lt;br /&gt;
&lt;br /&gt;For example, go to this page, search for syspipe and check out how the error handling logic works.
&lt;br /&gt;&lt;a href="http://plan9.bell-labs.com/sources/plan9/sys/src/9/port/sysfile.c"&gt;http://plan9.bell-labs.com/sources/plan9/sys/src/9/port/sysfile.c&lt;/a&gt;
&lt;br /&gt;
&lt;br /&gt;It's error-prone (you can easily forget a poperror) but it's at least clean and quite easy to check mechanically.
&lt;br /&gt;
&lt;br /&gt;Oh, it also has the problem that local variables aren't guaranteed to stay around after a longjmp, so it's probably only a historical curiosity.
&lt;br /&gt;
&lt;br /&gt;I miss Go's defer statement whenever I program in C these days.
&lt;br /&gt;</description><guid isPermaLink="true">992cca37150a5ac8c33ce6cece46a945</guid><pubDate>Thu, 30 Aug 2012 10:25:22 GMT</pubDate></item><item><title>ChadF - 2012-08-27 21:18:38</title><link>http://www.mikeash.com/?page=pyblog/friday-qa-2012-08-24-things-you-never-wanted-to-know-about-c.html#comments</link><description>&lt;div class="blogcommentquote"&gt;&lt;div class="blogcommentquoteinner"&gt;Exceptions can be used well, but it's tough to do so, and way too much code uses exceptions for expected errors rather than exceptional conditions.&lt;/div&gt;&lt;/div&gt;
&lt;br /&gt;Kinda reminds me of a joke I had with some co-workers a few years back about purposely creating overly complex code in java.
&lt;br /&gt;
&lt;br /&gt;Something with the gist of:
&lt;br /&gt;&lt;code&gt;
&lt;br /&gt;void foo()
&lt;br /&gt;{
&lt;br /&gt;&amp;nbsp;&amp;nbsp;// ... some code ...
&lt;br /&gt;
&lt;br /&gt;&amp;nbsp;&amp;nbsp;throw new EverythingIsFineException();
&lt;br /&gt;}
&lt;br /&gt;
&lt;br /&gt;try
&lt;br /&gt;{
&lt;br /&gt;&amp;nbsp;&amp;nbsp;foo();
&lt;br /&gt;
&lt;br /&gt;&amp;nbsp;&amp;nbsp;// ... We reached an error ...
&lt;br /&gt;}
&lt;br /&gt;catch(EverythingIsFineException ex)
&lt;br /&gt;{
&lt;br /&gt;}
&lt;br /&gt;&lt;/code&gt;
&lt;br /&gt;
&lt;br /&gt;I think the discussion was something relating to a Rube Goldberg like coding contest.
&lt;br /&gt;</description><guid isPermaLink="true">da25e8dfbde23e382d1dbc631ac2d15c</guid><pubDate>Mon, 27 Aug 2012 21:18:38 GMT</pubDate></item><item><title>mikeash - 2012-08-27 20:48:05</title><link>http://www.mikeash.com/?page=pyblog/friday-qa-2012-08-24-things-you-never-wanted-to-know-about-c.html#comments</link><description>&lt;b&gt;Michael Kingsford Gray&lt;/b&gt; and &lt;b&gt;Gary Hetzel:&lt;/b&gt; Brainlessly dogmatic positions are not welcome here, sorry. Both the idea that C should never be used for anything, and the idea that C is perfectly fine and the only problem is bad programmers, are stupid. If you guys want to hold an argument over which ridiculous position is better, feel free, but please hold it somewhere else.</description><guid isPermaLink="true">e8b654b40203bcac2c74132feec9388b</guid><pubDate>Mon, 27 Aug 2012 20:48:05 GMT</pubDate></item><item><title>Gary Hetzel - 2012-08-27 18:44:51</title><link>http://www.mikeash.com/?page=pyblog/friday-qa-2012-08-24-things-you-never-wanted-to-know-about-c.html#comments</link><description>Michael Kingsford Gray: "C" has not ever cost anything to anyone. Buffer overruns are the product either inattentive developers or those who are not as adept at the language as they would like to appear. In the same vein, if you don't know how to use a jackhammer did so anyhow, and created a mess, would you blame the tool?
&lt;br /&gt;</description><guid isPermaLink="true">cc5e9115e8ba206da5c5e6e56d2f17ab</guid><pubDate>Mon, 27 Aug 2012 18:44:51 GMT</pubDate></item><item><title>CUP - 2012-08-27 17:11:21</title><link>http://www.mikeash.com/?page=pyblog/friday-qa-2012-08-24-things-you-never-wanted-to-know-about-c.html#comments</link><description>If you look at the Microsoft autogenerated code, it does something very similar in its TRY - CATCH macros.
&lt;br /&gt;
&lt;br /&gt;In the early 90s I used to use longjmp for the throw and setjmp for the catch point.</description><guid isPermaLink="true">56c7eed83c5687ee107e0df9d2c01646</guid><pubDate>Mon, 27 Aug 2012 17:11:21 GMT</pubDate></item><item><title>Michael Kingsford Gray - 2012-08-27 10:37:18</title><link>http://www.mikeash.com/?page=pyblog/friday-qa-2012-08-24-things-you-never-wanted-to-know-about-c.html#comments</link><description>Anyone who programs in "C" for anything other than trivial applications, should be shot at dawn.
&lt;br /&gt;"C" has cost the planet an incalculable amount in just the HUGE cost of undetected buffer over-runs to render using it a crime against humanity.</description><guid isPermaLink="true">b1daf5737c3f7fb1e8012572c05607c8</guid><pubDate>Mon, 27 Aug 2012 10:37:18 GMT</pubDate></item><item><title>Gwynne Raskind - 2012-08-24 23:21:03</title><link>http://www.mikeash.com/?page=pyblog/friday-qa-2012-08-24-things-you-never-wanted-to-know-about-c.html#comments</link><description>&lt;b&gt;Justin:&lt;/b&gt; Yeah, you were the whole inspiration for all this; I just didn't limit myself to standards-compliance :). (Not the best choice on my part, but GCC and Clang do seem to be fairly ubiquitous these days, in my experience.)</description><guid isPermaLink="true">175485f37f1a39800b4514d2e4276325</guid><pubDate>Fri, 24 Aug 2012 23:21:03 GMT</pubDate></item><item><title>Justin Spahr-Summers - 2012-08-24 18:40:26</title><link>http://www.mikeash.com/?page=pyblog/friday-qa-2012-08-24-things-you-never-wanted-to-know-about-c.html#comments</link><description>Ha, I missed the closing paragraph of the article. :)</description><guid isPermaLink="true">fad7d7c7db2c4069499906f0e66e7342</guid><pubDate>Fri, 24 Aug 2012 18:40:26 GMT</pubDate></item><item><title>Justin Spahr-Summers - 2012-08-24 18:37:25</title><link>http://www.mikeash.com/?page=pyblog/friday-qa-2012-08-24-things-you-never-wanted-to-know-about-c.html#comments</link><description>I did something similar, using only standard C99:
&lt;br /&gt;&lt;a href="https://github.com/jspahrsummers/libextc/blob/master/include/exception.h"&gt;https://github.com/jspahrsummers/libextc/blob/master/include/exception.h&lt;/a&gt;
&lt;br /&gt;
&lt;br /&gt;The macros are even more abhorrent, but enable usage like:
&lt;br /&gt;
&lt;br /&gt;&lt;code&gt;
&lt;br /&gt;try {
&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;try {
&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;raise(Exception, NULL);
&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;}
&lt;br /&gt;} catch (Exception, ex) {
&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;caught = 1;
&lt;br /&gt;} finally {
&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;assert(caught == 1);
&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;executed_finally = true;
&lt;br /&gt;}
&lt;br /&gt;&lt;/code&gt;</description><guid isPermaLink="true">9616600627b7df48ff05fb67648ae176</guid><pubDate>Fri, 24 Aug 2012 18:37:25 GMT</pubDate></item><item><title>Kent Sorensen - 2012-08-24 17:25:08</title><link>http://www.mikeash.com/?page=pyblog/friday-qa-2012-08-24-things-you-never-wanted-to-know-about-c.html#comments</link><description>Interesting article. I'd like to point out AssertMacros.h  containing the require_action etc set of macros. To my eyes, you have pretty much ended up with the same solutions to the same problems that those macros solve. 
&lt;br /&gt;
&lt;br /&gt;These macros are ancient. I first came across them in the 1992 Develop magazine mentioned in the file header. Unfortunately I don't seem to have that particular issue anymore. I do have a stack of Develop magazines in the basement ...Purely for "historical" reasons, of course... </description><guid isPermaLink="true">04c81708aac9123b65a2875911aeba1f</guid><pubDate>Fri, 24 Aug 2012 17:25:08 GMT</pubDate></item><item><title>Grumpy Jameson - 2012-08-24 16:07:33</title><link>http://www.mikeash.com/?page=pyblog/friday-qa-2012-08-24-things-you-never-wanted-to-know-about-c.html#comments</link><description>&lt;div class="blogcommentquote"&gt;&lt;div class="blogcommentquoteinner"&gt;. In essence, @try in Objective-C and try in C++ are now the same thing, though they're still not interchangeable due to the separation of class heirarchies.&lt;/div&gt;&lt;/div&gt;
&lt;br /&gt;
&lt;br /&gt;This is only true for Mac 64-bit and iOS. For compatibility, these language constructs still use the old mechanism for 32-bit Mac apps.</description><guid isPermaLink="true">55e662162b16cae3cf6a7fabdc910bac</guid><pubDate>Fri, 24 Aug 2012 16:07:33 GMT</pubDate></item><item><title>Kapil Sachdeva - 2012-08-24 16:03:20</title><link>http://www.mikeash.com/?page=pyblog/friday-qa-2012-08-24-things-you-never-wanted-to-know-about-c.html#comments</link><description>Thanks Gwynne and Mike for the clarifications.
&lt;br /&gt;
&lt;br /&gt;&amp;lt;mike&amp;gt;
&lt;br /&gt;Within Cocoa, it's very difficult to use exceptions correctly (they don't cooperate with ARC by default, throwing through framework code is an error, the built-in classes almost always use errors rather than exceptions, etc.) so I don't recommend using them in Cocoa, regardless of their merits in other languages.
&lt;br /&gt;&amp;lt;/mike&amp;gt;
&lt;br /&gt;
&lt;br /&gt;So this is the actual reason for using NSError vs exception and is because of platform limitations from Apple.
&lt;br /&gt;
&lt;br /&gt;My understanding is that if everything was ideal (from platform support point of view) then the usage of exceptions i.e. try-catch-finally constructs vs error codes/NSError is the way to go to have more cleaner &amp;amp; robust code.
&lt;br /&gt;
&lt;br /&gt;I hope that Apple and bloggers would explain it the way you mentioned i.e "in the context of cocoa" it is recommended to use NSError because of "platform limitations". The discussion on the web makes it sound like try-catch-finally is bad vs error codes in general.
&lt;br /&gt;
&lt;br /&gt;I hope we are on the same page else please do guide. 
&lt;br /&gt;
&lt;br /&gt;Regards &amp;amp; thanks
&lt;br /&gt;Kapil
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;</description><guid isPermaLink="true">45f3efdb1dd0c80b601172e5c2f925ff</guid><pubDate>Fri, 24 Aug 2012 16:03:20 GMT</pubDate></item><item><title>mikeash - 2012-08-24 15:47:21</title><link>http://www.mikeash.com/?page=pyblog/friday-qa-2012-08-24-things-you-never-wanted-to-know-about-c.html#comments</link><description>Note that this is another guest article, written by Gwynne, not me.
&lt;br /&gt;
&lt;br /&gt;As for errors versus exceptions, I generally agree with Gwynne here. Exceptions &lt;i&gt;can&lt;/i&gt; be used well, but it's tough to do so, and way too much code uses exceptions for expected errors rather than exceptional conditions. Within Cocoa, it's very difficult to use exceptions correctly (they don't cooperate with ARC by default, throwing through framework code is an error, the built-in classes almost always use errors rather than exceptions, etc.) so I don't recommend using them in Cocoa, regardless of their merits in other languages.</description><guid isPermaLink="true">730abe6fcc10c3e41eedb7fd636ffcbe</guid><pubDate>Fri, 24 Aug 2012 15:47:21 GMT</pubDate></item><item><title>Gwynne Raskind - 2012-08-24 15:28:51</title><link>http://www.mikeash.com/?page=pyblog/friday-qa-2012-08-24-things-you-never-wanted-to-know-about-c.html#comments</link><description>&lt;b&gt;Kapil:&lt;/b&gt; My stand is the same as Apple's: Do not use exceptions in Objective-C to manage your control flow. The examples I wrote in this article were purely illustrative, not indicative of a way of doing anything.
&lt;br /&gt;
&lt;br /&gt;In the general sense, this whole discussion was completely moot from an Objective-C point of view; the &lt;code&gt;NSError&lt;/code&gt;-as-out-parameter paradigm is well-established in modern Cocoa, and it works. This article presents an approach geared towards pure C without clearly advocating any particular methodology at all (which is one of its problems).</description><guid isPermaLink="true">a468eb2b2c67deb8d6a044717471e849</guid><pubDate>Fri, 24 Aug 2012 15:28:51 GMT</pubDate></item><item><title>Kapil Sachdeva - 2012-08-24 14:56:03</title><link>http://www.mikeash.com/?page=pyblog/friday-qa-2012-08-24-things-you-never-wanted-to-know-about-c.html#comments</link><description>Mike, 
&lt;br /&gt;I am bit confused by your stance.
&lt;br /&gt;
&lt;br /&gt;&amp;lt;block&amp;gt;
&lt;br /&gt;Fortunately, Apple realized how ridiculous this tends to look in Cocoa and deprecated exceptions for error handling, instead opting for the NSError model. The use of exception handling in Objective-C is now reserved (at least in theory) for truly "exceptional" conditions, particularly programmer error.
&lt;br /&gt;&amp;lt;/block&amp;gt;
&lt;br /&gt;
&lt;br /&gt;I have seen this earlier on web and not necessarily in agreement with Apple on Error vs exception using language constructs like (try,catch, finally) for reasons explained by you in this article i.e. usage of error codes, goto statement, unreadable code etc when not using exception.
&lt;br /&gt;
&lt;br /&gt;What is you stand - should we use exceptions in objective C or not ?
&lt;br /&gt;
&lt;br /&gt;I am not clear as in the same article you are liking Apple's recommendation on NSError instead of exception and you are also exampling how to use try-catch.
&lt;br /&gt;
&lt;br /&gt;Most likely I am missing something here.
&lt;br /&gt;
&lt;br /&gt;Would appreciate if you could guide.
&lt;br /&gt;
&lt;br /&gt;Regards &amp;amp; thanks
&lt;br /&gt;Kapil
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;</description><guid isPermaLink="true">3226b39f4cdb6a02595e52b514276b3c</guid><pubDate>Fri, 24 Aug 2012 14:56:03 GMT</pubDate></item><item><title>Iain Delaney - 2012-08-24 14:53:26</title><link>http://www.mikeash.com/?page=pyblog/friday-qa-2012-08-24-things-you-never-wanted-to-know-about-c.html#comments</link><description>Another possibility is what I call the 'Unix Convention'. Your function returns an integer, with zero for success and errors mapped to different negative numbers. Macros for the error codes make things a little easier to read.
&lt;br /&gt;
&lt;br /&gt;When you have a function that returns or creates something, you pass in a pointer to be populated as the last parameter of the function.
&lt;br /&gt;
&lt;br /&gt;Then you can the function, check the return value, and if it's zero, then your pointer is good.
&lt;br /&gt;
&lt;br /&gt;This is how I did some things with a pre-ANSI C compiler. I think it gets you most of the way there.
&lt;br /&gt;
&lt;br /&gt;</description><guid isPermaLink="true">b14690c20a6c2f878a8187bf00d798c5</guid><pubDate>Fri, 24 Aug 2012 14:53:26 GMT</pubDate></item><item><title>Kapil Sachdeva - 2012-08-24 14:38:42</title><link>http://www.mikeash.com/?page=pyblog/friday-qa-2012-08-24-things-you-never-wanted-to-know-about-c.html#comments</link><description>There is a nice wrapping of setjmp/longjmp for C that gives you a feel of using Try-Catch as you would do in C++, C# or Java.
&lt;br /&gt;
&lt;br /&gt;&lt;a href="http://www.nicemice.net/cexcept/"&gt;http://www.nicemice.net/cexcept/&lt;/a&gt;</description><guid isPermaLink="true">3a011b31bd9cc1e4718a13360e1de0bd</guid><pubDate>Fri, 24 Aug 2012 14:38:42 GMT</pubDate></item><item><title>Goffredo Marocchi - 2012-08-24 14:23:13</title><link>http://www.mikeash.com/?page=pyblog/friday-qa-2012-08-24-things-you-never-wanted-to-know-about-c.html#comments</link><description>Congratulations Mike, I had not see C doing things like that. It sounds almost immoral :P.
&lt;br /&gt;
&lt;br /&gt;Jokes aside, great article as usual :).</description><guid isPermaLink="true">e2a4153184c28be161b2ef528c615971</guid><pubDate>Fri, 24 Aug 2012 14:23:13 GMT</pubDate></item></channel></rss>
