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

查看:136
本文介绍了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天全站免登陆