From owner-freebsd-questions Fri Feb 24 10:52:16 1995 Return-Path: questions-owner Received: (from majordom@localhost) by freefall.cdrom.com (8.6.9/8.6.6) id KAA22128 for questions-outgoing; Fri, 24 Feb 1995 10:52:16 -0800 Received: from cs.weber.edu (cs.weber.edu [137.190.16.16]) by freefall.cdrom.com (8.6.9/8.6.6) with SMTP id KAA22122 for ; Fri, 24 Feb 1995 10:52:14 -0800 Received: by cs.weber.edu (4.1/SMI-4.1.1) id AA27729; Fri, 24 Feb 95 11:33:05 MST From: terry@cs.weber.edu (Terry Lambert) Message-Id: <9502241833.AA27729@cs.weber.edu> Subject: Re: stupid C question.. To: ugen@netvision.net.il (Ugen J.S.Antsilevich) Date: Fri, 24 Feb 95 11:33:05 MST Cc: freebsd-questions@freefall.cdrom.com In-Reply-To: from "Ugen J.S.Antsilevich" at Feb 24, 95 12:13:03 pm X-Mailer: ELM [version 2.4dev PL52] Sender: questions-owner@FreeBSD.org Precedence: bulk > Hmm..seems that in part of my code i somehow used labels in a > strange way. > I used same label names in two different functions in same source > and everything seems to work ok. Does that means label names can be > local to the function? > Is this normal property of any C compiler or gcc only? Actually > i nether thought of it - just used labels somehow but anyway i would > like to have an exact answer. Variables and labels are subject to what are called scoping rules. Labels are scoped per function. You can only use a label once per function. A special exception is the case label; case labels are scoped per switch statement; you can use a case label only once in a single switch statement. Variables are dynamically scoped. An auto variable can be declared follwoing any left brace ("{") prior to statements (code). For instance: main() { int i = 5; printf( "i == %d\n", i); { int i = 7; printf( "i == %d\n", i); } printf( "i == %d\n", i); } It should be noted that dynamic scoping of variables is not handled by some older PC compilers. Actually, early versions of Microsoft C had problems with trashing the stack if you used them. Sean Fagan worked on the Microsoft C 5.x code under SCO. I remember I first met him (back in 1989) at SCO Forum when he was working on a problem of when a quad should be generated for a weird case. 8-). Many older compilers also don't like the left brace not following a function declaration or some other conditonal statement. My Altos 886 would puke on the "{" following the printf above. It would also puke on NULL statements following if/for/else/ etc. ...so you couldn't use just ';' in place of continue. I think this was to protect the programmer from himself for things like: for( i = 0; i < 10; i++); printf( "i is %d\n", i); which would print out a single "10". 8-). Many compilers (like early versions of Lattice C on the PC and Amiga, now sold by SAS but presumably fixed) would blow up when you used too many 'register' keywords. Typically, one should avoid dynamically scoping variables becauses it takes more instructions each time you adjust the stack pointer or put it back. Personally, I *like* reusing labels; it aids in a uniform code style for single entry/single exit programming, like: int foo() { int fd; int err = ER_FAILURE; /* default*/ ... stuff ... if( some_condition) { err = ER_SUCCESS; goto done; } if( ( fd = open( "myfile, O_RDWR)) == -1) goto error; if( try_something() == -1) goto error_1; ... more stuff ... err = ER_SUCCESS; /* * Fall through to recover implied state */ error_1: /* error with file still open*/ close( fd); error: /* plain error*/ done: /* success*/ return( err); } Single entry/single exit is especially important if you do things like holding locks in the code. It also helps you isolate critical code sections for if/when you want to do multithreading (even in the kernel). Well. This has turned into a pretty long diatribe. 8-). Hope it helps you out. Terry Lambert terry@cs.weber.edu --- Any opinions in this posting are my own and not those of my present or previous employers.