0

I am learning C++. I use both visual studio 2015 and codeblocks IDE to code C++. I tried to write a program that returns a reference variable from a function and I get different results(two results) from 2 IDE( Visual Studio 2015 and codeblocks) although I run the same code. I tried writing the following code:

#include <iostream>
using namespace std;
class Demo
{
public:
  int a;
};
//I wrote a function that returns a reference variable
Demo& func()
{
   Demo temp;
   temp.a = 1;
   return temp;
}
int main() 
{
  Demo& d = func(); //it error if I run this code on Codeblocks and it run 
                   //smoothly if I run it on Visual Studio 2015
  cout<<d.a;
  return 0;
}

I known that it depends on compiler but I want to know Which is correct in this case? Thank in advance!

TaQuangTu
  • 2,155
  • 2
  • 16
  • 30
  • 2
    Your code invokes undefined behavior. You can't return a reference to a local variable - this causes a dangling reference. – DeiDei May 24 '18 at 11:02
  • Possible duplicate of [C++ Returning reference to local variable](https://stackoverflow.com/questions/4643713/c-returning-reference-to-local-variable) – einpoklum May 24 '18 at 11:05
  • Thanks for your comment, I also want we to know more about two compilers in VS2015 and CodeBlocks. – TaQuangTu May 24 '18 at 11:11
  • What?! I get *"warning [C4172](https://msdn.microsoft.com/query/dev14.query?appId=Dev14IDEF1&l=EN-US&k=k(C4172)&rd=true): returning address of local variable or temporary: temp"* from VS2015. Please check the [online warning documentation](https://msdn.microsoft.com/query/dev14.query?appId=Dev14IDEF1&l=EN-US&k=k(C4172)&rd=true) before trying to run the code. You just have to click the number in the error list to get there! – Bo Persson May 24 '18 at 11:24
  • Maybe I have turned off all warning in VS2015. I will research more to turn on it again. Thank you. – TaQuangTu May 24 '18 at 11:28

3 Answers3

2

First note that what determines program behavior is the compiler rather than the IDE (the program you use to write the code). Other factors are what's currently on disk and what you get as input (e.g. from the user, the Network), the system clock etc.

Now, as @DeiDei correctly points out, you get different behaviour because your func() function returns a reference to a variable that's local to it, and goes out of scope when its execution concludes. Its memory on the stack (or the register associated with it) may become used by other data - and you get no guarantees on what happens when you access it. This is an example of compilable code which has Undefined Behavior when run.

Finally, most compilers will warn you about this - and I'm sure that's true for both compilers used by your IDEs. So you should:

  1. Turn on compiler warning flags.
  2. Read the warnings and address them.
einpoklum
  • 118,144
  • 57
  • 340
  • 684
  • Thank for you answer. Through this small example, I want people to be more careful when using any compiler – TaQuangTu May 24 '18 at 11:16
1

What you're doing is undefined behaviour as you are returning a reference to something that is destroyed when the function goes out of scope. The fact that is works in VS2015 is just chance.

If you want to return a locally created object then either return it by value, or dynamically allocate it and return it as a pointer using either shared_ptr or unique_ptr.

Sean
  • 60,939
  • 11
  • 97
  • 136
  • Thank for you answer. Through this small example, I want people to be more careful when using any compiler – TaQuangTu May 24 '18 at 11:16
1

It really is simple. When the function func reaches its last line, the life time of temp dies. Whenever you try to access that value, a segmentation fault will occur which tells you that you are trying to access an illegal memory location.

I really can't explain the reason of your success in Visual Studio more than luck.