473,662 Members | 2,752 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Question about variable scope conflict

I accidentally did this the other day (it was a lot less obvious in the
much longer actual program, hundreds of lines are omitted):

----------------------------------------------------------
#include <stdio.h>
#include <stdlib.h>

int gbl_var=0; /* one of several globals */

void compare(void){
(void) fprintf(stdout, "gbl_var is %d\n",gbl_var);
}

int main(void){
int gbl_var; /* <----- OOPS, left over from a previous version */
gbl_var=1;
/* much code, including a qsort where the compare function
needed the value of gbl_var, but read 0 instead of 1.
Here just call compare directly */
compare();
exit(EXIT_SUCCE SS);
}
-----------------------------------------------------------
% gcc -Wall -std=c99 -pedantic -o foo foo.c
% #no warnings or errors are reported
% ./foo
gbl_var is 0
------------------------------------------------------------

Once I found the bug I was a bit surprised that the compiler had not
issued a warning. What does the standard say about using the same
variable name in two overlapping scopes like this? Apparently it allows
it, I guess to avoid accidental name conflicts, for instance between a
global in a library and a similarly named variable in a function.
Still, it would have been nice if the compiler could have at least
optionally warned about this. Unfortunately so far none of the -W
switches I've tried have flagged this problem, including
-Wredundant-decls. Is there a "-Wvar-nested-scope" that I missed.

Regards,

David Mathog
Nov 30 '07 #1
7 1899
David Mathog wrote:
I accidentally did this the other day (it was a lot less obvious in
the much longer actual program, hundreds of lines are omitted):

----------------------------------------------------------
#include <stdio.h>
#include <stdlib.h>

int gbl_var=0; /* one of several globals */

void compare(void){
(void) fprintf(stdout, "gbl_var is %d\n",gbl_var);
}

int main(void){
int gbl_var; /* <----- OOPS, left over from a previous version */
gbl_var=1;
/* much code, including a qsort where the compare function
needed the value of gbl_var, but read 0 instead of 1.
Here just call compare directly */
compare();
exit(EXIT_SUCCE SS);
}
-----------------------------------------------------------
% gcc -Wall -std=c99 -pedantic -o foo foo.c
% #no warnings or errors are reported
% ./foo
gbl_var is 0
------------------------------------------------------------

Once I found the bug I was a bit surprised that the compiler had not
issued a warning. What does the standard say about using the same
variable name in two overlapping scopes like this? Apparently it
allows it, I guess to avoid accidental name conflicts, for instance
between a global in a library and a similarly named variable in a
function. Still, it would have been nice if the compiler could have at
least
optionally warned about this.
Use the -Wshadow switch for gcc.
Nov 30 '07 #2
David Mathog wrote:
santosh wrote:
>Use the -Wshadow switch for gcc.

I never would have thought to call it that.

Any idea where the term "shadow variable" in this context originated?
It pretty common to informally say that an object in a particular
scope "shadows" an identically named object in an outer scope. So I
guess the name for that gcc diagnostics switch was taken from this.

<snip>

Nov 30 '07 #3
David Mathog said:
I accidentally did this the other day (it was a lot less obvious in the
much longer actual program, hundreds of lines are omitted):
I'll trim a little more out for you:
int gbl_var=0; /* one of several globals */

void compare(void){
(void) fprintf(stdout, "gbl_var is %d\n",gbl_var);
}

int main(void){
int gbl_var; /* <----- OOPS, left over from a previous version */
gbl_var=1;
<snip>
Once I found the bug I was a bit surprised that the compiler had not
issued a warning. What does the standard say about using the same
variable name in two overlapping scopes like this?
It's perfectly legal, and the effect you noticed is conforming. The inner
scope's declaration takes precedence (if that's the right word!).
Apparently it allows
it, I guess to avoid accidental name conflicts, for instance between a
global in a library and a similarly named variable in a function.
Presumably, yes - but it's Yet Another Good Reason to minimise or even
eliminate your use of file scope objects.
Still, it would have been nice if the compiler could have at least
optionally warned about this.
You seem to be using gcc, so try the -Wshadow switch; here's what it does
with your code:

me@heregcc -Wshadow -o foo foo.c
foo.c: In function `main':
foo.c:11: warning: declaration of `gbl_var' shadows global declaration

--
Richard Heathfield <http://www.cpax.org.uk >
Email: -http://www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
Nov 30 '07 #4
In article <KJ************ *********@bt.co m>,
Richard Heathfield <rj*@see.sig.in validwrote:
>David Mathog said:
>Apparently it allows
it, I guess to avoid accidental name conflicts, for instance between a
global in a library and a similarly named variable in a function.

Presumably, yes - but it's Yet Another Good Reason to minimise or even
eliminate your use of file scope objects.
I've found that putting all of the file scope objects in one of two
structs (one for external linkage and one for internal) both makes it
harder to run into this problem and makes keeping track of what's where
a lot easier.
(So instead of having num_fds at file scope and curr_fd at function
scope, I have ioloop_data.num _fds at file scope and when I see curr_fd
in the code I know it's _not_ at file scope.)

This also has the side effect of making it a lot easier to eliminate
file-scope variables entirely should it be necessary to do so (they're
already wrapped up nicely in a struct, and all that's required is
adding code to create and destroy copies of that struct and changing
the existing interfaces to include a pointer to that struct everywhere
- not always trivial, but not nearly as hard as it could be). I've had
to do that rather less often than I've written code that used a single
struct to hold its file-scope objects, but even looking at just that
side effect the cost-benefit ratio probably comes out favoring the
benefit side.
dave

Nov 30 '07 #5
David Mathog wrote:
santosh wrote:
>Use the -Wshadow switch for gcc.

I never would have thought to call it that.

Any idea where the term "shadow variable" in this context originated?
No. However, you may not want to use that, since one use of local
names is to deliberately prevent accidental use of the external
scopes object within the local scope. I.e. a local definition of
foo prevents accessing any external (scope) foo.

--
Chuck F (cbfalconer at maineline dot net)
<http://cbfalconer.home .att.net>
Try the download section.

--
Posted via a free Usenet account from http://www.teranews.com

Nov 30 '07 #6
On Nov 30, 11:20 am, David Mathog <mat...@caltech .eduwrote:
I accidentally did this the other day (it was a lot less obvious in the
much longer actual program, hundreds of lines are omitted):
[...]
>
Once I found the bug I was a bit surprised that the compiler had not
issued a warning. What does the standard say about using the same
Lint early. Lint often. Lint is your friend. www.gimpel.com

(No connection to Gimpel, just a satisfied customer).

Regards,

-=Dave
Nov 30 '07 #7
On Fri, 30 Nov 2007 09:20:21 -0800, David Mathog <ma****@caltech .edu>
wrote:
>I accidentally did this the other day (it was a lot less obvious in the
much longer actual program, hundreds of lines are omitted):

----------------------------------------------------------
#include <stdio.h>
#include <stdlib.h>

int gbl_var=0; /* one of several globals */

void compare(void){
(void) fprintf(stdout, "gbl_var is %d\n",gbl_var);
}

int main(void){
int gbl_var; /* <----- OOPS, left over from a previous version */
gbl_var=1;
/* much code, including a qsort where the compare function
needed the value of gbl_var, but read 0 instead of 1.
Here just call compare directly */
compare();
exit(EXIT_SUCCE SS);
}
-----------------------------------------------------------
% gcc -Wall -std=c99 -pedantic -o foo foo.c
% #no warnings or errors are reported
% ./foo
gbl_var is 0
------------------------------------------------------------

Once I found the bug I was a bit surprised that the compiler had not
issued a warning. What does the standard say about using the same
There is no error so no diagnostic is required. Some compilers will
produce a warning, basically a "did you really mean to do this" type
of reminder.
>variable name in two overlapping scopes like this? Apparently it allows
The variable defined inside the function basically hides the variable
at file scope for any statements in the function.
>it, I guess to avoid accidental name conflicts, for instance between a
global in a library and a similarly named variable in a function.
Still, it would have been nice if the compiler could have at least
optionally warned about this. Unfortunately so far none of the -W
switches I've tried have flagged this problem, including
-Wredundant-decls. Is there a "-Wvar-nested-scope" that I missed.
Questions about a specific compiler are better asked in a newsgroup
that discusses it.
Remove del for email
Dec 3 '07 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
2224
by: William Gill | last post by:
Working with tkinter, I have a createWidgets() method in a class. Within createWidgets() I create several StringVars() and assign them to the textvariable option of several widgets. Effectively my code structure is: def createWidgets(self): ... var = StringVar() Entry(master,textvariable=var) ...
166
8597
by: Graham | last post by:
This has to do with class variables and instances variables. Given the following: <code> class _class: var = 0 #rest of the class
23
1698
by: NotYetaNurd | last post by:
for(int i=0;i<6;i++) { // } wont i go out of scope after the for loop ...?
9
2201
by: Stefan Turalski \(stic\) | last post by:
Hi, I done sth like this: for(int i=0; i<10; i++) {...} and after this local declaration of i variable I try to inicialize int i=0;
20
6957
by: weston | last post by:
I've got a piece of code where, for all the world, it looks like this fails in IE 6: hometab = document.getElementById('hometab'); but this succeeds: hometabemt = document.getElementById('hometab'); Has anyone ever seen anything like this before, or am I dreaming?
3
2276
by: CrazyJohn | last post by:
Hi guys, This is my first time posting question here, if I break any rules, please kindly point out. And I'm really glad to be a part of this community. Here is my question, Our lecturer told us that Unnamed Namespace is an alternative to Static Internal Variables, but he also said that Namespace has an
5
2228
by: somenath | last post by:
Hi All , I have one question regarding scope and lifetime of variable. #include <stdio.h> int main(int argc, char *argv) { int *intp = NULL; char *sptr = NULL;
1
1264
by: Piotr Sobolewski | last post by:
Hello, there is something I don't understand about list comprehensions. I understand how does this work: print for y in range(8)] However I don't understand why this one works: print for y in range(8)]
112
5425
by: istillshine | last post by:
When I control if I print messages, I usually use a global variable "int silent". When I set "-silent" flag in my command line parameters, I set silent = 1 in my main.c. I have many functions that may print some messages. foo(...) { if (!silent)
0
8345
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8857
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8547
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8633
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7368
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
4181
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
2763
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1999
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1754
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.