All of lore.kernel.org
 help / color / mirror / Atom feed
* [KJ] for loops and variable declarations...
@ 2007-06-05 11:22 Kevin Jackson
  2007-06-05 11:37 ` Pierre Habouzit
                   ` (3 more replies)
  0 siblings, 4 replies; 5+ messages in thread
From: Kevin Jackson @ 2007-06-05 11:22 UTC (permalink / raw)
  To: kernel-janitors

Hi all,

I'm just looking at kernel code for basically the first time - so
please be patient :)

I'm seeing this style of code:

uchar a
...
...
...
...
...
...(many lines later...)
for(a=2; a< VALUE; a++)


Why are loop variables declared so far away from where they are used?
Is this advantageous in some way?  It seems that the scope of the
variables is much larger than having them declared just as they are to
be used eg:

for(uchar a=2; a<VALUE; a++)

Sorry if this is a totally stupid question.

Kev
_______________________________________________
Kernel-janitors mailing list
Kernel-janitors@lists.linux-foundation.org
https://lists.linux-foundation.org/mailman/listinfo/kernel-janitors

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [KJ] for loops and variable declarations...
  2007-06-05 11:22 [KJ] for loops and variable declarations Kevin Jackson
@ 2007-06-05 11:37 ` Pierre Habouzit
  2007-06-05 11:59 ` John Anthony Kazos Jr.
                   ` (2 subsequent siblings)
  3 siblings, 0 replies; 5+ messages in thread
From: Pierre Habouzit @ 2007-06-05 11:37 UTC (permalink / raw)
  To: kernel-janitors


[-- Attachment #1.1: Type: text/plain, Size: 1116 bytes --]

On Tue, Jun 05, 2007 at 06:22:40PM +0700, Kevin Jackson wrote:
> Hi all,
> 
> I'm just looking at kernel code for basically the first time - so
> please be patient :)
> 
> I'm seeing this style of code:
> 
> uchar a
> ....
> ....
> ....
> ....
> ....
> ....(many lines later...)
> for(a=2; a< VALUE; a++)
> 
> 
> Why are loop variables declared so far away from where they are used?

  because C forbigs in-code variable declarations (unlike C++). GNU C
authorizes it though.

> Is this advantageous in some way?  It seems that the scope of the
> variables is much larger than having them declared just as they are to
> be used eg:
> 
> for(uchar a=2; a<VALUE; a++)

  This for is only allowed in C99, and is not well defined on some
compilers where a scope is not necessarily limited to the for loop as
common sense would suppose.

  So IMHO, kernel do not uses such kind of code (but I may be wrong).

-- 
·O·  Pierre Habouzit
··O                                                madcoder@debian.org
OOO                                                http://www.madism.org

[-- Attachment #1.2: Type: application/pgp-signature, Size: 189 bytes --]

[-- Attachment #2: Type: text/plain, Size: 187 bytes --]

_______________________________________________
Kernel-janitors mailing list
Kernel-janitors@lists.linux-foundation.org
https://lists.linux-foundation.org/mailman/listinfo/kernel-janitors

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [KJ] for loops and variable declarations...
  2007-06-05 11:22 [KJ] for loops and variable declarations Kevin Jackson
  2007-06-05 11:37 ` Pierre Habouzit
@ 2007-06-05 11:59 ` John Anthony Kazos Jr.
  2007-06-05 12:23 ` Matthew Wilcox
  2007-06-05 13:07 ` Robert P. J. Day
  3 siblings, 0 replies; 5+ messages in thread
From: John Anthony Kazos Jr. @ 2007-06-05 11:59 UTC (permalink / raw)
  To: kernel-janitors

> > I'm seeing this style of code:
> > 
> > uchar a
> > ....
> > ....
> > ....
> > ....
> > ....
> > ....(many lines later...)
> > for(a=2; a< VALUE; a++)
> > 
> > 
> > Why are loop variables declared so far away from where they are used?
> 
>   because C forbigs in-code variable declarations (unlike C++). GNU C
> authorizes it though.
> 
> > Is this advantageous in some way?  It seems that the scope of the
> > variables is much larger than having them declared just as they are to
> > be used eg:
> > 
> > for(uchar a=2; a<VALUE; a++)
> 
>   This for is only allowed in C99, and is not well defined on some
> compilers where a scope is not necessarily limited to the for loop as
> common sense would suppose.
> 
>   So IMHO, kernel do not uses such kind of code (but I may be wrong).

This is 2007. C99 is not "new" and anybody writing new code designed for 
an earlier version of C is certainly hopped up on crack at the very least.

A second valid point is that the Linux kernel requires not only the GNU 
CC, but a relatively recent GNU CC, and someone trying to use any other 
compiler with it is recommended to have fun banging their head against the 
wall. Especially since there are many (yes, MANY) places in the code that 
not only use C99+GNU-only constructs, but in fact use pure GNU-only 
extensions which have been discussed and approved several times on this 
list.

The time is long past to even joke about supporting any compiler other 
than GCC. If -c99 isn't in the CFLAGS by now, it needs to be.
_______________________________________________
Kernel-janitors mailing list
Kernel-janitors@lists.linux-foundation.org
https://lists.linux-foundation.org/mailman/listinfo/kernel-janitors

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [KJ] for loops and variable declarations...
  2007-06-05 11:22 [KJ] for loops and variable declarations Kevin Jackson
  2007-06-05 11:37 ` Pierre Habouzit
  2007-06-05 11:59 ` John Anthony Kazos Jr.
@ 2007-06-05 12:23 ` Matthew Wilcox
  2007-06-05 13:07 ` Robert P. J. Day
  3 siblings, 0 replies; 5+ messages in thread
From: Matthew Wilcox @ 2007-06-05 12:23 UTC (permalink / raw)
  To: kernel-janitors

On Tue, Jun 05, 2007 at 07:59:19AM -0400, John Anthony Kazos Jr. wrote:
> This is 2007. C99 is not "new" and anybody writing new code designed for 
> an earlier version of C is certainly hopped up on crack at the very least.

Yes, we're all hopped up on crack.

> A second valid point is that the Linux kernel requires not only the GNU 
> CC, but a relatively recent GNU CC, and someone trying to use any other 
> compiler with it is recommended to have fun banging their head against the 
> wall. Especially since there are many (yes, MANY) places in the code that 
> not only use C99+GNU-only constructs, but in fact use pure GNU-only 
> extensions which have been discussed and approved several times on this 
> list.
> 
> The time is long past to even joke about supporting any compiler other 
> than GCC. If -c99 isn't in the CFLAGS by now, it needs to be.

Actually, this is explicitly forbidden in the Linux kernel:

# warn about C99 declaration after statement
CFLAGS += $(call cc-option,-Wdeclaration-after-statement,)

Just because it's supported by the compiler, and permitted by a
standard, doesn't make it good CodingStyle.

Speaking of CodingStyle, the original poster should see
Documentation/CodingStyle chapter 6 for why this isn't a problem.
_______________________________________________
Kernel-janitors mailing list
Kernel-janitors@lists.linux-foundation.org
https://lists.linux-foundation.org/mailman/listinfo/kernel-janitors

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [KJ] for loops and variable declarations...
  2007-06-05 11:22 [KJ] for loops and variable declarations Kevin Jackson
                   ` (2 preceding siblings ...)
  2007-06-05 12:23 ` Matthew Wilcox
@ 2007-06-05 13:07 ` Robert P. J. Day
  3 siblings, 0 replies; 5+ messages in thread
From: Robert P. J. Day @ 2007-06-05 13:07 UTC (permalink / raw)
  To: kernel-janitors

On Tue, 5 Jun 2007, John Anthony Kazos Jr. wrote:

> The time is long past to even joke about supporting any compiler
> other than GCC.

technically, the intel compiler is also supported but since it
actually identifies itself as "gnu", it's really a difference that
makes no difference.

rday
-- 
====================================
Robert P. J. Day
Linux Consulting, Training and Annoying Kernel Pedantry
Waterloo, Ontario, CANADA

http://fsdev.net/wiki/index.php?title=Main_Page
====================================
_______________________________________________
Kernel-janitors mailing list
Kernel-janitors@lists.linux-foundation.org
https://lists.linux-foundation.org/mailman/listinfo/kernel-janitors

^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2007-06-05 13:07 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2007-06-05 11:22 [KJ] for loops and variable declarations Kevin Jackson
2007-06-05 11:37 ` Pierre Habouzit
2007-06-05 11:59 ` John Anthony Kazos Jr.
2007-06-05 12:23 ` Matthew Wilcox
2007-06-05 13:07 ` Robert P. J. Day

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.