471,078 Members | 812 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,078 software developers and data experts.

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
Jul 22 '05 #1
11 16088
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.
Jul 22 '05 #2
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

Jul 22 '05 #3
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

Jul 22 '05 #4
Bill Seurer <se****@us.ibm.com> wrote in message news:<c4***********@news.rchland.ibm.com>...
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);
}
Jul 22 '05 #5
Bill Seurer <se****@us.ibm.com> wrote in message news:<c4***********@news.rchland.ibm.com>...
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);
}
Jul 22 '05 #6
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.
Jul 22 '05 #7
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.
Jul 22 '05 #8
Jacek Dziedzic <ja*************@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.

Jul 22 '05 #9
Jacek Dziedzic <ja*************@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.

Jul 22 '05 #10
Neil Zanella wrote:
Jacek Dziedzic <ja*************@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.
Jul 22 '05 #11
Neil Zanella wrote:
Jacek Dziedzic <ja*************@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.
Jul 22 '05 #12

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by Andreas | last post: by
10 posts views Thread by Neil Zanella | last post: by
11 posts views Thread by Steve Hoyer | last post: by
33 posts views Thread by Anthony England | last post: by
2 posts views Thread by dc15 | last post: by
9 posts views Thread by sam_cit | last post: by
3 posts views Thread by AJ32 | last post: by
12 posts views Thread by Franz Hose | last post: by
reply views Thread by leo001 | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.