c++ 返回引用/堆栈内存 [英] c++ return reference / stack memory

查看:23
本文介绍了c++ 返回引用/堆栈内存的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

一个我不确定答案的基本问题.跟随功能有效吗?

A basic question that I'm not sure of the answer. Is the follow function valid?

std::vector<int> & test_function() {
   std::vector<int> x;

   // do whatever

   return x;
}

如果是这样,为什么?函数返回后程序不应该从堆栈中删除 x 吗?谢谢.

If so, why? Shouldn't the program delete x from the stack after the function returns? Thanks.

推荐答案

行为未定义.您不应该返回对局部变量的引用.

The behavior is undefined. You shouldn't return references to local variables.

这篇关于c++ 返回引用/堆栈内存的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆