2

I am confused with variable definition, when playing with goto and switch statements. The below code were accepted by compiler:

goto label0;
int j; // skipped by "goto"

label0:
j = 3;

My questions are:

  1. Since the definition of int j; is skipped, how will the program create object j and later assign value to it in j = 3 ?
  2. Is the code between goto and label compiled?
  3. Is the code between goto and label executed? (at run-time)
  4. Does variable definition happen at compile-time(or more proper term) or run-time?

(I ask this as a new question focusing more on the relative order of variable definition and compilation and execution. )

user2864740
  • 60,010
  • 15
  • 145
  • 220
modeller
  • 3,770
  • 3
  • 25
  • 49
  • 1
    Don't use `goto` in 1st place :P ... – πάντα ῥεῖ Jun 21 '14 at 20:09
  • IIRC, this is undefined behavior. I don't remember for sure and don't have a standard quote ready, though. – user2357112 Jun 21 '14 at 20:10
  • @πάνταῥεῖ, how about `switch`. Can we dodge that also? – modeller Jun 21 '14 at 20:12
  • 3
    *A program that jumps from a point where a variable with automatic storage duration is not in scope to a point where it is in scope is ill-formed unless the variable has scalar type, class type with a trivial default constructor and a trivial destructor, a cv-qualified version of one of these types, or an array of one of the preceding types and is declared without an initializer (8.5).* Didn't see anything about using that variable. – chris Jun 21 '14 at 20:13
  • @chris An int is a scalar (right?) so this section would imply the OP's code is well formed. – David Grayson Jun 21 '14 at 20:14
  • _'Can we dodge that also?'_ Yeah, we can! Have a look at this recently asked question: http://stackoverflow.com/questions/24343724/does-a-variable-defined-inside-a-switch-case-persists-its-value – πάντα ῥεῖ Jun 21 '14 at 20:15
  • @DavidGrayson, Actually, it would not (alone) AFAICT because nothing is said about using the variable that comes into scope. Maybe it's implied to be ok to use. I don't know. To answer the question, yes, `int` is a scalar. – chris Jun 21 '14 at 20:15
  • @πάνταῥεῖ, I doubt the ostrich style is the right way to go :P – modeller Jun 21 '14 at 20:18
  • @aruisdante, This is different. That question violates the "declared without an initializer" part. – chris Jun 21 '14 at 20:19
  • @user3701346 _'I doubt the ostrich style is the right way to go ...'_ Didn't mean to recommend it, but pointing out it's UB as well, as stated there!! – πάντα ῥεῖ Jun 21 '14 at 20:19
  • 1
    Remember: C++ (and C) accept *many* programs which are not well-defined.. just because it compiled without errors - remember to enable/check warnings - does not mean it is "legitimate". – user2864740 Jun 21 '14 at 20:20
  • @chris Yes, but the answers explain both cases. As does the various answers/duplicates linked in the question from πάντα ῥεῖ – aruisdante Jun 21 '14 at 20:20
  • @aruisdante, Oh, you're right. The second one does. And the other link was funny to see because I saw that question come up and thought nothing of this, just assumed it was ok to use. Conclusion: OP's code is perfectly valid. – chris Jun 21 '14 at 20:22
  • 1
    I think this code is fine. The problem that is prevented by the specification that @chris quoted is the *construction* of that variable. *Allocation* is often done somewhere else. `goto`s are pretty useless if you can't jump over side-effects. If a constructor is not called, that is bad -- OTOH you have to make sure yourself the objects are in the states *you* require them to be in when you jump to a certain part of the code. – dyp Jun 21 '14 at 20:24
  • @chris, if you would like, you should formulate an answer based on these comments and we can move on. – R Sahu Jun 21 '14 at 20:26
  • @RSahu, I think the combination of given links explains it pretty well, but I'm having a hard time with a decision of closing it as a single one, especially when two of the better answers are the second answers in their corresponding questions. – chris Jun 21 '14 at 20:45
  • @MattMcNabb, your comment itself is a duplicate of the answer below:-P – modeller Jun 22 '14 at 03:13
  • it autogenerates that comment when you flag article as duplicate – M.M Jun 22 '14 at 03:16

1 Answers1

1

Just found another question with close answer:

Why is it OK to jump into the scope of an object of scalar type w/o an initializer?

To summarize and answer the questions:

  1. The goto is a run-time thing. Definition still happens;
  2. Yes it is compiled.
  3. No it is not executed.
  4. Definition happens despite the part skipped by goto.
modeller
  • 3,770
  • 3
  • 25
  • 49