switch case with vector problem

E

easy

The following code gives me compiler errors galore telling me that I'm
crossing the initalization of the vectors and that case label 2 and
default are "within scope of cleanup or variable array". I suspect its
the destructors for the vectors that is causing the problem but why are
they being called at all? Shouldn't they go out of scope at the
closing bracket after default?

Putting brackets around the contents of case1 solves my problem but I
still dont understand why I need to do so.


void Test1(int value)
{
switch(value)
{
case 1:
std::cout << "Testing Scanner DAC" << std::endl;
std::vector<uint32_t> test;
std::vector<uint32_t> result;
break;
case 2:
break;
default:
break;
}
}

Thank you for your time.
 
V

Victor Bazarov

easy said:
The following code gives me compiler errors galore telling me that I'm
crossing the initalization of the vectors and that case label 2 and
default are "within scope of cleanup or variable array". I suspect
its the destructors for the vectors that is causing the problem but
why are they being called at all? Shouldn't they go out of scope at
the closing bracket after default?

Putting brackets around the contents of case1 solves my problem but I
still dont understand why I need to do so.


void Test1(int value)
{
switch(value)
{
case 1:

Add:
{

std::cout << "Testing Scanner DAC" << std::endl;
std::vector<uint32_t> test;
std::vector<uint32_t> result;

Add:
}
break;
case 2:
break;
default:
break;
}
}

Thank you for your time.

V
 
D

Default User

Victor said:

I think he understood the "what" of the problem, just not the "why".
The Standard addresses this:

It is possible to transfer into a block, but not in a way that bypasses
declarations with initialization. A program that jumps77) from a point
where a local variable with automatic storage duration is not in scope
to a point where it is in scope is ill-formed unless the variable has
POD type (3.9) and is declared without an initializer (8.5).
__________________
77) The transfer from the condition of a switch statement to a case
label is considered a jump in this respect.



So, if I understand it right, jumping to case label 2 bypasses the
initialization of "test" and "result" but those variables are still in
scope. By placing them in braces, they have a new scope and the
initializations don't matter.

Case labels are just jump points, they don't create scopes.

If "test" and "result" were POD and uninitialized, then it wouldn't
matter and you could have left out the braces.




Brian
 
O

Old Wolf

easy said:
The following code gives me compiler errors galore
I still dont understand why


switch(value)
{
case 1:
std::cout << "Testing Scanner DAC" << std::endl;
std::vector<uint32_t> test;
std::vector<uint32_t> result;
break;
case 2:

Imagine you had at this point:
test.push_back(1);

Then you execute the switch with value==2. You are then
operating on a vector that hasn't been constructed yet,
which is obviously a disaster.
 
A

ansumanm

You cannot use a goto statement to jump over statements that
declare variables involving implicit or explicit initialization.
Scenario 1:
{
.
.
.
goto done;

int ran = std::rand();
std::cout << ran;

done:
return 0;
}

This code doen not compile because the goto statement jumps over the
initialization of the ran variable.The theory is that a variable that
needs initialization must be guaranteed to have undergone that
initialization as long as the variable is in scope.

Scenario 2:
{
.
.
.
goto done;

int ran;
ran = std::rand();
std::cout << ran;

done:
return 0;
}
is valid code because the jump does not bypass initialization.

Scenario 3:

{
.
.
.
goto done;

{
ran = std::rand();
std::cout << ran;
}
done:
return 0;
}
This usage is acceptable because goto jumps over the entire scope
wherein the program declares and initializes the ran variable.
 
E

easy

Thanks all.

It shouldnt have been a problem at all since, as Old Wolf pointed out,
in general it is bad practice to have varibles defined in a switch/case
block. (without creating a new scope for them to live and die within)

I especially appreciated the response from ansumanm.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top