简体   繁体   English

使用Visual Studio和代码块时以C ++返回引用

[英]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: 因此,您应该:

  1. Turn on compiler warning flags. 打开编译器警告标志。
  2. Read the warnings and address them. 阅读警告并加以解决。

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_ptrunique_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.

相关问题 C ++中没有这样的目录或文件[Codeblocks / Visual Studio] - No such directory or file in C++ [Codeblocks/Visual Studio] 无法在 C/C++ Visual Studio 中使用宽字符串,即使它可以在使用 MinGW 的 CodeBlocks 中使用 - Cannot use wide strings in C/C++ Visual Studio, even though it works in CodeBlocks using MinGW visual studio 2012用于C ++的编译器是兼容的/我可以将它与codeblocks一起使用吗? - what compiler does visual studio 2012 use for C++ and is it compatible/can I use it with codeblocks? 在Visual Studio,CodeBlocks和Eclipse中使用C ++编译/运行问题? - Compiling/Running Issues with C++ in Visual Studio, CodeBlocks, and Eclipse? C++ 代码在 CodeBlocks 中构建,但不在 Visual Studio 2015 中构建 - C++ code builds in CodeBlocks but not in Visual Studio 2015 代码块使用Visual C ++开发人员工具运行 - Codeblocks run with Visual C++ developer tools 如何使用Visual Studio C ++编译器? - How to use Visual Studio C++ Compiler? Visual Studio和C ++:使用过滤器作为目录 - Visual Studio & C++: Use filters as directories 如何在 Visual Studio c++ 中使用文件夹? - How to use folders in visual studio c++? 在Visual Studio调试模式下运行时C ++对象引用已损坏 - c++ object reference corrupted when running in Visual Studio debug mode
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM