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