Errors, bugs, questions - page 2889

 
A100:

You have a random number 0, I have 540016640

but they're still random numbers.

I thought the MQ was zeroed out.

 
Roman:

I thought that MQ was zeroed out.

With global variables they seem to be initialized with zero, but in the body of functions I've encountered different initializations on the first pass. I try to always initialize before use. Although I agree, behavior must be unambiguous.

And another declaration in the loop body, in parentheses, makes the variable local to the loop. It's just not in the docs.

 
Roman:

Also, the compiler does not warn that the variable is not initialized.

Print(...) may not warn you as if the parameter was passed by a non-constant reference

int f( int& i ) { return i; }
void OnStart()
{
        int i;  
        f( i ); //нормально
}
 
A100:

If the loops are the same, then the compiler behaviour should be the same, and it is different. This is where the error lies. You explained why there is a warning in case (1), then explain why there isn't one in case (2) ? What has changed in principle? And then, if an uninitialized variable is used, why is the final result right when executing the code?

There is such a science - logic. If A and B are the same and A is red, B must also be red, not green

I have not studied programming, I can only speculate logically, as Vasily Ivanovich and Petka speculated on logic.

In the first case the whole string is executed

for ( int i = 0, j; i < 10; i = j )

and here the j variable is not initialized, but its value is assigned to the i variable

In the second case, even though the j variable isn't initial ized, in the next line where it is used, not its value is assigned, but its value is. In other words, the j variable is initialized with the value of the i variable

Here is a variant without warnings too

  for(int i = 0, j; i < 10; j = i)
   {
    j = i+1;
   }
 
Alexey Viktorov:

I did not study to be a programmer, I can only speculate logically, as Vasily Ivanovich and Petka speculated about logic.

In the first case the whole string is executed

and here the j variable is not initialized, but its value is assigned to the i variable

And who studied it? I think most people here (like me) are self-taught.

You need to study the for loop operator to understand the sequence of operations. In (1), the j variable is assigned the value of i+1

j = i+1

before it appears to the right of the assignment operator

i = j
 
Valeriy Yastremskiy:

The global variables seem to be initialised with zero, but the body of the function has encountered different initialisations on the first pass.
I always try to initialize before use. Although I agree, behavior should be unambiguous.

And another declaration in the loop body, in parentheses, makes the variable local to the loop. The docs just don't have it.

Yes, I always initialize variables too, it's like two times two,I should always remember.
Got it into my head from the C language ))
So this example of a loop with an uninitialized variable is bad practice.

 
Roman:

Yes, I always initialize variables too, it's like two times two,must remember always.
Got it into my head from the C language ))
So this example of a loop with an uninitialized variable is bad practice.

You need to initialize it but only with a meaningful value. There is no such value in the above example, so it is not a bad practice and is the only possible way. Otherwise there would be a double initialization

int f( int i ) { /*вычисления*/ }
void g( int k )
{
        for ( int i = 0, j; i < k; i = j )
        {
                j = f( i );
                /*вычисления*/
        }
}
 
A100:

Initialise, but only with a meaningful value. There is no such value in the above example, so the practice is not bad, it is the only possible one. Otherwise there would be double initialisation

What would it change if you initialised

int f( int i ) { /*вычисления*/ }
void g( int k )
{
   for ( int i=0, j=0; i < k; i = j )
   {
      j = f( i );
      /*вычисления*/
   }
}       
 
Roman:

What would it change if it was initialised with j=333?

And why by zero and not j=333 let's say? I.e. it's a meaningless initialisation that can hide a hard-to-find error

 
A100:

Why zero and not 333, say? I.e. it's a meaningless initialisation which can hide a hard to catch error

If you need to catch an error of this value, well, initialize it with 333 ))
It's just a start value.

Reason: