I
Isak Dinesen
Being relatively new to c++, I've recently discovered a few things
about the behavior of new with respect to wchar_t, ZeroMemory and
delete. I can't seem to find documentation describing the following
three behaviors:
1. int len = 5; wchar_t *pwch = new wchar_t[len];
Subsequent inspection of pwch always show that it is allocated space
for 12 WCHARs. (The amount of overallocation varies depending on the
value of len).
2. By default pwch always begins with a string of five 0xcdcd's,
followed by two 0xfdfd's, then four 0xabab's and finally the string is
always terminated by a 0xfeee.
3. If I somehow overwrite any of the 0xfdfd's, 0xabab's or the 0xfeee,
subsequent calls to delete fail and my application terminates
abnormally.
After spending an hour or so exploring, it has become clear that VC
likes to overallocate storage for wchar_t, uses a 0xfdfd, 0xabab,
0xfeee sequence to identify the overallocated bytes so delete can
correctly identify and free the bytes following the null string
terminator.
As a newb, I wanted to use ZeroMemory to zero-out the entire 12 WCHARs
rather than just the WCHAR at position pwch + 4. When that failed, I
read the documentation on new wchar_t and delete, and hunted around
usenet for about 2 hours before attempting to reinvent the wheel and
solve the problem on my own (losing another 2 hours).
Where does one find documentation on the allocation of specific types?
Why are these strings over-allocated? Is that an optimization?
Any replies are much appreciated.
about the behavior of new with respect to wchar_t, ZeroMemory and
delete. I can't seem to find documentation describing the following
three behaviors:
1. int len = 5; wchar_t *pwch = new wchar_t[len];
Subsequent inspection of pwch always show that it is allocated space
for 12 WCHARs. (The amount of overallocation varies depending on the
value of len).
2. By default pwch always begins with a string of five 0xcdcd's,
followed by two 0xfdfd's, then four 0xabab's and finally the string is
always terminated by a 0xfeee.
3. If I somehow overwrite any of the 0xfdfd's, 0xabab's or the 0xfeee,
subsequent calls to delete fail and my application terminates
abnormally.
After spending an hour or so exploring, it has become clear that VC
likes to overallocate storage for wchar_t, uses a 0xfdfd, 0xabab,
0xfeee sequence to identify the overallocated bytes so delete can
correctly identify and free the bytes following the null string
terminator.
As a newb, I wanted to use ZeroMemory to zero-out the entire 12 WCHARs
rather than just the WCHAR at position pwch + 4. When that failed, I
read the documentation on new wchar_t and delete, and hunted around
usenet for about 2 hours before attempting to reinvent the wheel and
solve the problem on my own (losing another 2 hours).
Where does one find documentation on the allocation of specific types?
Why are these strings over-allocated? Is that an optimization?
Any replies are much appreciated.