如何解决在 C++ 中返回本地协程变量的地址是否合法?
如果我返回一个本地协程变量的地址,例如通过承诺,它是否保证按 C++ 标准工作?考虑一个例子(基于 https://www.scs.stanford.edu/~dm/blog/c++-coroutines.html):
#include <coroutine>
#include <iostream>
struct ReturnObject {
struct promise_type {
unsigned * value_ = nullptr;
void return_void() {}
ReturnObject get_return_object() {
return {
.h_ = std::coroutine_handle<promise_type>::from_promise(*this)
};
}
std::suspend_never initial_suspend() { return {}; }
std::suspend_never final_suspend() noexcept { return {}; }
void unhandled_exception() {}
};
std::coroutine_handle<promise_type> h_;
operator auto() const { return h_; }
};
template<typename PromiseType>
struct GetPromise {
PromiseType *p_;
bool await_ready() { return false; }
bool await_suspend(std::coroutine_handle<PromiseType> h) {
p_ = &h.promise();
return false;
}
PromiseType *await_resume() { return p_; }
};
ReturnObject counter()
{
auto pp = co_await GetPromise<ReturnObject::promise_type>{};
for (unsigned i = 0;; ++i) {
pp->value_ = &i; // is it legal?
co_await std::suspend_always{};
}
}
int main()
{
std::coroutine_handle<ReturnObject::promise_type> h = counter();
auto &promise = h.promise();
for (int i = 0; i < 5; ++i) {
std::cout << "counter: " << *promise.value_ << std::endl;
h();
}
h.destroy();
}
https://gcc.godbolt.org/z/P5PMc15qW
在实践中我看到它确实有效,但它真的合法吗?
解决方法
是的,这是合法的。 counter
的局部变量存储在 h
拥有的动态分配对象中。
关于释放后使用可能性的常见警告是存在的,即 promise
悬挂在 h.destroy()
之后。
它在某些情况下可以工作,但这不是编写程序的好方法。指针指向的内存单元可以(并且可能很快)被其他变量覆盖。尝试在循环之前编写一些代码(声明一些变量)并再次检查结果。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。