Sign in to follow this  

Recommended Posts

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?

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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
}
}
}




Share this post


Link to post
Share on other sites
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();
}
}
}


Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites
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;
// or
operation { }


For example:

for (;;) scope;
// or
for (;;) {}
Same for while and do.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this