c++oleistorage

Should ILockBytes::SetSize preserve existing data?


I have read the MSDN documentation about ILockBytes::SetSize(), but it does not answer precisely whether my implementation of SetSize() should preserve existing data.

For example, if the current buffer size is 100, and SetSize(120) is called, should it preserve data in the first 100 bytes in the resized buffer?

Also, if the current buffer size is 100, and SetSize(70) is called, should it preserve data in the first 70 bytes in the resized buffer?

In other words - should I call realloc() or single alloc() in C++?


Solution

  • Yes, the existing byte values are expected to be preserved. Extending the size of the array just adds new bytes of unspecified value to the end of the array. And reducing the size of the array just chops off the trailing bytes from the end of the array.

    If extending the array did not preserve existing data, then writing new bytes to the end of the array via ILockBytes::WriteAt() would be meaningless:

    If ulOffset is past the end of the byte array and cb is greater than zero, ILockBytes::WriteAt increases the size of the byte array. The fill bytes written to the byte array are not initialized to any particular value.

    How you decide to implement the expansion/reduction logic internally is up to you.