scope

Started by
9 comments, last by Colin Jeanne 15 years, 8 months ago
Does this ever cause a problem?

void somefunction(void);

int main()
{
   int ix, ij;

   for(ix=0;ix<10;ix++)
   {
      for(ij=0;ij<10;ij++)
      {
         somefunction();
      }
   }
}

int somefunction()
{
   int ix, ij;

   for(ix=0;ix<10;ix++)
   {
      for(ij=0;ij<10;ij++)
      {
         //do stuff
      }
   }
}

All I'm talking about is the main function and somefunction both having variables named ix and ij. From my understanding there's no problem with that at all, those are completely different variables and there's no need to give the loop integers different names in somefunction(). But I seem to remember it causing problems once or twice in the past. Am I remembering incorrectly?
Advertisement
No problem.
We should do this the Microsoft way: "WAHOOOO!!! IT COMPILES! SHIP IT!"
ix and ij are locally defined variables, local in scope to each function. They're only visible to those functions so it's no problem having two variables with the same name, providing that they are segregated like that.

There's no problem with your code in terms of variable scope; as it stands, the two instances of ix and ij won't affect each other at all.
A 'for' loop also has its own scope (if you declare the variables inside the loop), so it would be legal to do this:

int somefunction(){   for(int ix = 0; ix < 10; ix++)   {      for(int ij = 0; ij < 10; ij++)      {         //do stuff      }   }   for(int ix = 0; ix < 10; ix++)   {      for(int ij = 0; ij < 10; ij++)      {         //do some more stuff      }   }}


"The right, man, in the wrong, place, can make all the dif-fer-rence in the world..." - GMan, Half-Life 2

A blog of my SEGA Megadrive development adventures: http://www.bigevilcorporation.co.uk

I didn't know 'for' loops had their own scope, that's interesting. You didn't even need two variable names then, did you? You could have gone like this:
int main(){   for(ix=0;ix<10;ix++)   {      for(ix=0;ix<10;ix++)      {         somefunction();      }   }}
Quote:Original post by icecubeflower
I didn't know 'for' loops had their own scope, that's interesting. You didn't even need two variable names then, did you? You could have gone like this:
*** Source Snippet Removed ***

Close, you'd need to declare the variables within the for loop, like this:
int main(){   for(int ix=0;ix<10;ix++)   {      for(int ix=0;ix<10;ix++)      {         somefunction();      }   }}


Edit: horrible mistype.

[Edited by - Colin Jeanne on July 25, 2008 10:04:23 PM]
Oh yeah, that's what I meant.

...wow that's scary. A compiler wouldn't even have saved me if I already had an ix declared at the top of my program. That outer 'for' loop would have executed only one time.
Braces define scope.

{  int i;} <-- i no longer exists here{  int i; <-- different i} <-- second i no longer exists


This is sometimes used for RAII locks:
void foo() {  for (int i = 0; i < 100; i++)   {    int z = foo();    {a)    boost::lock lock;  <-- claim lock      { <-- redundant scope        baz();      }b) } <-- lock is released    z += bar();    }}
In this example, lock exists between a and b.


All auto-allocated objects live within the scope only.

Certain constructs also introduce scope, such as for and while. For those, the auto-scope is something like:
operation scope;// oroperation { }


For example:
for (;;) scope;// orfor (;;) {}
Same for while and do.
Quote:Original post by Colin Jeanne
Quote:Original post by icecubeflower
I didn't know 'for' loops had their own scope, that's interesting. You didn't even need two variable names then, did you? You could have gone like this:
*** Source Snippet Removed ***

Close, you'd need to declare the variables within the for loop, like this:
*** Source Snippet Removed ***

That will fail to compile, as you are attempting to declare ix again in the test statement of each for loop. Here is the syntactically correct version:
for(int ix = 0; ix < 10; ++ix){    for(int ix = 0; ix < 10; ++ix)    {        somefunction();    }}

Note that the ix declared in the outer loop is shadowed by the ix declared in the inner loop. This means that any use of ix in the inner loop will automatically refer to the inner ix. If you need to use the outer ix in the inner loop, you will need to use different names. I'd recommend doing so anyway.
What are you talking about your wrote the exact same code as Colin Jeanne.

This topic is closed to new replies.

Advertisement