[C++ switch statements] variable scope question

Nyap

HTML Noob
OP
Banned
Joined
Jan 13, 2016
Messages
971
Trophies
0
Age
55
Location
That Chaos Site
XP
483
Country
My tutorial said this:
Code:
switch (x)
{
case 1:
int y;// okay, declaration is allowed
y=4;// okay, this is an assignment
break;

case 2:
y=5;// okay, y was declared above, so we can use it here too
break;

case 3:
int z=4;// illegal, you can't initialize new variables in the case statements
break;

default:
std::cout<<"default case"<<std::endl;
break;
}
Note that although variable y was defined in case 1, it was used in case 2 as well. All cases are considered part of the same scope, so a declaration in one case can be used in subsequent cases.
If "case" executes the statements below it until something like break; stops it, why does case 2 work? if case 2 was executed instead of case 1, then int y; would have never been executed/declared, and therefore it should cause a compile error, right?
 
Last edited by Nyap,

Cjuub

Well-Known Member
Member
Joined
May 25, 2006
Messages
198
Trophies
1
Age
33
XP
2,056
Country
"int y;" is not something that gets executed, it's used by the compiler during compile time to reserve space for y whether case 1 gets executed or not. Since it's declared before all uses of it, and in the same scope, it's perfectly legal.
 

Nyap

HTML Noob
OP
Banned
Joined
Jan 13, 2016
Messages
971
Trophies
0
Age
55
Location
That Chaos Site
XP
483
Country
so scope is just a high level thing, that prevents you from compiling if the decleration isn't above where it's used? the execution path doesn't matter? im confused

--------------------- MERGED ---------------------------

let me explain what I'm thinking right now

scope isn't a thing during runtime, and is just there to prevent confusion (so that people can't use a variable that's been declared somewhere completely different)
duration is what really matters
 
Last edited by Nyap,

Cjuub

Well-Known Member
Member
Joined
May 25, 2006
Messages
198
Trophies
1
Age
33
XP
2,056
Country
Yes, scope is only checked during compile time to make sure the generated low level code is correct. Scopes do not exist in the actual machine code, it's just rules set up by the language to keep things structured.
 

Coto

-
Member
Joined
Jun 4, 2010
Messages
2,979
Trophies
2
XP
2,565
Country
Chile
if you add {} between each case all variables reserved (defined) are pushed to stack, and destroyed when abandoning such scope. So you add sub-scopes

Also in low level assembly, for example ARM generates relative addresses + PC for variables such static definitions(your scope has control over it, not you), or at least it will try, (LDR r0,=0xc070c070) otherwise doing a weird combination of the barrel shifter you can create inmediate values, this for creating compiler optimized code.

Code:
mvn pc,#-134217725       @(0xf8000003~0xffffffff) equals  ldr pc,=0x7ffffffc  @just a single #inmediate instruction!
 
Last edited by Coto,

Nyap

HTML Noob
OP
Banned
Joined
Jan 13, 2016
Messages
971
Trophies
0
Age
55
Location
That Chaos Site
XP
483
Country
if you add {} between each case all variables reserved (defined) are pushed to stack, and destroyed when abandoning such scope. So you add sub-scopes
but isn't that called duration? according to my tutorial:
Local variables have automatic duration, which means they are created when the block they are part of is entered, and destroyed when the block they are part of is exited
Global variables have static duration, which means they are created when the program starts and are destroyed when it ends
 

Nyap

HTML Noob
OP
Banned
Joined
Jan 13, 2016
Messages
971
Trophies
0
Age
55
Location
That Chaos Site
XP
483
Country
D:

--------------------- MERGED ---------------------------

How do you close a thread? I think I'll just close this before I get confused even more

--------------------- MERGED ---------------------------

meh I'll just unwatch the thread
 

Site & Scene News

Popular threads in this forum

General chit-chat
Help Users
    SylverReZ @ SylverReZ: :rofl2: :rofl2: