[C++ switch statements] variable scope question

Discussion in 'Computer Programming, Emulation, and Game Modding' started by Nyap, May 19, 2016.

  1. Nyap
    OP

    Nyap HTML Noob

    Banned
    973
    344
    Jan 13, 2016
    That Chaos Site
    My tutorial said this:
    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, May 19, 2016
  2. Cjuub

    Cjuub GBAtemp Regular

    Member
    196
    139
    May 25, 2006
    "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.
     
  3. sarkwalvein

    sarkwalvein Professional asshole at GBATemp

    Member
    GBAtemp Patron
    sarkwalvein is a Patron of GBAtemp and is helping us stay independent!

    Our Patreon
    4,872
    4,931
    Jun 29, 2007
    Germany
    Niedersachsen
    Declarations are never executed. They are just declared variables with easy memory allocation living only inside a given scope.
     
  4. Nyap
    OP

    Nyap HTML Noob

    Banned
    973
    344
    Jan 13, 2016
    That Chaos Site
    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

    — Posts automatically merged - Please don't double post! —

    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, May 19, 2016
  5. Cjuub

    Cjuub GBAtemp Regular

    Member
    196
    139
    May 25, 2006
    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.
     
  6. Nyap
    OP

    Nyap HTML Noob

    Banned
    973
    344
    Jan 13, 2016
    That Chaos Site
    ahhhhh ok
     
  7. Coto

    Coto GBAtemp Addict

    Member
    2,353
    403
    Jun 4, 2010
    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, May 19, 2016
  8. Nyap
    OP

    Nyap HTML Noob

    Banned
    973
    344
    Jan 13, 2016
    That Chaos Site
    but isn't that called duration? according to my tutorial:
     
  9. Coto

    Coto GBAtemp Addict

    Member
    2,353
    403
    Jun 4, 2010
    Chile
    no its scope.
     
  10. Nyap
    OP

    Nyap HTML Noob

    Banned
    973
    344
    Jan 13, 2016
    That Chaos Site
    D:

    — Posts automatically merged - Please don't double post! —

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

    — Posts automatically merged - Please don't double post! —

    meh I'll just unwatch the thread