error: jump to case label

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Neil Zanella

    error: jump to case label

    Hello,

    Does anyone know what the following g++ compiler error message means:

    error: jump to case label

    I get this error when switching two case labels together with their bodies.
    I have no setjmp/longjmp or gotos in my program.

    Thanks,

    Neil
  • Bill Seurer

    #2
    Re: error: jump to case label

    Neil Zanella wrote:
    [color=blue]
    > I get this error when switching two case labels together with their bodies.[/color]

    Explain that further or better yet post the offending code.

    Comment

    • James Gregory

      #3
      Re: error: jump to case label

      On Fri, 02 Apr 2004 15:09:00 -0800, Neil Zanella wrote:
      [color=blue]
      > Hello,
      >
      > Does anyone know what the following g++ compiler error message means:
      >
      > error: jump to case label
      >
      > I get this error when switching two case labels together with their bodies.
      > I have no setjmp/longjmp or gotos in my program.
      >[/color]

      Perhaps the problem is "jump to case label croses initialization" ?

      The following is not allowed:

      switch (a)
      {
      case 1:
      int a = 6;
      //stuff
      break;

      case 2:
      //stuff
      break;
      }

      The following is allowed:

      switch (a)
      {
      case 1:
      {
      int a = 6;
      //stuff
      }
      break;

      case 2:
      //stuff
      break;
      }

      James

      Comment

      • James Gregory

        #4
        Re: error: jump to case label

        On Fri, 02 Apr 2004 15:09:00 -0800, Neil Zanella wrote:
        [color=blue]
        > Hello,
        >
        > Does anyone know what the following g++ compiler error message means:
        >
        > error: jump to case label
        >
        > I get this error when switching two case labels together with their bodies.
        > I have no setjmp/longjmp or gotos in my program.
        >[/color]

        Perhaps the problem is "jump to case label croses initialization" ?

        The following is not allowed:

        switch (a)
        {
        case 1:
        int a = 6;
        //stuff
        break;

        case 2:
        //stuff
        break;
        }

        The following is allowed:

        switch (a)
        {
        case 1:
        {
        int a = 6;
        //stuff
        }
        break;

        case 2:
        //stuff
        break;
        }

        James

        Comment

        • Neil Zanella

          #5
          Re: error: jump to case label

          Bill Seurer <seurer@us.ibm. com> wrote in message news:<c4kt55$1a hs$1@news.rchla nd.ibm.com>...[color=blue]
          > Neil Zanella wrote:
          >[color=green]
          > > I get this error when switching two case labels together with their bodies.[/color]
          >
          > Explain that further or better yet post the offending code.[/color]

          Sure I will. Here is the code. Uncommenting the lines for case 1 produces
          the compiler error message. Furthermore, out of curiosity, as an unrelated
          matter, I am quite interested in knowing how come the program starts looping
          when some large number is entered.

          Thanks,

          Neil

          #include <iostream>

          int main() {
          unsigned int x; do {
          std::cout << "Please enter an integer: " << std::flush;
          std::cin >> x;
          switch (x) {
          case 0:
          std::cout << "Hello!" << std::endl;
          break;
          default:
          unsigned int y = ++x;
          std::cout << "You could have entered " << y;
          std::cout << ". Why didn't you?" << std::endl;
          break;
          //case 1:
          // std::cout << "What??? You entered one?" << std::endl;
          // break;
          }
          } while (x != 0);
          }

          Comment

          • Neil Zanella

            #6
            Re: error: jump to case label

            Bill Seurer <seurer@us.ibm. com> wrote in message news:<c4kt55$1a hs$1@news.rchla nd.ibm.com>...[color=blue]
            > Neil Zanella wrote:
            >[color=green]
            > > I get this error when switching two case labels together with their bodies.[/color]
            >
            > Explain that further or better yet post the offending code.[/color]

            Sure I will. Here is the code. Uncommenting the lines for case 1 produces
            the compiler error message. Furthermore, out of curiosity, as an unrelated
            matter, I am quite interested in knowing how come the program starts looping
            when some large number is entered.

            Thanks,

            Neil

            #include <iostream>

            int main() {
            unsigned int x; do {
            std::cout << "Please enter an integer: " << std::flush;
            std::cin >> x;
            switch (x) {
            case 0:
            std::cout << "Hello!" << std::endl;
            break;
            default:
            unsigned int y = ++x;
            std::cout << "You could have entered " << y;
            std::cout << ". Why didn't you?" << std::endl;
            break;
            //case 1:
            // std::cout << "What??? You entered one?" << std::endl;
            // break;
            }
            } while (x != 0);
            }

            Comment

            • Jacek Dziedzic

              #7
              Re: error: jump to case label

              Neil Zanella wrote:[color=blue]
              > Sure I will. Here is the code. Uncommenting the lines for case 1 produces
              > the compiler error message.[/color]

              Try moving them before the 'default'
              [color=blue]
              > Furthermore, out of curiosity, as an unrelated
              > matter, I am quite interested in knowing how come the program starts looping
              > when some large number is entered.[/color]

              cin.fail() is set and all subsequent ">>" operations are ignored,
              with x unmodified every time. Hence, if x happened to be non-zero,
              the loop iterates infinitely.
              [color=blue]
              >
              > Thanks,
              >
              > Neil
              >
              > #include <iostream>
              >
              > int main() {
              > unsigned int x; do {
              > std::cout << "Please enter an integer: " << std::flush;
              > std::cin >> x;
              > switch (x) {
              > case 0:
              > std::cout << "Hello!" << std::endl;
              > break;
              > default:
              > unsigned int y = ++x;
              > std::cout << "You could have entered " << y;
              > std::cout << ". Why didn't you?" << std::endl;
              > break;
              > //case 1:
              > // std::cout << "What??? You entered one?" << std::endl;
              > // break;
              > }
              > } while (x != 0);
              > }[/color]

              HTH,
              - J.

              Comment

              • Jacek Dziedzic

                #8
                Re: error: jump to case label

                Neil Zanella wrote:[color=blue]
                > Sure I will. Here is the code. Uncommenting the lines for case 1 produces
                > the compiler error message.[/color]

                Try moving them before the 'default'
                [color=blue]
                > Furthermore, out of curiosity, as an unrelated
                > matter, I am quite interested in knowing how come the program starts looping
                > when some large number is entered.[/color]

                cin.fail() is set and all subsequent ">>" operations are ignored,
                with x unmodified every time. Hence, if x happened to be non-zero,
                the loop iterates infinitely.
                [color=blue]
                >
                > Thanks,
                >
                > Neil
                >
                > #include <iostream>
                >
                > int main() {
                > unsigned int x; do {
                > std::cout << "Please enter an integer: " << std::flush;
                > std::cin >> x;
                > switch (x) {
                > case 0:
                > std::cout << "Hello!" << std::endl;
                > break;
                > default:
                > unsigned int y = ++x;
                > std::cout << "You could have entered " << y;
                > std::cout << ". Why didn't you?" << std::endl;
                > break;
                > //case 1:
                > // std::cout << "What??? You entered one?" << std::endl;
                > // break;
                > }
                > } while (x != 0);
                > }[/color]

                HTH,
                - J.

                Comment

                • Neil Zanella

                  #9
                  Re: error: jump to case label

                  Jacek Dziedzic <jacek__NOSPAM_ _@janowo.net> wrote in message:
                  [color=blue]
                  > Try moving them before the 'default'[/color]

                  Thank you for your reply...
                  I know that works but that doens't really explain the nature of the problem.

                  Regards,

                  Neil
                  [color=blue][color=green]
                  > > #include <iostream>
                  > >
                  > > int main() {
                  > > unsigned int x; do {
                  > > std::cout << "Please enter an integer: " << std::flush;
                  > > std::cin >> x;
                  > > switch (x) {
                  > > case 0:
                  > > std::cout << "Hello!" << std::endl;
                  > > break;
                  > > default:
                  > > unsigned int y = ++x;
                  > > std::cout << "You could have entered " << y;
                  > > std::cout << ". Why didn't you?" << std::endl;
                  > > break;
                  > > //case 1:
                  > > // std::cout << "What??? You entered one?" << std::endl;
                  > > // break;
                  > > }
                  > > } while (x != 0);
                  > > }[/color]
                  >
                  > HTH,
                  > - J.[/color]

                  Comment

                  • Neil Zanella

                    #10
                    Re: error: jump to case label

                    Jacek Dziedzic <jacek__NOSPAM_ _@janowo.net> wrote in message:
                    [color=blue]
                    > Try moving them before the 'default'[/color]

                    Thank you for your reply...
                    I know that works but that doens't really explain the nature of the problem.

                    Regards,

                    Neil
                    [color=blue][color=green]
                    > > #include <iostream>
                    > >
                    > > int main() {
                    > > unsigned int x; do {
                    > > std::cout << "Please enter an integer: " << std::flush;
                    > > std::cin >> x;
                    > > switch (x) {
                    > > case 0:
                    > > std::cout << "Hello!" << std::endl;
                    > > break;
                    > > default:
                    > > unsigned int y = ++x;
                    > > std::cout << "You could have entered " << y;
                    > > std::cout << ". Why didn't you?" << std::endl;
                    > > break;
                    > > //case 1:
                    > > // std::cout << "What??? You entered one?" << std::endl;
                    > > // break;
                    > > }
                    > > } while (x != 0);
                    > > }[/color]
                    >
                    > HTH,
                    > - J.[/color]

                    Comment

                    • Buster

                      #11
                      Re: error: jump to case label

                      Neil Zanella wrote:
                      [color=blue]
                      > Jacek Dziedzic <jacek__NOSPAM_ _@janowo.net> wrote in message:
                      >[color=green]
                      >> Try moving them before the 'default'[/color]
                      >
                      > Thank you for your reply...
                      > I know that works but that doens't really explain the nature of the problem.[/color]

                      Please don't top-post.
                      [color=blue][color=green][color=darkred]
                      >>>#include <iostream>
                      >>>
                      >>>int main() {
                      >>> unsigned int x; do {
                      >>> std::cout << "Please enter an integer: " << std::flush;
                      >>> std::cin >> x;
                      >>> switch (x) {
                      >>> case 0:
                      >>> std::cout << "Hello!" << std::endl;
                      >>> break;
                      >>> default:
                      >>> unsigned int y = ++x;
                      >>> std::cout << "You could have entered " << y;
                      >>> std::cout << ". Why didn't you?" << std::endl;
                      >>> break;
                      >>> //case 1:
                      >>> // std::cout << "What??? You entered one?" << std::endl;
                      >>> // break;
                      >>> }
                      >>> } while (x != 0);
                      >>>}[/color][/color][/color]

                      James Gregory is right. The 'jump' in the error message is the computed
                      goto effected by the switch statement. When x = 1, the switch acts like
                      this:

                      goto case_label_1;
                      // ...
                      unsigned int y = ++ x;
                      // ...
                      case_label_1:
                      // here y is uninitialized

                      The problem is that the initialization of y is skipped when x == 1.
                      When the "case 1:" label is reached, stack space has been allocated for
                      y but its value has not been initialized. This is not allowed.

                      The general way round this situation is to make the scope of y smaller
                      by adding braces:

                      switch (x)
                      {
                      default:
                      unsigned z; // this is OK since z is uninitialized
                      {
                      unsigned y = x + 1;
                      // ...
                      }
                      case 1:
                      z = 2;
                      // ...
                      // cannot refer to y here so no problem with y's initialization
                      }

                      --
                      Regards,
                      Buster.

                      Comment

                      • Buster

                        #12
                        Re: error: jump to case label

                        Neil Zanella wrote:
                        [color=blue]
                        > Jacek Dziedzic <jacek__NOSPAM_ _@janowo.net> wrote in message:
                        >[color=green]
                        >> Try moving them before the 'default'[/color]
                        >
                        > Thank you for your reply...
                        > I know that works but that doens't really explain the nature of the problem.[/color]

                        Please don't top-post.
                        [color=blue][color=green][color=darkred]
                        >>>#include <iostream>
                        >>>
                        >>>int main() {
                        >>> unsigned int x; do {
                        >>> std::cout << "Please enter an integer: " << std::flush;
                        >>> std::cin >> x;
                        >>> switch (x) {
                        >>> case 0:
                        >>> std::cout << "Hello!" << std::endl;
                        >>> break;
                        >>> default:
                        >>> unsigned int y = ++x;
                        >>> std::cout << "You could have entered " << y;
                        >>> std::cout << ". Why didn't you?" << std::endl;
                        >>> break;
                        >>> //case 1:
                        >>> // std::cout << "What??? You entered one?" << std::endl;
                        >>> // break;
                        >>> }
                        >>> } while (x != 0);
                        >>>}[/color][/color][/color]

                        James Gregory is right. The 'jump' in the error message is the computed
                        goto effected by the switch statement. When x = 1, the switch acts like
                        this:

                        goto case_label_1;
                        // ...
                        unsigned int y = ++ x;
                        // ...
                        case_label_1:
                        // here y is uninitialized

                        The problem is that the initialization of y is skipped when x == 1.
                        When the "case 1:" label is reached, stack space has been allocated for
                        y but its value has not been initialized. This is not allowed.

                        The general way round this situation is to make the scope of y smaller
                        by adding braces:

                        switch (x)
                        {
                        default:
                        unsigned z; // this is OK since z is uninitialized
                        {
                        unsigned y = x + 1;
                        // ...
                        }
                        case 1:
                        z = 2;
                        // ...
                        // cannot refer to y here so no problem with y's initialization
                        }

                        --
                        Regards,
                        Buster.

                        Comment

                        Working...