返回指向局部变量的指针始终是未定义的行为

发布于 2025-01-18 09:42:19 字数 1169 浏览 4 评论 0原文

我已经读到,我们不应返回指针或对本地变量的引用。因此,在下面给定的示例中,我了解当我写作时:返回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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

你怎么这么可爱啊 2025-01-25 09:42:19

返回指向非静态函数局部变量的指针将导致您在调用站点获得的指针成为悬空指针,并且使用它将会产生未定义的行为。

在这里,情况并非如此。字符串文字具有静态存储持续时间,这意味着它将一直存在到程序结束。这意味着返回指向函数中声明的字符串文字的指针是安全的。

所以 foofunc 都是安全的,但如果你有

const char * bar()
{
    std::string text = "some text";
    // stuff
    return text.c_str();
}

那么你将返回一个指向不再存在的对象的指针,并且 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 and func are safe, but if you had

const char * bar()
{
    std::string text = "some text";
    // stuff
    return text.c_str();
}

Then you would be returning a pointer to an object that no longer exits and would have UB trying to read from that returned pointer.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文