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.
uinside the loop? Otherwise, what's the point?uis not used inside the loop but it is used after the loop.uin the loop is from the definition until the bottom of the loop. Outside of that scope, the outeruis visible.