C++ Programming - References - Discussion
Discussion Forum : References - General Questions (Q.No. 12)
12.
Which of the following statements is correct?
- We can return a global variable by reference.
- We cannot return a local variable by reference.
Discussion:
23 comments Page 1 of 3.
Meet said:
9 years ago
Correct option is D:
It is perfectly valid & fine to return a reference to the global variable.
#include <iostream>
int s=9;
int& foo()
{
return s;
}
int main()
{
std::cout<<"value of s = "<<s<<'\n';
foo()=3;
std::cout<<"value of s = "<<s<<'\n';
}
It is syntactically valid to return a reference to a local variable but it doesn't make any sense to return a reference a local variable. Because the local variable is destroyed when execution of function finishes. So the reference to such variable is invalid & results in undefined behavior in C++.
Consider following program:
#include <iostream>
int& foo()
{
int s(9);
return s;
}
int main()
{
std::cout<<foo()<<'\n';
}
Compiler shows only warning when compiling above program.
[Warning] reference to local variable 's' returned [-Wreturn-local-addr]
It is perfectly valid & fine to return a reference to the global variable.
#include <iostream>
int s=9;
int& foo()
{
return s;
}
int main()
{
std::cout<<"value of s = "<<s<<'\n';
foo()=3;
std::cout<<"value of s = "<<s<<'\n';
}
It is syntactically valid to return a reference to a local variable but it doesn't make any sense to return a reference a local variable. Because the local variable is destroyed when execution of function finishes. So the reference to such variable is invalid & results in undefined behavior in C++.
Consider following program:
#include <iostream>
int& foo()
{
int s(9);
return s;
}
int main()
{
std::cout<<foo()<<'\n';
}
Compiler shows only warning when compiling above program.
[Warning] reference to local variable 's' returned [-Wreturn-local-addr]
Divya Nesana K said:
6 years ago
Option C is correct.
We can return a global variable by reference.
If you workout a simple program you will get the answer !
And,
We cannot return a local variable by reference.
If you do so, you will get a warning.. which is a no right?
#include<iostream>
using namespace std;
int& a(){
int b=10;
return b;
}
int main(){
a();
}
bix.cpp [Warning] reference to local variable 'b' returned [-Wreturn-local-addr]
We can return a global variable by reference.
If you workout a simple program you will get the answer !
And,
We cannot return a local variable by reference.
If you do so, you will get a warning.. which is a no right?
#include<iostream>
using namespace std;
int& a(){
int b=10;
return b;
}
int main(){
a();
}
bix.cpp [Warning] reference to local variable 'b' returned [-Wreturn-local-addr]
Yogesh said:
9 years ago
We can return global variable by reference.
But can not return a local variable by reference.
The code written by @Pravee is working fine because after returning from the function, the value goes out of scope but that memory is not allocated by some other variable. So we are able to print proper value. But if that memory will be allocated by some other variable, then we will not able to get the correct value.
But can not return a local variable by reference.
The code written by @Pravee is working fine because after returning from the function, the value goes out of scope but that memory is not allocated by some other variable. So we are able to print proper value. But if that memory will be allocated by some other variable, then we will not able to get the correct value.
Sai kiran said:
8 years ago
We can return a global variable by reference and also we can return local variable by reference (it is not a language error), the user/ programmer has to take care not to return local variable by reference as it might not be there on the stack when he later uses it.
So, if we simply take what is allowed, at least by the compiler, the answer would be A.
So, if we simply take what is allowed, at least by the compiler, the answer would be A.
Jerry said:
7 years ago
Only A should be correct.
Otherwise they should specify what they mean by "can't", because you can actually return a local variable by reference, it's a potential bug, but you "can" do it you won't get any crash or compilation error by doing it.
And returning a reference to a global variable is totally legal.
Otherwise they should specify what they mean by "can't", because you can actually return a local variable by reference, it's a potential bug, but you "can" do it you won't get any crash or compilation error by doing it.
And returning a reference to a global variable is totally legal.
(1)
Ajay Kuril said:
9 years ago
Here local value is returned by reference so global and local both can be returned by reference.
int main()
{
float grade();
float gpa = grade();
cout<<gpa;
return 0;
}
float grade()
{
float gpa = 3.5;
float &a=gpa;
return a;
}
The output is 3.5.
int main()
{
float grade();
float gpa = grade();
cout<<gpa;
return 0;
}
float grade()
{
float gpa = 3.5;
float &a=gpa;
return a;
}
The output is 3.5.
Okan said:
8 years ago
int myGlobal = 1978;
int& returnGlobal()
{
return myGlobal;
}
int &myr = returnGlobal();
cout << "Global reference value=" << myr << endl;
It prints 1978 on the screen.
So returning global reference is possible.
int& returnGlobal()
{
return myGlobal;
}
int &myr = returnGlobal();
cout << "Global reference value=" << myr << endl;
It prints 1978 on the screen.
So returning global reference is possible.
Preetham said:
1 decade ago
You cannot return reference of local variable, by return statement function ends and memory attached to the local variable will be deleted or freed by the compiler. So the reference does not have anything to refer local variable is already deleted.
Nidhi said:
1 decade ago
Why is 1st wrong??
int& ret ()
{
int &d=g;
d=100;cout<<g;
return(d);
}
works fine where g is a global variable
nd in main i can do
ret()+=10;
cout<<"global value"<<g<<endl;
its 110;
int& ret ()
{
int &d=g;
d=100;cout<<g;
return(d);
}
works fine where g is a global variable
nd in main i can do
ret()+=10;
cout<<"global value"<<g<<endl;
its 110;
Pravee said:
1 decade ago
But if you see. ith following code works perfectly fine. It displays the value 9.
int& myfunc()
{
int i=9;
return i;
}
int main() {
int g=myfunc();
cout<<g<<endl;
return 0;
}
int& myfunc()
{
int i=9;
return i;
}
int main() {
int g=myfunc();
cout<<g<<endl;
return 0;
}
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers