Modifying BSTR length


can I safely modify the length of a BSTR like

BSTR bstrVal = SysAllocString(L"Hello world");
UINT* pSize = (UINT*)((BYTE*)bstrVal - 4);
*pSize = *pSize / 2;

without having SysFreeString(bstrVal) producing memory leaks?

Or does SysFreeString look at the size of the BSTR and frees only such amount?

Because my string is rather large I don't want to reallocate and copy.
Relying on the size being stored as 4 byte before the string itself it's a dirty hack and could change in future versions of windows (don't know if this even work for 64-bit windows, is 4 bytes enough ? )

Use SysStringLen to get the size and SysReAllocString to reallocate the string.
The BSTR memory format is not changed on 64bit. 2GB characters should be enough for the future.

If I duplicate with SysReAllocString I have to copy the original and so temporarily double the memory consumption.
I wanted to avoid that.
Take 2 (realising you want to use less of the memory)

can I safely modify the length of a BSTR like

You shouldn't alter the length of the BSTR, due to the memory caching I mentioned in my last attempt. (I have no real ideal about how it's implemented, but that's why it's best to play safe)

But there's nothing to stop you using only part of the memory. Just put a null terminator where you want your string to stop. The BSTRs routines are perfectly happy with embedded nulls, unlike normal string funtions.

Take 1 (when I thought you wanted more memory)

can I safely modify the length of a BSTR like


What you are suggesting is actually dangerous.

When you request a certain size of BSTR, the allocator would have given you memory from its memory cache. What it's using the memory blocks for after the ones for your string is not known to you. So changing how much memory you are using without informing the allocator is asking for trouble as it won't know about the change and could legitimately decide to use its memory (which you've stolen) for another BSTR allocation.

In addition to confusing the memory cache mechanism, there could be another BSTR right after your string. By changing your block size and extending the string you could be trampling on the start of another BSTR, including its length.

To reduce copying, you could use SysAllocStringLen to over allocate to start with?

Last edited on
Topic archived. No new replies allowed.