2

Consider the following code snippet:

unsigned int i; double* u = new double [10]; for (i=0; i<10; i++) { double u = 5.0; // Other code } delete[] u; 

Is it okay to use the variable name u twice? Or is this frowned upon? Will the code fail to compile using certain compilers?

EDIT: Is this better? Or is it still confusing for future maintainers of the code?

unsigned int i; double* u = new double [10]; // Do stuff with u delete[] u; for (i=0; i<10; i++) { double u = 5.0; // Other code } 
10
  • Surely you need to use the outer u inside the loop? Otherwise, what's the point? Commented Jul 18, 2014 at 14:49
  • The inner variable u hides the outer variable u in their scope and inners scopes, depending the case would be possible or not to refer the outer variable in some way. Commented Jul 18, 2014 at 14:51
  • If I remembered correctly it will work, but it's a bad practice and you almost never have to do this. Commented Jul 18, 2014 at 14:51
  • This is just a simple example. The outer u is not used inside the loop but it is used after the loop. Commented Jul 18, 2014 at 14:52
  • @πάνταῥεῖ Why not? The scope of the u in the loop is from the definition until the bottom of the loop. Outside of that scope, the outer u is visible. Commented Jul 18, 2014 at 14:57

2 Answers 2

4

The code

double u = 5.0; 

inside of the loop will shadow the definition

double* u = new double [10]; 

made prior to it.
All of your code inside the loop just sees double u. It's meant to compile fine, and besides introducing confusion for future maintainers of the code, there's nothing wrong from a syntactical point of view.


Note:
If you aren't using double* u inside of the loop, there's no point defining this variable before it. As a rule of thumb:

Local variable definitions should appear nearest before their first point of usage.


As for your edited question:
Yes this will be clearer, in the sense that one could see the intend double* u; shouldn't be used after the delete[] u;. Though it's neither safe, and still confusing, if one's not spotting the shadowing definition of double u; inside of the loop immediately.

The IMHO overall better solution, to not confuse anyone, would be to factor out the code in the loop, or the code dealing with the double* u into a separate function, or even simply use a different variable name.

Sign up to request clarification or add additional context in comments.

4 Comments

Say the outer u is only used before the loop. Is it still confusing for future maintainers of the code if I move delete[] u; before the loop. [I EDITED THE QUESTION TO REFLECT THIS]
@krylov 'Is it still confusing for future maintainers ...' I'd say no. But IMHO it would be better to completely factor out the code dealing with the array to a separate function, or simply use different names.
Would the downvoters mind to explain what should be improved in this answer, or where I state something wrong please?
@krylov You don't need to shout about your edits to me BTW. I'm old, but neither blind or deaf so far :P ...
1

That will work with most compilers. However, it is generally not a good practice as it can lead to confusion on the part of the programmer, particularly if the program becomes large.

1 Comment

Yes, there is a lot of long debugging sessions supporting that.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.