Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Reference to a variable that goes out of scope

Reference to a variable that goes out of scope

Scheduled Pinned Locked Moved C / C++ / MFC
question
5 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • E Offline
    E Offline
    elelont2
    wrote on last edited by
    #1

    int *pi;
    {
    int i = 3;
    pi = &i;
    }
    std::cout << *pi << std::endl;

    I figure that the *pi value in std:cout can not be trusted since i goes out of scope and the address of i can be used for other stuff. Is that correct? Thank

    A L 2 Replies Last reply
    0
    • E elelont2

      int *pi;
      {
      int i = 3;
      pi = &i;
      }
      std::cout << *pi << std::endl;

      I figure that the *pi value in std:cout can not be trusted since i goes out of scope and the address of i can be used for other stuff. Is that correct? Thank

      A Offline
      A Offline
      Aescleal
      wrote on last edited by
      #2

      Yep, that's right. It's also why you should never return references to local variables - what they're based on disappears at the end of the block they're declared in. Cheers, Ash

      1 Reply Last reply
      0
      • E elelont2

        int *pi;
        {
        int i = 3;
        pi = &i;
        }
        std::cout << *pi << std::endl;

        I figure that the *pi value in std:cout can not be trusted since i goes out of scope and the address of i can be used for other stuff. Is that correct? Thank

        L Offline
        L Offline
        Lost User
        wrote on last edited by
        #3

        It depends on stack usage. If you return a local var thats now out of scope to a func that eats the stack it will get munged. If not, it is still there, but its is risky of course. Actually we found a windows bug that did just this. It returned a local var. This was OK till we hooked the func, and thus increased its tack usage. In your case i is out of scope at code level, but is still on the stack since you havent returned from your func yet (your code is actually compiled with i on the stack just after pi). --edit-- I see no one has responded. I guess an understanding of assembler (ie, the way code actually compiles) is totally foreign territory to todays 3G coders.

        ============================== Nothing to say.

        S 1 Reply Last reply
        0
        • L Lost User

          It depends on stack usage. If you return a local var thats now out of scope to a func that eats the stack it will get munged. If not, it is still there, but its is risky of course. Actually we found a windows bug that did just this. It returned a local var. This was OK till we hooked the func, and thus increased its tack usage. In your case i is out of scope at code level, but is still on the stack since you havent returned from your func yet (your code is actually compiled with i on the stack just after pi). --edit-- I see no one has responded. I guess an understanding of assembler (ie, the way code actually compiles) is totally foreign territory to todays 3G coders.

          ============================== Nothing to say.

          S Offline
          S Offline
          Stefan_Lang
          wrote on last edited by
          #4

          Erudite_Eric wrote:

          It depends on stack usage.

          AFAIK it doesn't so much depend on stack, but on the runtime libraries used: if they choose to invalidate a memory address as soon as it goes out of scope, then code like this will crash with an access violation. It may depend on compiler settings, but I've never seen code like that actually work on Windows XP or 7. Then again I never deliberately tried ;)

          L 1 Reply Last reply
          0
          • S Stefan_Lang

            Erudite_Eric wrote:

            It depends on stack usage.

            AFAIK it doesn't so much depend on stack, but on the runtime libraries used: if they choose to invalidate a memory address as soon as it goes out of scope, then code like this will crash with an access violation. It may depend on compiler settings, but I've never seen code like that actually work on Windows XP or 7. Then again I never deliberately tried ;)

            L Offline
            L Offline
            Lost User
            wrote on last edited by
            #5

            No, it isnt to do with libraries, but the very way local variables are allocated, ie, on the stack. This is only unwound, upwards dont forget, when the func the variables are declared in returns. Thats when the memory can be reused, by calling another func that declares its own variables. So if you look at assembler you will see stuff like ebp+4 being set to a value. This is a local variable. At the end you see a ret 4. This returns from the func and winds the stack back by the same number as the size of the local variables declared.

            ============================== Nothing to say.

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups