| adebaene@club-internet.fr 2005-02-14, 9:11 pm |
|
Shlomo Kabaz wrote:
> My name is Shlomo Kabaz .
>
> I wrote an application in vc7 that manipulate variable
std::string .
>
> After using the operator "+=" for a few time it seems the string got
garbaged.
> After a long check it seems that when size of string is above 16 the
string
> is
> garbaged .
How do you know the string is garbaged? If it by watching in the
debugger, it's the debugger that is at fault, the program is correct.
In VC7, std::string use what is called "small string optimization".
That is, the std::string object contains an internal buffer of
_BUF_SIZE length. When the string is shorter than that, the string
content is stored directly in that buffer, so there is no need for
dynamic allocation. When the string is longer, part of the buffer is
used as pointer to a dynamically allocated memory block.
This is implmented by this snippet of xstring :
union _Bxty
{ // storage for small buffer or pointer to larger one
_Elem _Buf[_BUF_SIZE];
_Elem *_Ptr;
} _Bx;
This mechanism means that the content of the string is not always
accessed the same way inside a std::string object, depending on the
string ength. Ths VC 7.0 debugger has a hard-time dealing with that.
> When i changed definition of "_BUF_SIZE" in "xstring" file the
problem
> was
> solved
> The "_BUF_SIZE" was 16 before .
NEVER EVER touch the system or STL headers, unless you *really* know
what you are doing.
> What is wromg here , where is default dynamic allocation in STL .
Nothing in wrong in the STL, it's a problem with the debugger. cout the
string if you want to be sure.
Arnaud
MVP - VC
|