Mailing List ArchiveSupport open source code!
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]Re: tlug: GCC compiler question
- To: tlug@example.com
- Subject: Re: tlug: GCC compiler question
- From: Peter Linsley <plinsley@example.com>
- Date: Mon, 19 Oct 1998 10:47:28 +0900
- Content-Type: text/plain; charset=US-ASCII
- In-reply-to: Your message of "Sat, 17 Oct 1998 22:52:48 JST." <3628A130.90268CF3@example.com>
- Reply-To: tlug@example.com
- Sender: owner-tlug@example.com
Hello, This is a common `hack' which is only any use when considering macro expansion, such as in this case. It stems from the fact that the programmer requires that the macro is in it's own block; this could be inherent from the syntax of the macro or just that they want to deal with block scope. Let's look at your discovery: #define spin_lock_init(x) do { (x)->lock = 0; } while(0) If it were used in the following way: if (true) spin_lock_init (x); The semi-colon following "spin_lock_init (x);" will be consumed as part of the macro expansion as: if (true) do { (x)->lock = 0; } while(0); If we were to declare the macro as follows: #define spin_lock_init(x) { (x)->lock = 0; } Then this would expand to if (true) { (x)->lock = 0; } ...and not be syntactically correct, this will cause a compile error as no semi-colon is present. This is possibly a bad example to comprehend as there is only one expression, I presume the programmer was just insistent on good programming habits. A more likely example would be the following macro: #define m(A) if(A){exp1; exp2;} You can see the problems with the semi-colon when this is expanded, we need to wrap it in a "do{ ... } while(0)". Regards, Peter Your message dated: Sat, 17 Oct 1998 22:52:48 JST > I've been looking through some of the kernel source, and I keep seeing > things like the following:- > > #define spin_lock_init(x) do { (x)->lock = 0; } while(0) > > What's the point of the do {} while(0) bit? Why not just > > #define spin_lock_init(x) { (x)->lock = 0; } > > or even > > #define spin_lock_init(x) (x)->lock = 0; > > The only point I can see is causing compile-time errors for misuse, such > as > > z += spin_lock_init(x); > > which would compile under the 3rd alternative above. And who would > really write such code anyway? And why not the 2nd alternative? > > Neil. > --------------------------------------------------------------- > Next Nomikai: 20 November, 19:30 Tengu TokyoEkiMae 03-3275-3691 > Next Meeting: 12 December, 12:30 Tokyo Station Yaesu central gate > --------------------------------------------------------------- > Sponsor: PHT, makers of TurboLinux http://www.pht.co.jp > --------------------------------------------------------------- Next Nomikai: 20 November, 19:30 Tengu TokyoEkiMae 03-3275-3691 Next Meeting: 12 December, 12:30 Tokyo Station Yaesu central gate --------------------------------------------------------------- Sponsor: PHT, makers of TurboLinux http://www.pht.co.jp
- Follow-Ups:
- Re: tlug: GCC compiler question
- From: Darren Cook <darren@example.com>
- Re: tlug: GCC compiler question
- From: "Stephen J. Turnbull" <turnbull@example.com>
- References:
- tlug: GCC compiler question
- From: Neil Booth <NeilB@example.com>
Home | Main Index | Thread Index
- Prev by Date: Re: tlug: Re: Intranet
- Next by Date: Re: tlug: GCC compiler question
- Prev by thread: tlug: GCC compiler question
- Next by thread: Re: tlug: GCC compiler question
- Index(es):
Home Page Mailing List Linux and Japan TLUG Members Links