Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C++ (http://www.velocityreviews.com/forums/f39-c.html)
-   -   scope (http://www.velocityreviews.com/forums/t267751-scope.html)

Allan Bruce 06-26-2003 09:45 AM

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 *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


Corey Murtagh 06-26-2003 10:05 AM

Re: scope
 
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!"


Josephine Schafer 06-26-2003 10:13 AM

Re: scope
 

"Allan Bruce" <notabruce@notcsd.abdn.ac.uk> wrote in message
news:3EFAC09F.5060703@notcsd.abdn.ac.uk...
> 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
>





All times are GMT. The time now is 09:50 PM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.