通过引用传递的 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
我的问题是,对于 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 BSTR
s, 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 BSTR
s.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您描述的是“输入-输出”参数语义 - 参数在调用之前初始化,然后在调用过程中更改并且更改对调用者可见。可以接受,但是有这样的接口不是很方便。在这种情况下,被调用者必须重新分配
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.