goto skipping declarations
Steven Schveighoffer
schveiguy at gmail.com
Fri Sep 19 02:03:57 UTC 2025
On Friday, 19 September 2025 at 00:02:53 UTC, Walter Bright wrote:
> gcc does not produce an error:
>
> ```c
> int foo(int x)
> {
> goto END;
> int what;
> END: return what;
> }
> ```
>
> so D is still doing better! D gives an error. (ImportC does not
> give an error, on purpose!)
```c
int foo(int x) {
goto END;
int what = 0;
END:
return what;
}
```
Does yield an error:
```
<source>: In function 'int foo(int)':
<source>:5:5: error: jump to label 'END'
5 | END:
| ^~~
<source>:3:12: note: from here
3 | goto END;
| ^~~
<source>:4:11: note: crosses initialization of 'int what'
4 | int what = 0;
| ^~~~
Compiler returned: 1
```
So the skipping of initialization is technically caught, it's
just that D default initializes and C does not.
Let's try D code that doesn't initialize:
```d
int foo(int x) {
goto END;
int what = void;
END:
return what;
}
```
Same error. I still like the D mechanism better, as it's very
likely you didn't mean to do this, and it's generally better to
avoid this problem.
-Steve
More information about the Digitalmars-d
mailing list