将函数中的局部(函数内)变量的引用返回给调用者

发布于 2024-12-07 02:47:06 字数 623 浏览 1 评论 0原文

我的 Q 与这些非常相似,但不完全相同:

从函数返回对局部变量的常量引用

返回函数局部变量作为参考

我有一些用于解析 xml 文件的代码。 基本上发生的事情是使用 return 语句通过函数的引用 OUT 传递局部变量。更准确地说:

ezxml_t ezxml_parse_fd(int fd){

ezxml_root_t root;
//DO STUFF
return &root->xml;
}

调用者如下

ezxml_t xml = ezxml_parse_fd(fd);

好吧,该东西编译并工作(gcc)...但我一直知道局部变量一旦其作用域不再存在就会被销毁...我很困惑

my Q is quite similar to these, but not exactly the same to:

Returning const reference to local variable from a function

Returning function-local variable as reference

I have a bit of code that is used to parse an xml file.
what's basically happening is that a local variable is passed by reference OUT of the function using the return statement. To be more precise:

ezxml_t ezxml_parse_fd(int fd){

ezxml_root_t root;
//DO STUFF
return &root->xml;
}

the caller is the following

ezxml_t xml = ezxml_parse_fd(fd);

Well, the thing compiles and works (gcc)... but I have always known that a local variables are destroyed once their scope no longer exists... i'm confused

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(4

指尖微凉心微凉 2024-12-14 02:47:06

局部变量不是“通过引用传递”,因为这意味着返回&root;,这不是您的代码所说的。

相反,返回一个指向其他一些不相关变量的指针,您不想向我们展示该变量,我们应该假设代码已正确编写,以便该指针指向一些动态分配的内存,该内存将保持有效。

The local variable is not "passed out by reference", because that would mean return &root;, which is not what your code says.

Rather, a pointer to some other, unrelated variable, which you didn't care to show us, is returned, and we should assume that the code was correctly written so that that pointer points to some dynamically allocated memory which will remain valid.

我的痛♀有谁懂 2024-12-14 02:47:06

一旦函数返回,函数堆栈就会被展开并释放。对函数局部变量的引用无效。您现在可能会侥幸逃脱,但不要依赖它!
为什么不将输出参数传递给函数来存储值?也许,在这些行中:

void ezxml_parse_fd(int fd, ezxml_t *parse_value) 
{

ezxml_root_t root;
//DO STUFF
*parse_value = &root->xml;
return;
}

...

ezxml_t xml;
ezxml_parse_fd(fd, &xml);

如果您使用gcc,您很可能会看到一条警告消息“警告:函数返回局部变量的地址”请这样做注意编译器警告!它只是想帮助您(大多数时候):)
希望这有帮助!

Once the function returns, function stack is unwound & references to function-local variables are invalid. You may get away with it for now, but don't rely on it!
Why not pass an output parameter to the function to store the value? Maybe, on these lines:

void ezxml_parse_fd(int fd, ezxml_t *parse_value) 
{

ezxml_root_t root;
//DO STUFF
*parse_value = &root->xml;
return;
}

...

ezxml_t xml;
ezxml_parse_fd(fd, &xml);

If you are using gcc there is a good chance that you might have seen a warning message "warning: function returns address of local variable" Please do pay attention to compiler warning! It is only trying to help you (most of the times) :)
Hope this helps!

放低过去 2024-12-14 02:47:06

是的,局部变量已经被销毁,但是您的引用指向内存中保存最后分配值的位置。放置变量的位置。在大多数情况下,它可以在简单类型的单线程环境中工作。
但是,当您在堆栈上使用析构函数分配某个对象时,析构函数将在块(在您的情况下为函数)执行完成时被调用。因此,在函数之外,您可以引用被破坏的对象。
简而言之:不要这样做:-)

yes, the local variables are destroyed already, but your reference points to the place in memory that hold last assigned value. The place where your variable was placed. And it will work in most cases in the single threaded environment for simple types.
But when you allocate on the stack some object with destructor, the destructor will be called just when the execution of block (in your case -- function) will be finished. So outside the function you have a reference to destroyed object.
Shortly: don't do this :-)

梦过后 2024-12-14 02:47:06

在 C 语言中——这可能是非常错误的。
请告诉我们 ezxml_t 是什么以及 root->xml 是什么。

我的猜测是 ezxml_t 会被您返回的指向 root->xml 的指针自动转换为。

In C - that is probably very wrong.
Please tell us what ezxml_t is and what root->xml is.

My guess is that ezxml_t is automatically casted to by the pointer to root->xml that you return.

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