[C++ switch statements] variable scope question

Nyap

HTML Noob
OP
Banned
Joined
Jan 13, 2016
Messages
971
Trophies
0
Age
54
Location
That Chaos Site
XP
482
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
32
XP
1,849
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
54
Location
That Chaos Site
XP
482
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
32
XP
1,849
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,934
Trophies
2
XP
2,409
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
54
Location
That Chaos Site
XP
482
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
54
Location
That Chaos Site
XP
482
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
 
General chit-chat
Help Users
  • No one is chatting at the moment.
  • JuanMena @ JuanMena:
    Kissing random dudes choking in celery? Really? Need to study for that?
  • K3N1 @ K3N1:
    Yes it requires a degree
  • K3N1 @ K3N1:
    I could also yank out the rest of my teeth but theirs professionals for that
  • x65943 @ x65943:
    If your throat closes, putting oxygen in your mouth will not solve anything - as you will be introducing oxygen prior to the area of obstruction
  • JuanMena @ JuanMena:
    Just kiss me Kyle.
  • x65943 @ x65943:
    You either need to be intubated to bypass obstruction or create a stoma inferior to the the area of obstruction to survive
  • x65943 @ x65943:
    "Just kiss me Kyle." And I thought all the godreborn gay stuff was a smear campaign
  • JuanMena @ JuanMena:
    If I die, tell my momma I won't be carrying Baby Jesus this christmas :sad::cry:
  • K3N1 @ K3N1:
    Smear campaigns are in The political section now?
  • JuanMena @ JuanMena:
    Chary! Chary! Chary, Chary, Chary!
  • Sonic Angel Knight @ Sonic Angel Knight:
    Pork Provolone :P
  • Psionic Roshambo @ Psionic Roshambo:
    Sounds yummy
  • K3N1 @ K3N1:
    Sweet found my Wii u PSU right after I ordered a new one :tpi:
  • JuanMena @ JuanMena:
    It was waiting for you to order another one.
    Seems like, your PSU was waiting for a partner.
  • JuanMena @ JuanMena:
    Keep them both
    separated or you'll have more PSUs each year.
  • K3N1 @ K3N1:
    Well one you insert one PSU into the other one you get power
  • JuanMena @ JuanMena:
    It literally turns it on.
  • K3N1 @ K3N1:
    Yeah power supplies are filthy perverts
  • K3N1 @ K3N1:
    @Psionic Roshambo has a new friend
    +1
  • JuanMena @ JuanMena:
    It's Kyle, the guy that went to school to be a Certified man Kisser.
  • Psionic Roshambo @ Psionic Roshambo:
    Cartmans hand has taco flavored kisses
  • A @ abraarukuk:
    hi guys
  • Iron_Masuku @ Iron_Masuku:
    Hello
    Skelletonike @ Skelletonike: hmm