mikeash.com pyblog/friday-qa-2009-12-11-a-gcd-case-study-building-an-http-server.html commentshttp://www.mikeash.com/?page=pyblog/friday-qa-2009-12-11-a-gcd-case-study-building-an-http-server.html#commentsmikeash.com Recent CommentsThu, 28 Mar 2024 22:30:27 GMTPyRSS2Gen-1.0.0http://blogs.law.harvard.edu/tech/rssjtc - 2009-12-20 06:57:55http://www.mikeash.com/?page=pyblog/friday-qa-2009-12-11-a-gcd-case-study-building-an-http-server.html#commentsThank you for a very informative series on GCD. <br /> <br />John728574f1ff308c1b300d74519d5b800cSun, 20 Dec 2009 06:57:55 GMTJonathan Mitchell - 2009-12-13 20:49:09http://www.mikeash.com/?page=pyblog/friday-qa-2009-12-11-a-gcd-case-study-building-an-http-server.html#commentsI agree. The clang static analyser does catch it though, detecting the undefined value that results from int x = x + 1.fed8b46c0c6cc5fe1ae3f238c989923bSun, 13 Dec 2009 20:49:09 GMTmikeash - 2009-12-13 17:16:31http://www.mikeash.com/?page=pyblog/friday-qa-2009-12-11-a-gcd-case-study-building-an-http-server.html#commentsThat's not really what I want, though. That warns if you shadow a variable (and shadowing is sometimes useful) and doesn't warn if you misspelled something and thus <i>didn't</i> shadow a variable, but are still using a variable in its own initializer.1dba5cf807802d3dc839f4be9876105bSun, 13 Dec 2009 17:16:31 GMTJonathan Mitchell - 2009-12-13 11:44:37http://www.mikeash.com/?page=pyblog/friday-qa-2009-12-11-a-gcd-case-study-building-an-http-server.html#commentsvoid Foo(int x) <br />{ <br />&nbsp;&nbsp;&nbsp;&nbsp;if(x) <br />&nbsp;&nbsp;&nbsp;&nbsp;{ <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;int x = x + 1; // use the parameter to initialize the local <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;... <br />&nbsp;&nbsp;&nbsp;&nbsp;} <br />} <br /> <br />GCC_WARN_SHADOW, -Wshadow will give you a warning for this, if desirable.3e5f30e6b39a0c45a1db2fd46a3103e0Sun, 13 Dec 2009 11:44:37 GMTmikeash - 2009-12-12 17:42:00http://www.mikeash.com/?page=pyblog/friday-qa-2009-12-11-a-gcd-case-study-building-an-http-server.html#commentsNo, referring to a variable in its own initializer is perfectly legal, if frequently nonsensical. For example, I write code like this with disturbing regularity: <br /> <br /><code>void Foo(int x) <br />{ <br />&nbsp;&nbsp;&nbsp;&nbsp;if(x) <br />&nbsp;&nbsp;&nbsp;&nbsp;{ <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;int x = x + 1; // use the parameter to initialize the local <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;... <br />&nbsp;&nbsp;&nbsp;&nbsp;} <br />}</code> <br /> <br />It's legal, builds fine, and doesn't even produce any warnings. But the <code>x</code> that's used in the initializer is not the parameter, it is the local variable, and thus the variable is effectively uninitialized even though there is an initializer. <br /> <br />And in fact, gcc is perfectly happy to have a block refer to itself, too: <br /> <br /><code>void Bar(void) <br />{ <br />&nbsp;&nbsp;&nbsp;&nbsp;void (^block)(void) = ^{ block(); }; <br />}</code> <br /> <br />This compiles fine, no warnings. <br /> <br />It also doesn't work as expected, because it captures the value of <code>block</code> from <i>before</i> the initializer occurs, thus capturing a junk value. To work around that problem, you can qualify the variable with <code>__block</code>, which causes the block to track changes to the variable. Works fine, except that if you initialize the block on the same line rather than on a separate line, gcc throws these bizarre errors: <br /> <br /><code>test.m: In function ‘__Bar_block_invoke_1’: <br />test.m:16: error: request for member ‘__forwarding’ in something not a structure or union <br />test.m: In function ‘Bar’: <br />test.m:16: error: invalid type argument of ‘unary *’</code> <br /> <br />Clang handles it fine, and there's nothing wrong with the code. It's just a gcc bug.f4ed88d34cfa23784e5f031c69e258cfSat, 12 Dec 2009 17:42:00 GMTGwynne Raskind - 2009-12-12 10:26:50http://www.mikeash.com/?page=pyblog/friday-qa-2009-12-11-a-gcd-case-study-building-an-http-server.html#comments<code>__block dispatch_source_t source; // gcc won't compile if the next line is an initializer?!</code> <br /> <br />This is because you refer to source itself within the block, which you can't do in the variable's initializer because the initializer is evaluated before the declaration within the compiler; in essence, GCC doesn't know that the variable exists yet. Whether that behavior conforms to C90 or C99, I don't know.316d782e5a22412c8f0f03e98dcc6a0fSat, 12 Dec 2009 10:26:50 GMTJulesLt - 2009-12-12 08:48:14http://www.mikeash.com/?page=pyblog/friday-qa-2009-12-11-a-gcd-case-study-building-an-http-server.html#commentsDamn you - I'm intrigued to know how a fully developed GCD based HTTP server would benchmark against Apache now! <br /> <br />89695ece41f45d8259e3426caf7af428Sat, 12 Dec 2009 08:48:14 GMT