<分区>
一个我不确定答案的基本问题。 follow函数是否有效?
std::vector<int> & test_function() {
std::vector<int> x;
// do whatever
return x;
}
如果是,为什么?程序不应该在函数返回后从堆栈中删除 x 吗?谢谢。
<分区>
一个我不确定答案的基本问题。 follow函数是否有效?
std::vector<int> & test_function() {
std::vector<int> x;
// do whatever
return x;
}
如果是,为什么?程序不应该在函数返回后从堆栈中删除 x 吗?谢谢。
最佳答案
行为未定义。您不应返回对局部变量的引用。
关于C++返回引用/堆栈内存,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3252292/