LINUX.IE, website of the Irish Linux Users' Group
Tux rules!

   
Home
New Users
Articles
Download
Projects
Community
Vendors

  Print Version
Email to...
 
Archives:


planetILUG

Recent News

News Archive


Join the
ILUG
on FaceBook


Join the
ILUG
on LinkedIn


Join the
ILUG SETI
Group



















 
 :: Mailing Lists

[ILUG] C puzzle

[ILUG] C puzzle

Paul Jakma paul at clubi.ie
Tue Jul 6 12:00:53 IST 2004


On Tue, 6 Jul 2004, Philip Reynolds wrote:

> You need to use a temporary variable, to allow the use of free() or 
> removal of an element in the list.

Indeed. the problem is trying to stuff it into the for loop so that 
macro will still work :)

> #define LIST_LOOP(L, V, N, temp) \
> 	for ((N) = (L)->head; \
> 		(N) && ((temp) = (N)->next); \
> 		(N) = (temp))
>
> Something like the above.

Nah, that breaks the macro.

However, I've had very useful suggestions from messrs Lyda and 
Arlied, which I've to go test out. Something like:

#define LIST_LOOP (list, val, this, next) \
  for ( (this) = (list)->head, (next) = (this)->next; \
        (this) != NULL; \
        (next) = (this)->next, (this) = (next)) \
    if ( ((val) = (this)->data) != NULL)

Should do the trick (as discerned from the linux kernel list macros, 
which dave pointed me at, so damn obvious once you see it of course). 
Lyda actually managed to shove conditionals into the for loop, but in 
his defence, because I had insisted that i needed assignment of next 
to be conditional, so it's not his fault ;). I havnt tested whether 
it's portable yet though (it has to work with a certain compiler 
which is a bit stricter than gcc).

> --
> Philip Reynolds                      | RFC Networks Ltd.

regards,
-- 
Paul Jakma	paul at clubi.ie	paul at jakma.org	Key ID: 64A2FF6A
 	warning: do not ever send email to spam at dishone.st
Fortune:
panic: kernel segmentation violation. core dumped		(only kidding)



More information about the ILUG mailing list
Read this without the formatting.
                                                                                                    

 

Hosted by HEAnet


Maintained by the ILUG website team. The aim of Linux.ie is to support and help commercial and private users of Linux in Ireland. You can display ILUG news in your own webpages, read backend information to find out how. Networking services kindly provided by HEAnet, server kindly donated by Dell. Linux is a trademark of Linus Torvalds, used with permission. No penguins were harmed in the production or maintenance of this highly praised website. Looking for the Indian Linux Users' Group? Try here. If you've read all this and aren't a lawyer: you should be!
RSS Version
Powered by Dell