Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > error: jump to case label

Reply
Thread Tools

error: jump to case label

 
 
Neil Zanella
Guest
Posts: n/a
 
      04-02-2004
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
 
Reply With Quote
 
 
 
 
Bill Seurer
Guest
Posts: n/a
 
      04-02-2004
Neil Zanella wrote:

> I get this error when switching two case labels together with their bodies.


Explain that further or better yet post the offending code.
 
Reply With Quote
 
James Gregory
Guest
Posts: n/a
 
      04-03-2004
On Fri, 02 Apr 2004 15:09:00 -0800, Neil Zanella wrote:

> 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.
>


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

 
Reply With Quote
 
Neil Zanella
Guest
Posts: n/a
 
      04-03-2004
Bill Seurer <> wrote in message news:<c4kt55$1ahs$>...
> Neil Zanella wrote:
>
> > I get this error when switching two case labels together with their bodies.

>
> Explain that further or better yet post the offending code.


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);
}
 
Reply With Quote
 
Jacek Dziedzic
Guest
Posts: n/a
 
      04-04-2004
Neil Zanella wrote:
> Sure I will. Here is the code. Uncommenting the lines for case 1 produces
> the compiler error message.


Try moving them before the 'default'

> 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.


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.

>
> 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);
> }


HTH,
- J.
 
Reply With Quote
 
Neil Zanella
Guest
Posts: n/a
 
      04-04-2004
Jacek Dziedzic <jacek__NOSPAM__@janowo.net> wrote in message:

> Try moving them before the 'default'


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

Regards,

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);
> > }

>
> HTH,
> - J.

 
Reply With Quote
 
Buster
Guest
Posts: n/a
 
      04-04-2004
Neil Zanella wrote:

> Jacek Dziedzic <jacek__NOSPAM__@janowo.net> wrote in message:
>
>> Try moving them before the 'default'

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


Please don't top-post.

>>>#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);
>>>}


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.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: extern const variable in case label Martin Eisenberg C++ 0 10-02-2008 11:52 AM
switch case to a jump table sam_cit@yahoo.co.in C Programming 9 05-10-2007 05:03 PM
gcc: error: case label does not reduce to an integer constant SysSpider C Programming 29 12-31-2004 02:52 PM
Can't use object as a case label! JKop C++ 8 09-19-2004 03:47 PM
error: jump to case label cousaert C++ 9 08-27-2004 02:10 PM



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57