通过引用传递的 BSTR 是否需要在调用函数中重新分配?

发布于 2024-11-14 13:22:21 字数 602 浏览 4 评论 0原文

BSTR newID_x = SysAllocString(L"newID");
BSTR newX_x = SysAllocString(L"newX");

functionA(&newID_x);

//Func A does some operation on newID_x, we have that value in newID_x now

functionA(&newX_x);
//After Func A is called for the second time, both newID_x and newX_x become the same
//i.e, both are pointing to same locations and hence values too

我的问题是,对于 BSTR 来说,这是正确的行为吗?在调用 之后,我们是否需要将 newX_x 保存在一些新的 BSTR 中>functionA 第一次?

或者 functionA 的一部分是否错误,它可能错误地分配/取消分配传递的 BSTR

BSTR newID_x = SysAllocString(L"newID");
BSTR newX_x = SysAllocString(L"newX");

functionA(&newID_x);

//Func A does some operation on newID_x, we have that value in newID_x now

functionA(&newX_x);
//After Func A is called for the second time, both newID_x and newX_x become the same
//i.e, both are pointing to same locations and hence values too

My question is that, is it a correct behavior for BSTRs, do we need to save the newX_x in some new BSTR after calling functionA the first time?

Or is it wrong on part of functionA that it may be wrongly allocating/de-allocating the passed BSTRs.

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

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

发布评论

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

评论(1

暖心男生 2024-11-21 13:22:21

您描述的是“输入-输出”参数语义 - 参数在调用之前初始化,然后在调用过程中更改并且更改对调用者可见。可以接受,但是有这样的接口不是很方便。在这种情况下,被调用者必须重新分配BSTR,然后将所有权传递给调用者。

What you describe is "in-out" parameter semantics - the parameter is initialized prior to call, then while in the call it is changed and the change is visible to the caller. It is acceptable, but having such interface is not very convenient. In this case the callee will have to reallocate the BSTR and then pass ownership to the caller.

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