返回指向局部变量的指针始终是未定义的行为
我已经读到,我们不应返回指针或对本地变量的引用。因此,在下面给定的示例中,我了解当我写作时:返回f;
enfunce foo
时,我正在将指针返回到本地变量。并在功能之外使用该指针,将导致不确定的行为。
#include <iostream>
const char* foo()
{
const char* f = "ffdf";
return f;//returning pointer to a local variable
}
const char* func()
{
return "fsfs";
}
int main()
{
const char* ptr = func();
const char* f = foo();
std::cout<<f<<std::endl; //I know this is undefined behavior because we're using a pointer that points to a local variable
std::cout<<ptr; //But IS THIS UNDEFINED BEHAVIOR too?
}
我的问题是,对于返回语句返回“ FSFS”;
内部功能func
内的返回语句。我知道在C ++ 17中有强制性复制Elison。因此,我的问题针对所有现代C ++版本(C ++ 11,C ++ 17等)。行为是否取决于/C ++版本不同。
特别是,我知道语句std :: cout&lt;&lt;&lt;&lt;&lt;&lt;&lt :: endl;
内部main
始终是未定义的行为指向局部变量的指针(悬空)。但是语句std :: cout&lt;&lt;
也会导致不确定的行为。如果不是为什么,这里会发生什么。
PS:在描述第一个cout
语句中实际发生的事情时,我可能是错误的。因此,如果我错了,请纠正我。另外,我的最初意图不仅限于字符串字面的特定类型。例如,我本可以选择返回int
而不是字符串文字,并将返回类型作为const int&amp;
。但是,由于人们已经开始回答,所以我没有更改示例以使用int
。
I have read that we should not return a pointer or a reference to a local variable. So in the below given example, i understand that when i wrote: return f;
inside function foo
, i am returning a pointer to a local variable. And using that pointer outside the function, will lead to undefined behavior.
#include <iostream>
const char* foo()
{
const char* f = "ffdf";
return f;//returning pointer to a local variable
}
const char* func()
{
return "fsfs";
}
int main()
{
const char* ptr = func();
const char* f = foo();
std::cout<<f<<std::endl; //I know this is undefined behavior because we're using a pointer that points to a local variable
std::cout<<ptr; //But IS THIS UNDEFINED BEHAVIOR too?
}
My question is that does the same hold true for the return statement return "fsfs";
inside function func
. I am aware that in C++17 there is mandatory copy elison. So my question is directed towards all modern C++ version(C++11, C++17, etc). Does the behavior depends/differs on C++ version.
In particular, i know that the statement std::cout<<f<<std::endl;
inside main
is always undefined behavior because we're using a pointer(dangling) that points to a local variable. But does the statement std::cout<<ptr;
also leads to undefined behavior. If not why and what will happen here.
PS: I might be wrong in describing what is actually happening in the first cout
statement too. So please correct me if i am wrong. Also, my original intention was not restricted to a particular type like a string literal. For example, i could've have chosen to return an int
instead of a string literal and have the return type as const int&
. But since people have already started answering i am not changing the example to use int
.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
返回指向非静态函数局部变量的指针将导致您在调用站点获得的指针成为悬空指针,并且使用它将会产生未定义的行为。
在这里,情况并非如此。字符串文字具有静态存储持续时间,这意味着它将一直存在到程序结束。这意味着返回指向函数中声明的字符串文字的指针是安全的。
所以
foo
和func
都是安全的,但如果你有那么你将返回一个指向不再存在的对象的指针,并且 UB 会尝试从返回的对象中读取指针。
Returning a pointer to a non-static function local variables will cause the pointer you get at the call site to be a dangling pointer and using it will have undefined behavior.
Here, this is not the case. A string literal has static storage duration, meaning it will live until the end of the program. This means it is safe to return a pointer to a string literal that was declared in a function.
So both
foo
andfunc
are safe, but if you hadThen you would be returning a pointer to an object that no longer exits and would have UB trying to read from that returned pointer.