473,394 Members | 1,766 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

switch case with vector problem

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.

Mar 30 '06 #1
6 5566
easy wrote:
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
--
Please remove capital As from my address when replying by mail
Mar 30 '06 #2
Victor Bazarov wrote:
easy wrote:
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.
Add:
{

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


Add:
}


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
Mar 30 '06 #3
easy wrote:
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);
break;


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.

Mar 30 '06 #4
Default User wrote:
Victor Bazarov wrote:
easy wrote:
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.

Add:
{

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


Add:
}


I think he understood the "what" of the problem, just not the "why".
[..]


You're right. My bad.

V
--
Please remove capital As from my address when replying by mail
Mar 31 '06 #5
You cannot use a goto statement to jump over statements that
declare variables involving implicit or explicit initialization.
Scenario 1:
{
Mar 31 '06 #6
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.

Apr 2 '06 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Gabriel Schreiber | last post by:
Hy I need some help on this problem: I want to send some output to a number of streams. So I need an object, that behaves (or is) like an std::ostream. (additional wish: the object is an...
10
by: Chih-Hsu Yen | last post by:
I encountered a strange problem about switch-case statement. switch(cmd) { case 1: statements; break; case 2: statements; break; ... .... case 11: S1; S2; S3; statements;
3
by: pgraeve | last post by:
I am a convert from VB to C# so bear with me on this "conversion" question C# switch statement seems to be the closest relative to VB's Select Case. I used VB's Select Case statement liberally. ...
13
by: Fei Liu | last post by:
Hi Group, I've got a problem I couldn't find a good solution. I am working with scientific data files in netCDF format. One of the properties of netCDF data is that the actual type of data is only...
14
by: markww | last post by:
Hi, I want to use the vector container class to store pixel data. Currently I have some memory allocated using c++'s new operator. I allocate the memory differently based on if the pixel type is...
11
by: Smithers | last post by:
Just wondering if the sequence of the case(s) in the switch block might impact performance. Suppose I switch on an int, and I have 4 expected cases. Lets say I expect case 3 to happen 95% of the...
2
by: huzzaa | last post by:
I am trying to get the user to input 1-10 and that will select a a number from an array and place it into a variable. here is the code. do { cout << "Enter the first row...
13
by: xz | last post by:
What if I want the following: vector<intv; // v is loaded by push_back() switch( v.size() ) { case 2: //do something
7
by: Rohit | last post by:
Hi, I am working on a switch module which after reading voltage through a port pin and caterogizing it into three ranges(open,low or high), passes this range to a function switch_status() with...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.