error: jump to case label

Discussion in 'C++' started by cousaert, Aug 27, 2004.

  1. cousaert

    cousaert Guest

    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;
    }
     
    cousaert, Aug 27, 2004
    #1
    1. Advertising

  2. cousaert

    cousaert Guest

    cousaert wrote:

    Oh yeah, I'm working with gcc3.3.2 on Mandrake 10.0
     
    cousaert, Aug 27, 2004
    #2
    1. Advertising

  3. "cousaert" <> wrote in message
    news:9oEXc.1441$-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
     
    Marcelo Slomp, Aug 27, 2004
    #3
  4. cousaert

    Conrad Weyns Guest

    "cousaert" <> wrote in message
    news:9oEXc.1441$-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;
    > }
    >
    >
    >
     
    Conrad Weyns, Aug 27, 2004
    #4
  5. cousaert

    Boris Glawe Guest

    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 = 0;
    blok[5] = 1;
    blok[6] = 1;
    blok[9] = 1;
    blok[10] = 1;
    break;

    [...]
    case2:
    int i
    for (i =0; i< 16 ; i++) blok = 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 = 0;
    blok[5] = 1;
    blok[6] = 1;
    blok[9] = 1;
    blok[10] = 1;
    break;

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

    }

    do_what_ever_you_want_with_blok();
    [...]
    delete[] blok;
     
    Boris Glawe, Aug 27, 2004
    #5
  6. cousaert

    Boris Glawe Guest

    >
    > 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 = 0;
    blok[5] = 1;
    blok[6] = 1;
    blok[9] = 1;
    blok[10] = 1;
    break;

    [...]
    case2:
    int i
    for (i =0; i< 16 ; i++) blok = 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.

    */
     
    Boris Glawe, Aug 27, 2004
    #6
  7. cousaert

    Boris Glawe Guest


    > [...]
    > case2:


    it must of course be

    case 2:

    and not

    case2:

    > [...]
     
    Boris Glawe, Aug 27, 2004
    #7
  8. cousaert

    Boris Glawe Guest


    > 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 = 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 :)
     
    Boris Glawe, Aug 27, 2004
    #8
  9. 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
     
    Thomas Matthews, Aug 27, 2004
    #9
  10. cousaert

    Rolf Magnus Guest

    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;

    }
    > }
     
    Rolf Magnus, Aug 27, 2004
    #10
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Neil Zanella

    error: jump to case label

    Neil Zanella, Apr 3, 2004, in forum: C++
    Replies:
    6
    Views:
    43,077
    Buster
    Apr 4, 2004
  2. switch case to a jump table

    , May 7, 2007, in forum: C Programming
    Replies:
    9
    Views:
    751
    ┬Ča\\/b
    May 10, 2007
  3. Eadwine Rose
    Replies:
    2
    Views:
    231
    Eadwine Rose
    Oct 15, 2006
  4. Knut Krueger
    Replies:
    8
    Views:
    340
    Knut Krueger
    Aug 20, 2013
  5. Replies:
    31
    Views:
    263
    Tim Rentsch
    Apr 15, 2014
Loading...

Share This Page