-6
#include <stdio.h>

int main()
{
    goto lb;
    static int a=5;

lb:
    goto b;
    int b=6;

b:
    printf("%d %d",a,b);
    return 0;
}

When I save this code with a ".c" file extension, it works well and gives output as 5 followed by a "garbage" value.

But, in C++, it results in an error. I can't understand why there is an error. Can you tell me how to resolve it?

Magnus Hoff
  • 21,529
  • 9
  • 63
  • 82
ojas360
  • 13
  • 1
  • 1
  • 5

3 Answers3

5

This has nothing to do with statics whatsoever. Your problem can be reproduced with a much smaller piece of code that has no static variables in it at all.

The compilation error is very clear:

main.cpp: In function 'int main()':
main.cpp:12:1: error: jump to label 'b' [-fpermissive]
 b:
 ^
main.cpp:9:10: error:   from here [-fpermissive]
     goto b;
          ^
main.cpp:10:9: error:   crosses initialization of 'int b'
     int b=6;
         ^

C++ has rules against goto-jumping across initialisations; this goes hand in hand with its support for classes and objects that are, in general, far more complex than the objects you can create in C.

You should read this post.

Community
  • 1
  • 1
Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055
3

In C, you're allowed to jump past the initialisation of a variable, and it will remain uninitialised, giving a garbage value (or perhaps other undefined behaviour).

In C++, you're not allowed to jump past the initialisation of a variable. That's because variables in C++ are, in general, more complicated beasts with constructors and destructors. Leaving them uninitialised could leave them in a state where the can't safely be destroyed, making the program go wrong in all manner of ways when they need to be destroyed; and so, instead, the language requires that they are correctly initialised.

At least on my compiler, the error message makes this quite clear:

test.cpp: In function ‘int main()’:
test.cpp:17:1: error: jump to label ‘b’ [-fpermissive]
test.cpp:13:6: error:   from here [-fpermissive]
test.cpp:15:5: error:   crosses initialisation of ‘int b’

explaining that it's an error to jump past the initialisation of the variable.

Mike Seymour
  • 249,747
  • 28
  • 448
  • 644
  • are you stalking me today ;) – Lightness Races in Orbit Oct 17 '14 at 13:54
  • 2
    Btw. do you know if this is a historical relic in C? If I remember right you used to have to declare all variables at the beginning of the scope with older versions, or am I confusing things (I assume this was done to only have to push the stack-pointer once, which most compilers today are still doing but I'm guessing compilers used to do less analysis in the past). edit: found it, [it was C89](http://stackoverflow.com/questions/288441/variable-declaration-placement-in-c) – PeterT Oct 17 '14 at 13:54
  • @PeterT That's an interesting question in its own right. – Lightness Races in Orbit Oct 17 '14 at 13:55
0

It is not static it is the goto statement in C++. goto can not cross an initialization in C++. http://en.wikipedia.org/wiki/Compatibility_of_C_and_C++

Prashant Kumar
  • 678
  • 6
  • 7