-3

Can you please upvote this post since others show interest in answering and commenting on it? The guy who likes to devote things clearly doesn't know how to remove devoting or upvote a post back.

I'm trying to use labels in my project but when I jump over a set of instructions using goto to transfer control to another section of the code, I get this error that says: transfer control bypasses initialization of (certain variables).

This is the code that produces the error:

    goto label1;

    label00:
    int a = 0;//the compiler can't let me skip this line
    int b; // but this line is fine to skip over
    b = 0; //because i initialize it here instead of doing it like the a variable

    label1:
    //other instructions

as you can see I have two variables initialized but one of them is defined then initialized but the other one is defined and initialized at the same line.

The one that is defined and initialized at the same line variable a does not produce an error when skipped over, but the other one does.

I'm using VS2019 to compile this code. I think this should not throw an error at all and the compiler should give you a warning so that you know you're skipping something in both cases a and b initializations.

Is there any solution to this like disabling something in the settings?

I don't want to declare my variables then initialized them when using labels.

segfaulty
  • 39
  • 1
  • 7
  • why do you think it should be no error? – 463035818_is_not_an_ai Feb 01 '22 at 16:45
  • are the other instructions using variables a or b? or neither – NeonFire Feb 01 '22 at 16:45
  • Put that variable block inside `{` ... `}` curly braces, so they are scoped. – Eljay Feb 01 '22 at 16:45
  • 1
    Obligatory warning: [`goto` is bad](https://stackoverflow.com/questions/11906056/is-using-a-goto-statement-bad) because it causes confusion, and is either _rarely or never_ necessary. – Drew Dormann Feb 01 '22 at 16:50
  • 2
    `goto` is surprisingly hard to get right. It's even harder to convince others that you did get it right, so I tend to avoid it even when it's a correct and clean solution to a problem. In the past I've spent more time fighting to get a `goto` through a code review and then explaining it and why it's better to code maintainers than I would have spent writing the code without the `goto` in the first place. – user4581301 Feb 01 '22 at 16:52
  • Also, if your code is littered with `goto` statements, and you would like others to look at your code to fix an issue with the code in some way, I can guarantee that the number of persons that will help you will be reduced by a very large percentage, and on the extremes, no one will help you out. The reason is that persons will not waste their time trying to untangle [spaghetti code](https://en.wikipedia.org/wiki/Spaghetti_code). It may sound unfair, but that's the real world of helping out on programming issues. – PaulMcKenzie Feb 01 '22 at 16:56
  • @NeonFire no I don't use these variables at any line and still have the error, in my code when I skip I skip the initializations and the utilization of them altogether, so that's why I think it should be no error in doing this. – segfaulty Feb 01 '22 at 16:57
  • @Eljay Thank you it worked by initializing them inside **{}**, but I don't understand what's happening here. – segfaulty Feb 01 '22 at 16:59
  • Re: "I'm trying to use labels" -- don't do that. – Pete Becker Feb 01 '22 at 17:02
  • Variables `a` and `b` are still valid variables after the label but have undefined values. You may never use them again, but the compiler doesn't look deeply enough to see this, nor does it have to because the language grammar says this is illegal. Further, without this protection some future programmer might update your code to use them with comedic results. – user4581301 Feb 01 '22 at 17:02
  • Adding in the curly braces limits the lifespan of those variables, so there is no problem if a `goto` jumps over the scoped block. (I'm abstaining from wisdom on the use of `goto` in the first place.) – Eljay Feb 01 '22 at 17:03
  • @Eljay ok, so I have to add all the code of declaring the variables and using them inside one scope. – segfaulty Feb 01 '22 at 17:06
  • @user4581301 why they are valid variables even if you skip the declaration of them. – segfaulty Feb 01 '22 at 17:20
  • The program skips the initialization at runtime. Compiler still sees the declarations in the code and produces the variables at compile time. – user4581301 Feb 01 '22 at 17:55
  • @user4581301 so I might find a compiler that lets you do this without an error? – segfaulty Feb 01 '22 at 18:07
  • You might, but it would be a non-Standard compiler. What you've done is flat-out illegal because it's an easy way to prevent programmer errors. – user4581301 Feb 01 '22 at 18:15

1 Answers1

2

I think this should not throw an error at all

The compiler is free to refuse to compile ill-formed programs.

Is there any solution to this

Solutions:

  1. Don't initialise a.
  2. Declare a before the jump.
  3. Declare a after the label.
  4. Don't use the goto (my favourite).
  5. Limit the scope of a by declaring it within a block statement that ends before the label.
eerorika
  • 232,697
  • 12
  • 197
  • 326
  • 1) how do I supposed to use it if I don't initialize it? 2) it's not the best solution, and I think it's not good when you consider debugging. 3) I want a to be declared and used before that label 4) I have to – segfaulty Feb 01 '22 at 17:04
  • The best solution until now is using **{}** to declare and initialize the variables. – segfaulty Feb 01 '22 at 17:05
  • @lovethisstuff `1) how do I supposed to use it if I don't initialize it?` Option 1: If you don't use it, then it's not a problem that you cannot use it. Option 2: You can use it by assigning a value to it. Option 3: Use one of the other solutions that I provided in the answer. `2) it's not the best solution, and I think it's not good when you consider debugging.` I disagree. It's easier to debug a program that compiles and runs than one that doesn't compile at all. Furthermore, I don't see how it would be detrimental to debugging. – eerorika Feb 01 '22 at 17:09
  • 2
    @lovethisstuff `3) I want a to be declared and used before that label` Solution: Stop wanting this. Or pick one of the other solutions. `4) I have to` Why do you "have to" use goto? – eerorika Feb 01 '22 at 17:11
  • @lovethisstuff `The best solution until now is using {} to declare and initialize the variables.` I added it to list of solutions. – eerorika Feb 01 '22 at 17:16
  • ok, then I'll pick the last solution which is the best one for my case. – segfaulty Feb 01 '22 at 17:17