You're welcome, and, no, unfortunately there's no standard way to reallocate a buffer while maintaining its zero-filled state.
A possible work-around is to manually zero-fill the new region using memset(), as in:
Code: Select all
#include "crt.bi"
function Creallocate(byval p as any ptr, byval oldsize as integer, byval newsize as integer) as any ptr
var c = reallocate(p, newsize)
memset(c+oldsize, 0, newsize-oldsize)
return c
end function
dim as byte ptr x
x = callocate(128)
x = Creallocate(x, 128, 1024)
But the downside to this approach is the fact that when you do this, you explicitly write to the whole memory region, which results it being immediately pulled out of the paging pool and committed into physical memory, this leads to poor performance and memory bloat in large-scale applications, and is considered a bad practice, as it leads to bottlenecks performance-wise, should be fine for small structures though.
A good alternative would be eliminating the need to zero-fill your memory in the first place, for instance, you can use
allocate as long as you initialize the data immediately (this is also considered an optimized approach), as in; why zero-fill a buffer that you're going to overwrite anyways?
You should never need to reallocate the buffer for a UDT, if you're using it for data storage, and have variable-length data in there, it is advised to only store a pointer to the actual data in the UDT itself, note that UDTs are usually stored on the stack when you use DIM to create them, and if you try to reallocate a pointer to a UDT on the stack... well, bad things could surely happen.