Scope of a variable

D

Deepchand.P

Friends i did this prog in VS 2005.

#include <iostream>
using namespace std;

int* f();

int main()
{
int *k = f();
cout<<"Inside int main() "<<endl<<"k = "<<k<<endl<<"*k =
"<<*k<<endl<<"&k = "<<&k<<endl<<endl;
cout<<"Calling cout<<*f()"<<endl<<*f()<<endl<<endl;
return 0;
}

int* f()
{
int m=100;
cout<<"Inside int* f() "<<endl<<"m = "<<m<<endl<<"&m =
"<<&m<<endl<<endl;
return(&m);
}


y does we still get the value 100 by using *f(); whats actually
happening there. From what i have understood so far the value of a
variable becomes garbage when the variable goes out of scope.

correct me if am wrong and plz explain me the working of this program.
 
R

Robert Bauck Hamar

Friends i did this prog in VS 2005.

#include <iostream>
using namespace std;

int* f();

int main()
{
int *k = f();
cout<<"Inside int main() "<<endl<<"k = "<<k<<endl<<"*k =
"<<*k<<endl<<"&k = "<<&k<<endl<<endl;
cout<<"Calling cout<<*f()"<<endl<<*f()<<endl<<endl;
return 0;
}

int* f()
{
int m=100;
cout<<"Inside int* f() "<<endl<<"m = "<<m<<endl<<"&m =
"<<&m<<endl<<endl;
return(&m);
}


y does we still get the value 100 by using *f();

What y? Or did you mean "Why do we ...". I did misread this the first time.
Please try to use proper English.
whats actually happening there.

Undefined behaviour.
From what i have understood so far the value of a
variable becomes garbage when the variable goes out of scope.

Yes. You are returning a pointer to some object that goes out of scope. That
means you have a dangling pointer, and when you use it, it's undefined
behaviour. It could output what you expect, or it could do something else.
correct me if am wrong and plz explain me the working of this program.

There is none.
 
A

Anarki

What y? Or did you mean "Why do we ...". I did misread this the first time.
Please try to use proper English.


Undefined behaviour.


Yes. You are returning a pointer to some object that goes out of scope. That
means you have a dangling pointer, and when you use it, it's undefined
behaviour. It could output what you expect, or it could do something else.


There is none.

you mean to say *f() will do perform an undefined behaviour?
 
N

Neelesh Bodas

you mean to say *f() will do perform an undefined behaviour?

Returning address of a local variable from a function results in
"undefined program behavior". It simply means that all bets on such a
program are off. In the current context, it implies that you can't
really guarantee what will be printed, if any.


Neelesh
 
M

Michael DOUBEZ

Neelesh Bodas a écrit :
Returning address of a local variable from a function results in
"undefined program behavior". It simply means that all bets on such a
program are off. In the current context, it implies that you can't
really guarantee what will be printed, if any.

It is not the behavior of f() that is undefined but the usage of the
returned pointer (i.e. dangling pointer).

Let's take the following function:
int* f()
{
return reinterpret_cast<int*>(0x42424242);
}

Calling f() is defined behavior (returning a pointer on int at address
0x42424242) but deferencing the returned value is undefined behavior
(this could be argued since it is likely to terminate the program
abruptly :) ).

Michael
 
R

Ron Natalie

Scope by the way applies to names not variables.
The term here is that the lifetime of the object
has ended.
 
J

James Kanze

Neelesh Bodas a écrit :
It is not the behavior of f() that is undefined but the usage of the
returned pointer (i.e. dangling pointer).
Let's take the following function:
int* f()
{
return reinterpret_cast<int*>(0x42424242);
}
Calling f() is defined behavior (returning a pointer on int at address
0x42424242) but deferencing the returned value is undefined behavior
(this could be argued since it is likely to terminate the program
abruptly :) ).

Are you sure of that? I thought that just returning the address
would be undefined behavior, even if the caller ignored it.
It's an invalid address, and just about anything you do with an
invalid address is undefined behavior.
 
M

Michael DOUBEZ

James Kanze a écrit :
Are you sure of that? I thought that just returning the address
would be undefined behavior, even if the caller ignored it.
It's an invalid address, and just about anything you do with an
invalid address is undefined behavior.

Yes, there seems to be an issue here:
3.5.4.2 note 4
The effect of using an invalid pointer value (including passing it to
adeallocation function) is undefined.

Is returning a pointer value actually 'using it' ? I guess not, IMHO, it
is at most byte copy.

Michael
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,774
Messages
2,569,599
Members
45,170
Latest member
Andrew1609
Top