scope

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Allan Bruce

    scope

    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 *variablePointe r;
    int count = 32;
    variablePointer = &count;
    cout << *variablePointe r;
    }

    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 *variablePointe r;
    {
    int count = 32;
    variablePointer = &count;
    }
    cout << *variablePointe r;
    }

    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

  • Corey Murtagh

    #2
    Re: scope

    Allan Bruce wrote:
    <snip>[color=blue]
    >
    > 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 *variablePointe r;
    > {
    > int count = 32;
    > variablePointer = &count;
    > }
    > cout << *variablePointe r;
    > }[/color]

    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(si zeof 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.
    [color=blue]
    > 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?[/color]

    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!"

    Comment

    • Josephine Schafer

      #3
      Re: scope


      "Allan Bruce" <notabruce@notc sd.abdn.ac.uk> wrote in message
      news:3EFAC09F.5 060703@notcsd.a bdn.ac.uk...[color=blue]
      > 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 *variablePointe r;
      > int count = 32;
      > variablePointer = &count;
      > cout << *variablePointe r;
      > }
      >
      > This I understand as it is very similar in c. My belief is that
      > variablePointer is a pointer to an int.[/color]
      Correct
      [color=blue]
      >count is declared as an int and
      > initialised to 32.[/color]
      Correct
      [color=blue]
      >variablePointe r is then set to point to count, and
      > therefore has value 32 when cout is called.[/color]
      VariablePointer does NOT hold 32..it holds the address of count which holds
      32.
      [color=blue]
      > My problem is then in the following example which is supposedly wrong:
      > {
      > int *variablePointe r;
      > {
      > int count = 32;
      > variablePointer = &count;
      > }
      > cout << *variablePointe r;
      > }
      >
      > 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?[/color]
      Yes..variablePo inter 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.
      [color=blue]
      > Thanks
      > Allan
      >[/color]


      Comment

      Working...