C++
Jump to Label Crosses Initialization Fix
A jump may not land at a point where a variable is in scope but its initialization never ran. A goto that skips an initialized declaration, and a switch that dispatches past one, both stop the build for the same reason. Each section below takes one shape, shows the exact message it prints, and gives the fix that belongs to that shape.
Written November 26, 2026 · Tested with GCC 13.3.0 · 9 min read
- The first error: a goto that jumps over an initialization
- The fix that moves the declaration above the jump
- A switch case that initializes a variable
- The fix that ends the scope with braces
Written November 26, 2026
Tested with GCC 13.3.0
What this guide is
This error is the build saying a jump lands somewhere a variable is in scope, but the code that initializes that variable was skipped. Landing there would leave the variable usable with no initialization behind it, and for a class type it would also leave a destructor to run later on an object that was never constructed. The language bars the jump instead of allowing that state.
Two statements produce it. A goto that jumps forward over an initialized declaration fails with jump to label and a note naming the crossed initialization. A switch fails with jump to case label, because dispatching to a case or default label is a jump to that label, and it crosses every initialization between the top of the switch body and the label. The fixes are moves and scope changes, not changes to the values: end the variable's scope before the label, move its declaration above the jump, or declare it without an initializer and assign after the label.
The examples here use C++20 and GCC 13.3.0. Each program is a separate file, compiled with g++ -std=c++20 and the file name. A failing compile prints its diagnostic to standard error and exits with status 1; a passing program is run, and its output is shown exactly as printed. Each section's failing program isolates one shape, so the diagnostic in that section belongs to that shape and nothing else.
The first error: a goto that jumps over an initialization
The smallest shape declares and initializes a variable between a goto and its label:
#include <iostream>
int main() {
goto done;
int value = 42;
done:
std::cout << "reached\n";
return 0;
}
$ g++ -std=c++20 goto_forward.cpp -o goto_forward
goto_forward.cpp: In function 'int main()':
goto_forward.cpp:6:1: error: jump to label 'done'
6 | done:
| ^~~~
goto_forward.cpp:4:10: note: from here
4 | goto done;
| ^~~~
goto_forward.cpp:5:9: note: crosses initialization of 'int value'
5 | int value = 42;
| ^~~~~
The error points at line 6, the label, and the two notes do the explaining: from here points at the goto on line 4, and crosses initialization of 'int value' points at the declaration on line 5. The label sits after the declaration, so value is in scope at the label. A jump from line 4 arrives there without line 5 ever running. That combination, in scope but never initialized by the path taken, is what the error names.
The fix that moves the declaration above the jump
Declare and initialize the variable before the goto, so no path to the label crosses its initialization:
#include <iostream>
int main() {
int value = 42;
goto done;
done:
std::cout << value << '\n';
return 0;
}
$ g++ -std=c++20 goto_forward_fixed.cpp -o goto_forward_fixed
$ ./goto_forward_fixed
42
The jump is unchanged. The declaration now runs on every path, including the jump, so the label no longer sits past an initialization. When the variable is only needed after the label, the same effect comes from declaring it after the label instead; what matters is that the declaration and the label are not on opposite sides of the jump with the variable in scope at the label.
A switch case that initializes a variable
Inside a switch, no goto is written at all, and the same rule fires. One case declares and initializes a variable, and every later label in the same switch body errors:
#include <iostream>
int main() {
int choice = 1;
switch (choice) {
case 1:
int value = 42;
std::cout << value << '\n';
break;
case 2:
std::cout << "two\n";
break;
default:
std::cout << "other\n";
break;
}
return 0;
}
$ g++ -std=c++20 switch_init.cpp -o switch_init
switch_init.cpp: In function 'int main()':
switch_init.cpp:10:14: error: jump to case label
10 | case 2:
| ^
switch_init.cpp:7:17: note: crosses initialization of 'int value'
7 | int value = 42;
| ^~~~~
switch_init.cpp:13:9: error: jump to case label
13 | default:
| ^~~~~~~
switch_init.cpp:7:17: note: crosses initialization of 'int value'
7 | int value = 42;
| ^~~~~
There is one error per label after the declaration: case 2 on line 10 and default on line 13 each get their own jump to case label error, and each carries the same note pointing back at line 7. Dispatching on choice jumps directly to the matched label. Jumping to case 2 or default enters the scope of value, whose declaration sits under case 1, without running its initialization. Even running with choice equal to 1 does not help: the error is about the jumps the switch can make, not about the value used in one run. The declaration's scope runs to the end of the switch body, which is why every later label is affected and case 1 itself is not.
The fix that ends the scope with braces
Wrap the case body in braces so the variable's scope ends before the next label:
#include <iostream>
int main() {
int choice = 1;
switch (choice) {
case 1: {
int value = 42;
std::cout << value << '\n';
break;
}
case 2:
std::cout << "two\n";
break;
default:
std::cout << "other\n";
break;
}
return 0;
}
$ g++ -std=c++20 switch_braces_fixed.cpp -o switch_braces_fixed
$ ./switch_braces_fixed
42
The braces give the case its own block. The scope of value now ends at the closing brace, before case 2, so dispatching to case 2 or default no longer enters that scope at all. This is the standard fix for this shape: it changes nothing about the logic, it keeps the variable next to the code that uses it, and it works for any number of later labels. A break inside the braces still exits the switch, as the run above shows.
The fix that moves the declaration before the switch
The second switch fix moves the declaration above the switch, next to the other variables the function sets up:
#include <iostream>
int main() {
int choice = 1;
int value = 42;
switch (choice) {
case 1:
std::cout << value << '\n';
break;
case 2:
std::cout << "two\n";
break;
default:
std::cout << "other\n";
break;
}
return 0;
}
$ g++ -std=c++20 switch_move_decl_fixed.cpp -o switch_move_decl_fixed
$ ./switch_move_decl_fixed
42
value is initialized before dispatch, so every label is reached with the initialization already done, and case 1 simply reads it. Prefer this form when several cases use the same variable. Prefer the braces form when only one case uses it, so the variable stays local to that case.
The fix that splits the case into a helper function
The third switch fix moves the case body, declaration included, into its own function:
#include <iostream>
void handle_one() {
int value = 42;
std::cout << value << '\n';
}
int main() {
int choice = 1;
switch (choice) {
case 1:
handle_one();
break;
case 2:
std::cout << "two\n";
break;
default:
std::cout << "other\n";
break;
}
return 0;
}
$ g++ -std=c++20 switch_helpers_fixed.cpp -o switch_helpers_fixed
$ ./switch_helpers_fixed
42
Inside handle_one, value is an ordinary local variable: declared, initialized, and used with no label after it in its scope. The switch itself now contains no declaration at all, so there is nothing for a case label to cross. This form pays off when the case body is long or shared; for a two-line case, the braces fix is smaller.
A class type crossed by a goto
The crossed variable does not need to be a built-in type. A std::string with an initializer fails the same way, with the type spelled out in the note:
#include <iostream>
#include <string>
int main() {
goto done;
std::string name = "veldica";
done:
std::cout << "reached\n";
return 0;
}
$ g++ -std=c++20 string_cross.cpp -o string_cross
string_cross.cpp: In function 'int main()':
string_cross.cpp:7:1: error: jump to label 'done'
7 | done:
| ^~~~
string_cross.cpp:5:10: note: from here
5 | goto done;
| ^~~~
string_cross.cpp:6:17: note: crosses initialization of 'std::string name'
6 | std::string name = "veldica";
| ^~~~
The wording matches the int shape: jump to label 'done', from here, and crosses initialization of 'std::string name'. The stakes are higher than for an int, because a std::string that is in scope will have its destructor run when the scope ends. A jump that skipped its constructor would leave that destructor running on an object that was never built. The fixes are the same three moves: braces around the declaration's scope where a label follows, the declaration above the jump, or a declaration without an initializer where the type allows one, covered below.
No initializer: a scalar passes and a class type still fails
Removing the initializer does not always remove the error. For a scalar type it does. This program jumps over an uninitialized int declaration and builds:
#include <iostream>
int main() {
goto done;
int value;
done:
std::cout << "reached\n";
return 0;
}
$ g++ -std=c++20 noinit_scalar.cpp -o noinit_scalar
$ ./noinit_scalar
reached
The same shape inside a switch, with the assignment after the declaration, also builds:
#include <iostream>
int main() {
int choice = 2;
switch (choice) {
case 1:
int value;
value = 42;
std::cout << value << '\n';
break;
case 2:
std::cout << "two\n";
break;
default:
std::cout << "other\n";
break;
}
return 0;
}
$ g++ -std=c++20 switch_noinit.cpp -o switch_noinit
$ ./switch_noinit
two
A scalar declared without an initializer has no initialization step to skip: jumping into its scope leaves it uninitialized, which the language allows, and the code is responsible for assigning before reading. For std::string, the same removal changes nothing:
#include <iostream>
#include <string>
int main() {
goto done;
std::string name;
done:
std::cout << "reached\n";
return 0;
}
$ g++ -std=c++20 noinit_string.cpp -o noinit_string
noinit_string.cpp: In function 'int main()':
noinit_string.cpp:7:1: error: jump to label 'done'
7 | done:
| ^~~~
noinit_string.cpp:5:10: note: from here
5 | goto done;
| ^~~~
noinit_string.cpp:6:17: note: crosses initialization of 'std::string name'
6 | std::string name;
| ^~~~
There is no = and no argument list in std::string name;, and the error still fires, with the same crosses initialization of 'std::string name' note. A class type with a non-trivial default constructor is initialized by the declaration itself: the constructor call is the initialization, whether or not it is written out. The switch form behaves identically:
#include <iostream>
#include <string>
int main() {
int choice = 2;
switch (choice) {
case 1:
std::string name;
std::cout << name.size() << '\n';
break;
case 2:
std::cout << "two\n";
break;
default:
std::cout << "other\n";
break;
}
return 0;
}
$ g++ -std=c++20 switch_string_cross.cpp -o switch_string_cross
switch_string_cross.cpp: In function 'int main()':
switch_string_cross.cpp:11:14: error: jump to case label
11 | case 2:
| ^
switch_string_cross.cpp:8:25: note: crosses initialization of 'std::string name'
8 | std::string name;
| ^~~~
switch_string_cross.cpp:14:9: error: jump to case label
14 | default:
| ^~~~~~~
switch_string_cross.cpp:8:25: note: crosses initialization of 'std::string name'
8 | std::string name;
| ^~~~
Both later labels error, exactly as in the int switch, even though name has no written initializer. The deciding question is never whether an initializer is visible in the source. It is whether entering the scope without running the declaration would skip an initialization the type actually performs: an explicit initializer, or a non-trivial constructor.
The fix that declares first and assigns after the label
Where the type allows it, split the declaration from the assignment: declare without an initializer before the jump, and assign after the label:
#include <iostream>
int main() {
int value;
goto done;
done:
value = 42;
std::cout << value << '\n';
return 0;
}
$ g++ -std=c++20 declare_assign_fixed.cpp -o declare_assign_fixed
$ ./declare_assign_fixed
42
The goto now crosses only an uninitialized scalar declaration, which is allowed, and the assignment runs after the label on the path that reaches the read. This fix is scalar-only in practice: it does not apply to the std::string shapes above, because their declarations initialize even with nothing written after the name. For a class type, use braces or move the declaration instead.
A neighbor that compiles: a backward goto over an initialization
Direction matters. A goto that jumps backward to a label placed before a declaration does not cross the initialization on the way to the label, and it builds:
#include <iostream>
int main() {
int count = 0;
retry:
int value = 42;
++count;
if (count < 2) {
goto retry;
}
std::cout << count << ' ' << value << '\n';
return 0;
}
$ g++ -std=c++20 goto_backward.cpp -o goto_backward
$ ./goto_backward
2 42
Each pass through the loop body reaches the declaration of value in normal order, so the initialization runs every time. Jumping backward out of a variable's scope is a different act from jumping forward into it: leaving a scope runs the destructors of the variables in it, in reverse order of construction, and is allowed. The error is specifically about entering a scope without running what the scope's declarations do.
A neighbor that is this error in disguise: jumping into a block
A label inside a block, jumped to from outside the block, produces the same family with the label's own name:
#include <iostream>
int main() {
goto inside;
{
int value = 42;
inside:
std::cout << "inside\n";
}
return 0;
}
$ g++ -std=c++20 goto_into_scope.cpp -o goto_into_scope
goto_into_scope.cpp: In function 'int main()':
goto_into_scope.cpp:7:5: error: jump to label 'inside'
7 | inside:
| ^~~~~~
goto_into_scope.cpp:4:10: note: from here
4 | goto inside;
| ^~~~~~
goto_into_scope.cpp:6:13: note: crosses initialization of 'int value'
6 | int value = 42;
| ^~~~~
The braces do not protect this shape, because the label is inside them: jumping to inside enters the block and lands past the declaration of value, in its scope, without its initialization. Compare the braces fix in the switch: there the label was outside the braces, so the scope had already ended. Braces fix a jump only when the label sits after the closing brace, not when it sits inside.
A neighbor that compiles: jumping over a whole block
If the jump clears the entire block, declaration and closing brace together, there is no error:
#include <iostream>
int main() {
goto done;
{
int value = 42;
std::cout << value << '\n';
}
done:
std::cout << "reached\n";
return 0;
}
$ g++ -std=c++20 goto_over_block.cpp -o goto_over_block
$ ./goto_over_block
reached
At the label done, value is not in scope at all: its scope began and ended inside the skipped block. Nothing at the label can name it, so nothing there can observe that its initialization never ran. This is the same principle as the switch braces fix, seen from the other side: the error needs the variable in scope at the label. End the scope before the label, by whatever structure fits, and the jump is legal.
A neighbor that compiles: a goto inside one scope region
A goto that skips ordinary statements, but no declaration, is not this error:
#include <iostream>
int main() {
int value = 42;
goto done;
value = 7;
done:
std::cout << value << '\n';
return 0;
}
$ g++ -std=c++20 goto_same_region.cpp -o goto_same_region
$ ./goto_same_region
42
value was declared and initialized above the goto, and the statement the jump skips is an assignment. The program prints 42, because the skipped assignment never runs. Skipping statements is what goto does; the restriction is only on skipping into a variable's scope past its initialization. If a goto in existing code compiles, this is usually why: every declaration it crosses is either above it, uninitialized and scalar, or out of scope at the label.
A neighbor that is not this error: break and continue
break and continue transfer control too, and neither produces this error:
#include <iostream>
int main() {
int choice = 1;
switch (choice) {
case 1:
std::cout << "one\n";
break;
case 2:
std::cout << "two\n";
break;
default:
std::cout << "other\n";
break;
}
for (int i = 0; i < 3; ++i) {
if (i == 1) {
continue;
}
std::cout << i << '\n';
}
return 0;
}
$ g++ -std=c++20 switch_break.cpp -o switch_break
$ ./switch_break
one
0
2
break exits the switch or the loop that contains it, and continue starts the next iteration of its loop. Neither jumps to a label inside a scope, and neither can land past an initialization with the variable still in scope at the landing point. When a build reports jump to case label, adding or moving a break does not address it: the case bodies fall through to the next label in the source, but the error is about the dispatch jump into the labels, not about fall-through between them. Fix the declaration's scope or position, not the break placement.
The exact rule: entering a scope without its initialization
Transfer of control may not enter the scope of a variable without running its initialization. A jump, whether written as a goto or performed by switch dispatch to a case or default label, is ill-formed when it lands at a point where a variable is in scope and the jump skipped the declaration that initializes it. That is the whole rule behind both messages: jump to label for a written goto, and jump to case label for a switch dispatch, each with a note naming the initialization that was crossed.
Entering the scope is allowed only for variables with nothing to skip: scalar types declared without initializers, class types with trivial default constructors and trivial destructors declared without initializers, cv-qualified versions of those, and arrays of those. An explicit initializer of any kind closes the door, and so does a non-trivial constructor or destructor, even when the declaration writes no initializer, as the std::string programs show. A variable whose scope has already ended at the label is not entered at all, which is why braces around a case body and a jump over a complete block both compile.
Leaving a scope is unrestricted by this rule. A backward jump, or a jump out of a block, exits the scopes it leaves and runs the destructors of the automatic variables in them, in the reverse order of their construction. The asymmetry is the point: destruction of a constructed object is well defined, while use of an object whose construction was skipped is not.
The fixes, ranked
- First, for a
switch, wrap the offending case body in braces. It is the smallest change, it keeps the variable local to the case, and it fixes every later label at once. - Second, move the declaration above the jump: above the
goto, or above theswitchwhen several cases share the variable. The initialization then runs before any jump can be taken. - Third, for a long or shared case body, move it into a helper function and call it from the case. The declaration becomes an ordinary local in the function, outside the switch entirely.
- Fourth, for a scalar only, declare without an initializer before the jump and assign after the label. Do not use this form for a class type with a non-trivial constructor or destructor: its declaration initializes even with no initializer written.
- Fifth, check which side of the braces the label is on before reaching for braces as a fix: a label inside the block is still crossed, as the block program shows. The label must sit after the closing brace.
Which fix for which cause
| Cause | Message | Fix |
|---|---|---|
goto jumps forward over an initialized declaration |
jump to label 'done' with note crosses initialization of 'int value' |
Move the declaration above the goto, or after the label |
switch case declares and initializes a variable, later labels follow |
jump to case label with note crosses initialization of 'int value', one error per later label |
Wrap the case body in braces, move the declaration before the switch, or split the case into a helper function |
| Class type with a non-trivial constructor crossed by a jump | jump to label or jump to case label with note crosses initialization of 'std::string name' |
Braces or a moved declaration; removing the written initializer does not help |
| Scalar declared without an initializer crossed by a jump | No error | None needed; assign before any read |
| Label inside a block, jumped to from outside | jump to label 'inside' with note crosses initialization of 'int value' |
Move the label out of the block, or the declaration above the jump |
| ## Checklist |
- Read the note first: it names the variable and the line of the declaration whose initialization was crossed.
- Count the errors in a
switch: one per label after the declaration. Fixing the declaration fixes all of them together. - Ask whether the variable is in scope at the label. If its scope ends before the label, there is no error to fix.
- Ask what the declaration does: an explicit initializer, or a non-trivial constructor, counts as initialization even when nothing is written after the name.
- For a
switch, reach for braces around the case body first; move the declaration before theswitchwhen cases share the variable. - Keep the declare-then-assign split for scalars only, and assign after the label before any read.
- Separate the neighbors before fixing: a backward
goto, a jump over a whole block, a jump that skips only assignments, andbreakorcontinueare all legal and need no change.
Related guides on this site
- Fix C++ "Break Outside Loop" — break in an if, a Function, and a Lambda — the companion control-flow error, where a
breakappears where no loop or switch encloses it. - Fix "Else Without a Previous If" in C++ — the companion statement-structure error, where a statement is separated from the construct it belongs to.
- C++ Loops: for, while, and Range-Based for — the loop forms whose
breakandcontinuetransfers are covered as neighbors in this guide. - C++ Variables and Data Types — declarations and initialization of the built-in types whose initialized declarations this error protects.
Sources
- goto statement — for transfer of control entering the scope of a variable being ill-formed unless every variable entered is a scalar type declared without an initializer, a class type with trivial default constructor and trivial destructor declared without an initializer, a cv-qualified version of one of those, or an array of one of those, and for destructors being called, in reverse order of construction, when control exits a scope.
- switch statement — for control passing to the statement labeled by the matched
caselabel or thedefaultlabel, forcaseanddefaultlabels not altering flow by themselves, and for a declaration inside the switch body having to be scoped in its own compound statement because transfer of control is not permitted to enter the scope of a variable. - Scope — for block scope being introduced by a compound statement and by selection statements including
switch, and for a variable that belongs to a block scope being a block variable.
Keep Exploring
Seven tips and seven puzzles so far, each one a short runnable example with the fix explained.
Paste an AddressSanitizer, UBSan, or ThreadSanitizer report and get the bug, the file and line, and the fix \u2014 in plain language.
C++20 and C++23 features, migration paths, and measured trade-offs.
The full C++ hub \u2014 language basics, the standard library, coroutines, and measured comparisons.
Paste a [Service] section and get a plain-language verdict on every sandboxing setting it finds.