Jump to content
  • Advertisement
Sign in to follow this  

This topic is 3713 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

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
Advertisement
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
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.

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
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!