scope

Discussion in 'C++' started by Allan Bruce, Jun 26, 2003.

  1. Allan Bruce

    Allan Bruce Guest

    Hi there,

    I mainly use c for programming but have decided to try and learn c++.
    I found a text which delcares the following

    {
    int *variablePointer;
    int count = 32;
    variablePointer = &count;
    cout << *variablePointer;
    }

    This I understand as it is very similar in c. My belief is that
    variablePointer is a pointer to an int. count is declared as an int and
    initialised to 32. variablePointer is then set to point to count, and
    therefore has value 32 when cout is called.
    My problem is then in the following example which is supposedly wrong:
    {
    int *variablePointer;
    {
    int count = 32;
    variablePointer = &count;
    }
    cout << *variablePointer;
    }

    I just want to clarigy if I understand why this is wrong.
    variablePointer again is a pointer to an int and has scope within the
    whole of the above function. We then enter a new scope sub-level, and
    count is decalred as an int and initialised to the value of 32.
    variablePointer is set to point to count. We then leave this scope
    level, and when we try to use cout for variablePointer, we are causing
    undefined behaviour. Is this a correct assumption?
    Thanks
    Allan
    Allan Bruce, Jun 26, 2003
    #1
    1. Advertising

  2. Allan Bruce wrote:
    <snip>
    >
    > This I understand as it is very similar in c. My belief is that
    > variablePointer is a pointer to an int. count is declared as an int and
    > initialised to 32. variablePointer is then set to point to count, and
    > therefore has value 32 when cout is called.
    > My problem is then in the following example which is supposedly wrong:
    > {
    > int *variablePointer;
    > {
    > int count = 32;
    > variablePointer = &count;
    > }
    > cout << *variablePointer;
    > }


    Not supposedly - it *is* wrong.

    The variable 'count' will go out of scope after you take a pointer to
    it, and before you dereference that pointer. The closest analogy I can
    think of in C-like code is:

    int* varptr = (int*)malloc(sizeof int);
    *varptr = 32;
    free(varptr);
    printf("%d", *varptr);

    Since the memory is no longer allocated to an object, the system is free
    to use it for anything... and your pointer is no longer valid. The
    chances are that you'll find that it has been written to before you get
    to dereferencing the pointer. And if you (or the compiled code) do
    anything else between count going out of scope and dereferencing the
    pointer, then the probability of the value remaining unmodified
    approaches zero.

    In the specific example you quoted, depending on what's going on behind
    the scenes, I'd say you have a fairly good chance that the stack space
    you're pointing to still contains the value count had before it went out
    of scope. However, relying on this to be true is a fundamentally
    *wrong* practice that *will* cause you problems in future.

    > I just want to clarigy if I understand why this is wrong.
    > variablePointer again is a pointer to an int and has scope within the
    > whole of the above function. We then enter a new scope sub-level, and
    > count is decalred as an int and initialised to the value of 32.
    > variablePointer is set to point to count. We then leave this scope
    > level, and when we try to use cout for variablePointer, we are causing
    > undefined behaviour. Is this a correct assumption?


    Err.. see above :)

    (I must remember to re-read to the bottom before responding... *sigh*)

    --
    Corey Murtagh
    The Electric Monk
    "Quidquid latine dictum sit, altum viditur!"
    Corey Murtagh, Jun 26, 2003
    #2
    1. Advertising

  3. "Allan Bruce" <> wrote in message
    news:...
    > Hi there,
    >
    > I mainly use c for programming but have decided to try and learn c++.
    > I found a text which delcares the following
    >
    > {
    > int *variablePointer;
    > int count = 32;
    > variablePointer = &count;
    > cout << *variablePointer;
    > }
    >
    > This I understand as it is very similar in c. My belief is that
    > variablePointer is a pointer to an int.

    Correct

    >count is declared as an int and
    > initialised to 32.

    Correct

    >variablePointer is then set to point to count, and
    > therefore has value 32 when cout is called.

    VariablePointer does NOT hold 32..it holds the address of count which holds
    32.

    > My problem is then in the following example which is supposedly wrong:
    > {
    > int *variablePointer;
    > {
    > int count = 32;
    > variablePointer = &count;
    > }
    > cout << *variablePointer;
    > }
    >
    > I just want to clarigy if I understand why this is wrong.
    > variablePointer again is a pointer to an int and has scope within the
    > whole of the above function. We then enter a new scope sub-level, and
    > count is decalred as an int and initialised to the value of 32.
    > variablePointer is set to point to count. We then leave this scope
    > level, and when we try to use cout for variablePointer, we are causing
    > undefined behaviour. Is this a correct assumption?

    Yes..variablePointer was pointing to count which gets destroyed when it's
    enclosing block terminates.
    Thus variablePointer still holds an address that system is free to use at
    it's will and should not be accessed.

    > Thanks
    > Allan
    >
    Josephine Schafer, Jun 26, 2003
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Paul Opal
    Replies:
    12
    Views:
    887
    Paul Opal
    Oct 11, 2004
  2. ann
    Replies:
    13
    Views:
    642
    Patricia Shanahan
    Sep 13, 2005
  3. Steven T. Hatton
    Replies:
    9
    Views:
    448
  4. Xah Lee
    Replies:
    0
    Views:
    2,215
    Xah Lee
    Feb 26, 2009
  5. Replies:
    0
    Views:
    152
Loading...

Share This Page