Velocity Reviews - Computer Hardware Reviews

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

Reply
Thread Tools

error: jump to case label

 
 
cousaert
Guest
Posts: n/a
 
      08-27-2004
Newsgroups: alt.comp.lang.learn.c-c++
Date: Fri, 27 Aug 2004 12:36:11 +0200
Lines: 36
User-Agent: KNode/0.7.6
MIME-Version: 1.0
Content-Type: text/plain; charset=us-ascii
Content-Transfer-Encoding: 7Bit

I get the error "jump to case label" when I try to compile some code with
this in:


int type = randrange(1,1);
int *blok = new int;
switch(type)
{
case 1:
int blok[16] = {0, 0, 0, 0,0, 1, 1, 0, 0, 1, 1, 0, 0, 0, 0,
0 };
blok = blok1;
break;
case '2':
int blok1[16] = {0, 0, 0, 0,0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 0,
0 };
blok = blok1;
break;
}

Can somebody tell me what I'm doing wrong?
If I try this:

int type = randrange(1,1);
int *blok = new int;
if(type==1)
{
int blok1[16] = {0, 0, 0, 0,0, 1, 1, 0, 0, 1, 1, 0, 0, 0, 0, 0 };
blok = blok1;
}
if(type==2)
{
int blok1[16] = {0, 0, 0, 0,0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0 };
blok = blok1;
}



 
Reply With Quote
 
 
 
 
cousaert
Guest
Posts: n/a
 
      08-27-2004
cousaert wrote:

Oh yeah, I'm working with gcc3.3.2 on Mandrake 10.0
 
Reply With Quote
 
 
 
 
Marcelo Slomp
Guest
Posts: n/a
 
      08-27-2004
"cousaert" <(E-Mail Removed)> wrote in message
news:9oEXc.1441$(E-Mail Removed)-ops.be...
> Newsgroups: alt.comp.lang.learn.c-c++
> Date: Fri, 27 Aug 2004 12:36:11 +0200
> Lines: 36
> User-Agent: KNode/0.7.6
> MIME-Version: 1.0
> Content-Type: text/plain; charset=us-ascii
> Content-Transfer-Encoding: 7Bit
>
> I get the error "jump to case label" when I try to compile some code with
> this in:
>
>
> int type = randrange(1,1);
> int *blok = new int;
> switch(type)
> {
> case 1:
> int blok[16] = {0, 0, 0, 0,0, 1, 1, 0, 0, 1, 1, 0, 0, 0,

0,
> 0 };
> blok = blok1;
> break;
> case '2':
> int blok1[16] = {0, 0, 0, 0,0, 0, 1, 1, 1, 1, 0, 0, 0, 0,

0,
> 0 };
> blok = blok1;
> break;
> }
>
> Can somebody tell me what I'm doing wrong?
> If I try this:
>
> int type = randrange(1,1);
> int *blok = new int;
> if(type==1)
> {
> int blok1[16] = {0, 0, 0, 0,0, 1, 1, 0, 0, 1, 1, 0, 0, 0, 0, 0 };
> blok = blok1;
> }
> if(type==2)
> {
> int blok1[16] = {0, 0, 0, 0,0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0 };
> blok = blok1;
> }
>
>
>


you wrote:
case 1:
...
case '2':
remove the ' ' from the last case statement


 
Reply With Quote
 
Conrad Weyns
Guest
Posts: n/a
 
      08-27-2004

"cousaert" <(E-Mail Removed)> wrote in message
news:9oEXc.1441$(E-Mail Removed)-ops.be...
> Newsgroups: alt.comp.lang.learn.c-c++
> Date: Fri, 27 Aug 2004 12:36:11 +0200
> Lines: 36
> User-Agent: KNode/0.7.6
> MIME-Version: 1.0
> Content-Type: text/plain; charset=us-ascii
> Content-Transfer-Encoding: 7Bit
>
> I get the error "jump to case label" when I try to compile some code with
> this in:
>
>
> int type = randrange(1,1);
> int *blok = new int;
> switch(type)
> {
> case 1:
> int blok[16] = {0, 0, 0, 0,0, 1, 1, 0, 0, 1, 1, 0, 0, 0,

0,
> 0 };
> blok = blok1;


blok1 ?
/conrad weyns


> break;
> case '2':
> int blok1[16] = {0, 0, 0, 0,0, 0, 1, 1, 1, 1, 0, 0, 0, 0,

0,
> 0 };
> blok = blok1;
> break;
> }
>
> Can somebody tell me what I'm doing wrong?
> If I try this:
>
> int type = randrange(1,1);
> int *blok = new int;
> if(type==1)
> {
> int blok1[16] = {0, 0, 0, 0,0, 1, 1, 0, 0, 1, 1, 0, 0, 0, 0, 0 };
> blok = blok1;
> }
> if(type==2)
> {
> int blok1[16] = {0, 0, 0, 0,0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0 };
> blok = blok1;
> }
>
>
>



 
Reply With Quote
 
Boris Glawe
Guest
Posts: n/a
 
      08-27-2004
cousaert wrote:
> Newsgroups: alt.comp.lang.learn.c-c++
> Date: Fri, 27 Aug 2004 12:36:11 +0200
> Lines: 36
> User-Agent: KNode/0.7.6
> MIME-Version: 1.0
> Content-Type: text/plain; charset=us-ascii
> Content-Transfer-Encoding: 7Bit
>
> I get the error "jump to case label" when I try to compile some code with
> this in:
>
>
> int type = randrange(1,1);
> int *blok = new int;

/*why do you call "new int". Further down you say "blok = blok1", which means,
that you don't have a pointer to the memory area where your int value has been
stored anymore. This is called a memory leak: You reserved memory with "new" but
you cannot access or release this memory anymore. Calling this one in a loop
will result in extremely high memory usage for nor reason.
Just say

int*blok; //nothing more

*/
> switch(type)
> {
> case 1:
> int blok[16] = {0, 0, 0, 0,0, 1, 1, 0, 0, 1, 1, 0, 0, 0, 0,
> 0 };

// you can ommit the array's size, if you initialize it explicitly (since the
compiler can gues the size from the number of elements)
> blok = blok1;

// blok1 has never been defined or declared before.
> break;
> case '2':
> int blok1[16] = {0, 0, 0, 0,0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 0,
> 0 };
> blok = blok1;
> break;
> }


/*
You defined a pointer above called "blok" Then you're trying to point the
pointer to an array, which is alive within the switch statement only. If the
switch statement is done, the array's memory is released again (which means,
that it doesn't belong to your program anymore), but your pointer is still
pointing at this memory area. If you try to access the data, the pointer is
pointing to (what is called dereferencing a pointer), you try to access
data/memory, which does not belong to your program anymore. The operating system
will kick you out of the race with a segmentation fault of memory access
violation or whatever it's called.

A solution would be to create the array with malloc:

int *blok = new int[16]

//now you have to assigen it the values:

switch (type){

case 1:
int i
for (i =0; i< 16 ; i++) blok[i] = 0;
blok[5] = 1;
blok[6] = 1;
blok[9] = 1;
blok[10] = 1;
break;

[...]
case2:
int i
for (i =0; i< 16 ; i++) blok[i] = 0;
blok[6] = 1;
blok[7] = 1;
blok[8] = 1;
blok[9] = 1;
break;

}


now you can continue with whatever you're planing to do.

In the end, when you don't need blok anymore call this:

delete[] blok

which releases the memory, you've reserved within the swich-statement.

(keep in mind there is a difference between delete and deleter[]. The first one
deletes the memory the pointer is pointing to, the second one deletes an array,
the pointer is pointing to.)

The technical difference between the two is, that in your code, the memory for
the array is allocated and released automatically

You say to the compiler "I need a block of memory where 16 integers fit in and
please assign the memory with my values. Delete the memory again, if I'm leaving
the switch statement".

What I do is
"give me a block of memory where 16 integers fit in. Save the start of this
block in my pointer (here it's blok). I'll care myself about the fate of the memory"

This is necessary, since you don't want your array to be deleted after having
left the switch statement. If you assign the pointer another value, you don't
have access to your memory, which you wanted to take care for yourself. Thus you
have a block of memory reserved, which is not useful, which is called a memory leak.

*/
>
> Can somebody tell me what I'm doing wrong?
> If I try this:
>
> int type = randrange(1,1);
> int *blok = new int;
> if(type==1)
> {
> int blok1[16] = {0, 0, 0, 0,0, 1, 1, 0, 0, 1, 1, 0, 0, 0, 0, 0 };
> blok = blok1;
> }
> if(type==2)
> {
> int blok1[16] = {0, 0, 0, 0,0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0 };
> blok = blok1;
> }
>
>


Here is what I'd do:

int type = randrange(1,1);
int *blok = new int[16]

//now you have to assigen it the values:

switch (type){

case 1:
int i
for (i =0; i< 16 ; i++) blok[i] = 0;
blok[5] = 1;
blok[6] = 1;
blok[9] = 1;
blok[10] = 1;
break;

[...]
case2:
int i
for (i =0; i< 16 ; i++) blok[i] = 0;
blok[6] = 1;
blok[7] = 1;
blok[8] = 1;
blok[9] = 1;
break;

}

do_what_ever_you_want_with_blok();
[...]
delete[] blok;
 
Reply With Quote
 
Boris Glawe
Guest
Posts: n/a
 
      08-27-2004
>
> I get the error "jump to case label" when I try to compile some code with
> this in:
>
>
> int type = randrange(1,1);
> int *blok = new int;


/*why do you call "new int". Further down you say "blok = blok1", which means,
that you don't have a pointer to the memory area where your int value has been
stored anymore. This is called a memory leak: You reserved memory with "new" but
you cannot access or release this memory anymore. Calling this one in a loop
will result in extremely high memory usage for nor reason.
Just say

int*blok; //nothing more

*/

> switch(type)
> {
> case 1:
> int blok[16] = {0, 0, 0, 0,0, 1, 1, 0, 0, 1, 1, 0, 0, 0, 0,
> 0 };


// you can ommit the array's size, if you initialize it explicitly (since the
compiler can gues the size from the number of elements)

> blok = blok1;


// blok1 has never been defined or declared before.

> break;
> case '2':
> int blok1[16] = {0, 0, 0, 0,0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 0,
> 0 };
> blok = blok1;
> break;
> }



/*
You defined a pointer above called "blok" Then you're trying to point the
pointer to an array, which is alive within the switch statement only. If the
switch statement is done, the array's memory is released again (which means,
that it doesn't belong to your program anymore), but your pointer is still
pointing at this memory area. If you try to access the data, the pointer is
pointing to (what is called dereferencing a pointer), you try to access
data/memory, which does not belong to your program anymore. The operating system
will kick you out of the race with a segmentation fault of memory access
violation or whatever it's called.

A solution would be to create the array with new:

int *blok = new int[16]

//now you have to assigen it the values:

switch (type){

case 1:
int i
for (i =0; i< 16 ; i++) blok[i] = 0;
blok[5] = 1;
blok[6] = 1;
blok[9] = 1;
blok[10] = 1;
break;

[...]
case2:
int i
for (i =0; i< 16 ; i++) blok[i] = 0;
blok[6] = 1;
blok[7] = 1;
blok[8] = 1;
blok[9] = 1;
break;

}

do_what_ever_you_want_with_blok();
[...]
delete[] blok;



now you can continue with whatever you're planing to do.

In the end, when you don't need blok anymore call this:

delete[] blok

which releases the memory, you've reserved within the swich-statement.

(keep in mind there is a difference between delete and delete[]. The first one
deletes the memory the pointer is pointing to, the second one deletes an array,
the pointer is pointing to.)

The technical difference between the your version and my version is, that in
your code, the memory for the array is allocated and released automatically, in
mine the memory is handled manually (which is not that complicated, if you dont
forget to call delete[])

You say to the compiler "I need a block of memory where 16 integers fit in and
please assign my values to the memory. Delete the memory again, if I'm leaving
the switch statement".

What I do is
"give me a block of memory where 16 integers fit in. Save the start of this
block in my pointer (here it's blok). I'll care myself about the fate of the memory"

This is necessary, since you don't want your array to be deleted after having
left the switch statement. If you assign the pointer another value, you don't
have access anymore to your memory, which you wanted to take care for yourself.
Thus you have a block of memory reserved, which is not useful, which is called a
memory leak.

*/

 
Reply With Quote
 
Boris Glawe
Guest
Posts: n/a
 
      08-27-2004

> [...]
> case2:


it must of course be

case 2:

and not

case2:

> [...]

 
Reply With Quote
 
Boris Glawe
Guest
Posts: n/a
 
      08-27-2004

> If you try to access
> the data, the pointer is pointing to (what is called dereferencing a
> pointer), you try to access data/memory, which does not belong to your
> program anymore. The operating system will kick you out of the race with
> a segmentation fault of memory access violation or whatever it's called.
>


It's not necessary, that your OS will kick you out immediately, because it's
possible and very likely, that the array's memory is still in possession of your
program. But it has a different purpose then storing the array's data. accessing
this data via the pointer results in undefined behaviour. Such mistakes causes
security wholes, since one can use the pointer to access memory, which is not
intended to be accessed. Here an example which a spent hous for, when I began
with C-Programming

int arr[100];

int i;
for (i =0 ; i<= 100;i++){
arr[i] = 0;
}

This resulted in an infinite loop (in my case). Why ? The loop runs 101 times
and thus leads to a buffer overflow. I my case the compiler placed the variable
i next to the array in memory. The last iteration is

arr[100] = 0;

The last integer in arr is placed in arr[99] and the variable i has been placed
next to arr[99], which is arr[100]. i is thus set to 0 again and could never
become greater then 100.

This is example is unfortunately not reproducible, since the placement of i in
memory is chosen randomly by the compiler. It's very unlikely that it will be
placed next to the array in this case...

These are the pitfalls with C and C++. But the performance is worht it
 
Reply With Quote
 
Thomas Matthews
Guest
Posts: n/a
 
      08-27-2004
cousaert wrote:
> Newsgroups: alt.comp.lang.learn.c-c++
> Date: Fri, 27 Aug 2004 12:36:11 +0200
> Lines: 36
> User-Agent: KNode/0.7.6
> MIME-Version: 1.0
> Content-Type: text/plain; charset=us-ascii
> Content-Transfer-Encoding: 7Bit
>
> I get the error "jump to case label" when I try to compile some code with
> this in:
>
>
> int type = randrange(1,1);
> int *blok = new int;

Note that the above statement actually allocates one integer in
dynamic memory and sets the variable "blok" to point to it. This will
be referenced below.


> switch(type)
> {
> case 1:
> int blok[16] = {0, 0, 0, 0,0, 1, 1, 0, 0, 1, 1, 0, 0, 0, 0,
> 0 };

1. I presume you wanted the name as "blok1".

2. You are declaring a variable that is local to the switch statment.
When execution leaves the switch statement, the variable will cease
to exist.

3. However, if you declare the variable as "static", the variable will
live until the program ends.

4. Did you want to declare this variable as const?

5. Prefer named constants to magic numbers for array sizes:
const unsigned int BLOCK_SIZE = 16;
int blok1[BLOCK_SIZE] = //...



> blok = blok1;

Here is where the trouble manifests itself.
1. Because "blok" already points to an integer in dynamic memory
(see above), it is now lost. There is no method to reclaim the
single integer (except for maybe terminating your program).

2. If you meant to copy the data pointed to by blok1 to the area
pointed to by blok, there is trouble. You only allocated one
integer, while blok1 has 16. Also, arrays cannot be copied
by using the assignment operator. See std::copy.

3. If you meant to set the pointer blok to the location of blok1,
the data will disappear because blok1 was declared with local
scope. The location that blok points to may not be available
any more. Dereferencing the blok pointer will result in
undefined behavior (anything can happen).


> break;
> case '2':

This should be numeric 2 rather than character 2.

> int blok1[16] = {0, 0, 0, 0,0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 0,
> 0 };
> blok = blok1;

See above discussion.

> break;
> }
>
> Can somebody tell me what I'm doing wrong?
> If I try this:
>
> int type = randrange(1,1);
> int *blok = new int;
> if(type==1)
> {
> int blok1[16] = {0, 0, 0, 0,0, 1, 1, 0, 0, 1, 1, 0, 0, 0, 0, 0 };
> blok = blok1;
> }
> if(type==2)
> {
> int blok1[16] = {0, 0, 0, 0,0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0 };
> blok = blok1;
> }

The problem of local scope has bitten here too.

I believe this is what you want:
int * GetBlockData(void)
{
static const unsigned int BLOCK_SIZE = 16;
static int blok1[BLOCK_SIZE] = {0, 0, 0, 0, 0, 1, 1, 0,
0, 1, 1, 0, 0, 0, 0, 0);
static int blok2[BLOCK_SIZE] = {0, 0, 0, 0, 0, 0, 1, 1,
1, 1, 0, 0, 0, 0, 0, 0};
int * blok;

switch (randrange(1,1))
{
case 1:
blok = blok1;
break;
case 2:
blok = blok2;
break;
default:
blok = NULL;
}
return blok;
}


--
Thomas Matthews

C++ newsgroup welcome message:
http://www.slack.net/~shiva/welcome.txt
C++ Faq: http://www.parashift.com/c++-faq-lite
C Faq: http://www.eskimo.com/~scs/c-faq/top.html
alt.comp.lang.learn.c-c++ faq:
http://www.comeaucomputing.com/learn/faq/
Other sites:
http://www.josuttis.com -- C++ STL Library book

 
Reply With Quote
 
Rolf Magnus
Guest
Posts: n/a
 
      08-27-2004
cousaert wrote:

> I get the error "jump to case label" when I try to compile some code
> with this in:


I guess you just forgot to read the rest of the error message, which
probably says something like ..."crosses initialization of `int
blok[16]'". Adding curly braces should help.

> int type = randrange(1,1);
> int *blok = new int;
> switch(type)
> {
> case 1:


{
> int blok[16] = {0, 0, 0, 0,0, 1, 1, 0, 0, 1, 1, 0, 0,
> 0, 0,
> 0 };
> blok = blok1;
> break;

}

> case '2':


{
> int blok1[16] = {0, 0, 0, 0,0, 0, 1, 1, 1, 1, 0, 0, 0,
> 0, 0,
> 0 };
> blok = blok1;
> break;

}
> }


 
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 On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
switch case to a jump table sam_cit@yahoo.co.in C Programming 9 05-10-2007 05:03 PM
Double left click jump to top, same with ctrl or shift to jump to bottom? Eadwine Rose Javascript 2 10-15-2006 08:38 PM
<label><div></div></label> allowed? Josef K. ASP .Net 3 05-22-2005 02:13 PM
label versus asp:label Question. Thanks. Shapper ASP .Net 2 05-07-2005 05:55 AM
error: jump to case label Neil Zanella C++ 6 04-04-2004 02:47 PM



Advertisments