Goto and automatic variable initializer in c

The textbook says

If you use the goto operator to go to the middle of a block, automatic variables inside this block are not initialized.

Then in the code below, if I can access / declared, then why is it not initialized?

int main() { goto here; { int i=10; here: printf("%d\n",i); } return 0; } 

ps: output is some amount of garbage.

+6
source share
6 answers

There is no logic to your question "if i can be accessed, why ...". The โ€œaccess i โ€ capability is not an argument for or against anything. It just means that the printf statement is in the same scope as i . However, since you jumped over the initializer, the variable is not initialized (as your tutorial says).

Reading an uninitialized variable is undefined behavior, so your program is poorly formed.

The memory for variable i already deferred at compile time, since it is known that the variable exists inside the internal block. Memory is not allocated dynamically, as you might imagine. It already exists, but it never fell into any division due to goto .

Rule of thumb: Do not jump over the initializers.

+9
source

Variables are visible in the area in which they are declared (between {} in this case), regardless of the order in which statements are executed within this area. goto bypasses i initialization, that is, it is undefined when calling printf() .

+2
source

Consider another, obvious situation:

 int main() { int i; //i is declared, but not initialized goto here; { i=10;//i is initialized here: //you've skipped the initialization printf("%d\n",i);//and got garbage } return 0; } 

In your case:

 int main() { goto here; { //printf("%d\n",i); // i does not exist here yet int i; //from here until the end of the scope variable i exists i=10; // i exists here and smth is written into it here: // i exists here printf("%d\n",i); // i exists here and it value is accessed } return 0; } 

So int i = 5; that really 2 things. One of them is a declaration and is not overlooked by anything, including goto (just like the opening of a new region is also not affected. You jumped into the middle of the region, but the region was already there). Secondly, this is the purpose of the operation, and since this is normal operation (program stream), it can be skipped using goto or 'break' or 'continue', or 'return'

+1
source

The C compiler will analyze the source file and "write" any variable initialization.
When he reaches

 printf("%d\n", i) 

he will know that the variable I already exists, and he should be able to use it, since it is in scope.
In the execution space, it is reserved for the variable i on the stack immediately after calling the main function and before executing any of the code in main ().

0
source

Since the locale says this:

6.7.8 Initialization

Semantics

If an object with automatic storage duration is not explicitly initialized, its value is undefined.

J.2 Undefined Behavior

Undefined behavior in the following cases:

The value of an object with automatic storage duration is used while it is undefined.

6.8.4.2 switch statement

EXAMPLE In a fragment of an artificial program

 switch (expr) { int i = 4; f(i); case 0: i = 17; /* falls through into default code */ default: printf("%d\n", i); } 

an object whose identifier exists exists with automatic storage duration (inside the block), but is never initialized, and thus, if the control expression has a nonzero value, the call to the printf function will have access to the undefined value. Similarly, a call to f cannot be reached.

0
source

C allows you to access something in your address space, regardless of whether it was actually initialized or not. Sometimes such work leads to crashes or shows garbage, sometimes it happens that it prints something useful, but all this behavior is undefined. A convenient trick, but a great way to break your program, so do not think that just getting the result means that your trick works.

-1
source

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


All Articles