In the following piece of code
label:
int a;
goto label;
Does it create new variables or use same variable
Also what happens when we call function again and again using goto
In the following piece of code
label:
int a;
goto label;
Does it create new variables or use same variable
Also what happens when we call function again and again using goto
First of all, this won't build since a label must be followed by a statement, and a declaration isn't a statement:
6.8.1 Labeled statements
Syntax
1 labeled-statement: identifier : statement case constant-expression : statement default : statement
Secondly, this shouldn't create a new variable. goto
does not introduce a new scope, and therefore doesn't create a new instance of a
each iteration. And even in situations where you are introducing a new scope, such as
for (;;) {int a; ... }
the space for a
is (usually) only allocated once; even though logically you are dealing with a new instance of a
for each loop iteration, physically you are (usually) recycling the same memory location. Any compiler that physically created new space for a
without reclaiming the previous space would be grievously broken IMO.
Just for giggles I wrote the following:
#include <stdio.h>
#ifdef __STDC_VERSION__
#if __STDC_VERSION__ >= 199901L
#define C99
#endif
#endif
int main(void)
{
label:
#ifdef C99
;
#endif
int a;
printf("&a = %p\n", (void *) &a);
goto label;
return 0;
}
built it with gcc -std=c99 -pedantic -Wall -Werror
, and I get the following output:
&a = 0xbf98ad8c &a = 0xbf98ad8c &a = 0xbf98ad8c &a = 0xbf98ad8c &a = 0xbf98ad8c &a = 0xbf98ad8c &a = 0xbf98ad8c &a = 0xbf98ad8c &a = 0xbf98ad8c &a = 0xbf98ad8c &a = 0xbf98ad8c &a = 0xbf98ad8c &a = 0xbf98ad8c &a = 0xbf98ad8c
No It will not declare that variable again and allocate any memory to it. It will use the same variable.
Moreover your program is wrong, a label cannot be in front of any declaration, it must be before a statement and declaration is not a statement. So to correct it put a semi colon after the label.
This question could as well have been "how and when does the compiler allocate a local variable?", because that is the issue here.
Lets say we have code like this:
{
lots_of_code();
int x;
lots_of_code();
x = 5 + something;
lots_of_code_not_using_x();
return x;
}
This code is in a local scope. In this case it looks like the inside of a function, but the same rules would apply to the inside of an if-statement, for loop etc. When encountering this code, the compiler will ask itself some questions:
x = 5 + something;
is executed. It isn't necessarily allocated even then, the compiler could as well re-order the instructions, choosing to execute lots_of_code_not_using_x();
first, if that is more efficient and does not change the meaning of the code. In that case, x is allocated when the code reaches the return statement.x is guaranteed by the standard to be valid all the way from the point of entering the scope, until the end of the scope. 9899:2011 6.2.4
"5 An object whose identifier is declared with no linkage and without the storage-class specifier static has automatic storage duration, as do some compound literals..." /--/
"6 For such an object that does not have a variable length array type, its lifetime extends from entry into the block with which it is associated until execution of that block ends in any way. (Entering an enclosed block or calling a function suspends, but does not end, execution of the current block.) If the block is entered recursively, a new instance of the object is created each time. The initial value of the object is indeterminate. If an initialization is specified for the object, it is performed each time the declaration or compound literal is reached in the execution of the block; otherwise, the value becomes indeterminate each time the declaration is reached."
So to answer the question: no, your goto spaghetti doesn't affect how the variable is allocated in the slightest. Had there been any code using the variable, for example int a = 0;
then that code would possibly have been executed over and over again, depending on how smart the compiler is to optimize away the whole goto mess.