Why is it normal to go into the scope of an object of scalar type without an initializer?

When I read the C ++ standard, it seems that the following code perfectly matches the standard.

int main() { goto lol; { int x; lol: cout << x << endl; } } // OK 

[n3290: 6.7 / 3]: You can pass to a block, but not to a path that bypasses initialization declarations. A program that jumps from a point where the variable with the automatic storage time is not within the point where it is in the scope is poorly formed , if only the variable has a scalar type , the class type with the trivial default constructor and trivial destructor, cv- A qualified version of one of these types or an array of one of the previous types and declared without an initializer .

Why is this needed? Isn't it dangerous to jump over its definition and use undefined x ? And why does the existence of an initializer matter?

+6
source share
2 answers

In any case, you would use uninitialized x , since int x; as uninitialized as it is going to receive. The existence of an initializer, of course, matters because you missed it. int x = 5; , for example, initializes x , so it would be important if you jump over it or not.

+7
source

Is it dangerous to jump over its definition and use uninitialized x ?

But x will be uninitialized anyway, because it was declared without an initializer! Thus, goto can skip assignment instructions that set (sort-of-initialize) x , but it is not surprising that goto can skip assignment operations; and the declaration itself does virtually nothing if there is no initializer.

+2
source

Source: https://habr.com/ru/post/903885/


All Articles