[英]Return reference in c++ when use Visual Studio and Codeblocks
I am learning C++. 我正在学习C ++。 I use both visual studio 2015 and codeblocks IDE to code C++. 我同时使用Visual Studio 2015和代码块IDE来编码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. 我尝试编写一个从函数返回引用变量的程序,尽管我运行相同的代码,但从2个IDE(Visual Studio 2015和代码块)中得到了不同的结果(两个结果)。 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! 预先感谢!
First note that what determines program behavior is the compiler rather than the IDE (the program you use to write the code). 首先请注意,决定程序行为的是编译器,而不是IDE(用于编写代码的程序)。 Other factors are what's currently on disk and what you get as input (eg 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. 现在,正如@DeiDei正确指出的那样,您会得到不同的行为,因为func()
函数返回对该变量本地的变量的引用,并在执行结束时超出范围。 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. 最后,大多数编译器都会对此发出警告-我确信这对于您的IDE所使用的两种编译器都是正确的。 So you should: 因此,您应该:
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. 在VS2015中有效的事实只是偶然。
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
. 如果要返回本地创建的对象,则可以按值返回它,也可以使用shared_ptr
或unique_ptr
动态分配它并将其作为指针返回。
It really is simple. 真的很简单。 When the function func
reaches its last line, the life time of temp
dies. 当函数func
到达最后一行时, temp
的生存时间消失。 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. 每当您尝试访问该值时,都会发生segmentation fault
,告诉您您正在尝试访问非法的内存位置。
I really can't explain the reason of your success in Visual Studio more than luck. 除了运气,我真的无法解释您在Visual Studio中成功的原因。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.