开发者

Does a BSTR passed by reference need re-allocation in the calling function?

开发者 https://www.devze.com 2023-03-12 03:02 出处:网络
BSTR newID_x = SysAllocString(L\"newID\"); BSTR newX_x = SysAllocString(L\"newX\"); functionA(&newID_x);
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开发者_JS百科 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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消