-
Problems with STL string
Hi,
I'm having an issue with the STL string, in particular using erase. Not sure if anyone has seen this before and can advise or not...
I'm developing on Windows 2000 using .NET and the project is a console app.
Very simply I am constructing a string from the command line application name command line parameter and then using erase to remove the the path from the name:
string strErase = "\\";
string strParam(szParamList[PARAM_NAME]);
strParam.erase(0, strParam.rfind(strErase)+1);
Now if I look at strParam in the watch window this is what I see:
1. after constructing:
strParam {"w:\cleanacc\Debug\cleanacc.exe"} std::basic_string<char,std::char_traits<char>,std::allocator<char> >
+ std::_String_val<char,std::allocator<char> > {_Alval={...} } std::_String_val<char,std::allocator<char> >
npos 4294967295 const unsigned int
+ _Bx {_Buf=0x0012fad4 "°/" _Ptr=0x002f07b0 "w:\cleanacc\Debug\cleanacc.exe" } std::basic_string<char,std::char_traits<char>,std::allocator<char> >::_Bxty
_Mysize 30 unsigned int
_Myres 31 unsigned int
2. after erase:
- strParam {"å/*HN^ä"} std::basic_string<char,std::char_traits<char>,std::allocator<char> >
+ std::_String_val<char,std::allocator<char> > {_Alval={...} } std::_String_val<char,std::allocator<char> >
npos 4294967295 const unsigned int
+ _Bx {_Buf=0x0012fad4 "°/" _Ptr=0x002f07b0 "cleanacc.exe" } std::basic_string<char,std::char_traits<char>,std::allocator<char> >::_Bxty
_Mysize 12 unsigned int
_Myres 31 unsigned int
I can printf the contents and it all appears OK, the size and the Ptr appear OK as well but I am worried by what is shown next to the variable name ({"å/ HN^ä"}).
If I substitute the command line parameter for the string "This is a slightly longer \\string than I need" then the erase works absolutely fine:
string strErase = "\\";
string strParam("This is a slightly longer \\string than I need");
strParam.erase(0, strParam.rfind(strErase)+1);
1. after constructing:
- strParam {"This is a slightly longer \string than I need"} std::basic_string<char,std::char_traits<char>,std::allocator<char> >
+ std::_String_val<char,std::allocator<char> > {_Alval={...} } std::_String_val<char,std::allocator<char> >
npos 4294967295 const unsigned int
+ _Bx {_Buf=0x0012fad4 "°/" _Ptr=0x002f07b0 "This is a slightly longer \string than I need" } std::basic_string<char,std::char_traits<char>,std::allocator<char> >::_Bxty
_Mysize 45 unsigned int
_Myres 47 unsigned int
2. after erase:
- strParam {"string than I need"} std::basic_string<char,std::char_traits<char>,std::allocator<char> >
+ std::_String_val<char,std::allocator<char> > {_Alval={...} } std::_String_val<char,std::allocator<char> >
npos 4294967295 const unsigned int
+ _Bx {_Buf=0x0012fad4 "°/" _Ptr=0x002f07b0 "string than I need" } std::basic_string<char,std::char_traits<char>,std::allocator<char> >::_Bxty
_Mysize 18 unsigned int
_Myres 47 unsigned int
Can anyone help?
-
Right, I have now established exactly what I am seeing and why it is working the way it is.
STL strings work hold their contents in one of two ways. If the string is less than or equal to 16 characters, including null terminator, then they are stored in an internal buffer, in the debug window as:
_Bx {_Buf=0x0012fad4 "°/"
otherwise they are stored seperate area of memory and the string uses a pointer the memory, in dthe debug wndow as:
_Ptr=0x002f07b0 "w:\cleanacc\Debug\cleanacc.exe"
When you construct the string the debug window nows where to find the contents and displays it correctly:
strParam {"w:\cleanacc\Debug\cleanacc.exe"}
However, if you erase enough of the string to shorten it to 16 characters or less, including null terminator, the debug window now expects to find the contents in the internal buffer and fails to display it correctly.
Interestingly, if you go the other way, i.e. from a short string to a long one, the contents are moved automatically from the internal buffer to the ptr store and the debug window displays the contents fine:
1. Create short string:
- strString {"This is "} std::basic_string<char,std::char_traits<char>,std::allocator<char> >
+ std::_String_val<char,std::allocator<char> > {_Alval={...} } std::_String_val<char,std::allocator<char> >
npos 4294967295 const unsigned int
- _Bx {_Buf=0x0012fde4 "This is " _Ptr=0x73696854 <Bad Ptr> } std::basic_string<char,std::char_traits<char>,std::allocator<char> >::_Bxty
+ _Buf 0x0012fde4 "This is " char [16]
+ _Ptr 0x73696854 <Bad Ptr> char *
_Mysize 8 unsigned int
_Myres 15 unsigned int
2. Append string to make it greater than 16 characters:
- strString {"This is a string that is too long"} std::basic_string<char,std::char_traits<char>,std::allocator<char> >
+ std::_String_val<char,std::allocator<char> > {_Alval={...} } std::_String_val<char,std::allocator<char> >
npos 4294967295 const unsigned int
- _Bx {_Buf=0x0012fde4 "°/" _Ptr=0x002f07b0 "This is a string that is too long" } std::basic_string<char,std::char_traits<char>,std::allocator<char> >::_Bxty
+ _Buf 0x0012fde4 "°/" char [16]
+ _Ptr 0x002f07b0 "This is a string that is too long" char *
_Mysize 33 unsigned int
_Myres 47 unsigned int
Just in case anyone is intersted.
-
I'm glad you've found the answer all by yourself! Remember also that many STL containers have a debug implementation which is quite different from theie release implementation. Anyway, to verify that the string is valid, simply make sure that its size isn't 0 and that every characters in str[0] until str[str.size()] is what you expect to find.
Danny Kalev
Similar Threads
-
By ObiWan in forum VB Classic
Replies: 3
Last Post: 05-23-2006, 10:35 AM
-
By airrazor in forum Java
Replies: 11
Last Post: 11-06-2005, 10:18 AM
-
By airrazor in forum Database
Replies: 1
Last Post: 10-29-2005, 10:13 AM
-
Replies: 3
Last Post: 10-02-2005, 11:57 PM
-
By Martin in forum VB Classic
Replies: 22
Last Post: 12-03-2001, 03:53 AM
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
Forum Rules
|
Top DevX Stories
Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL
|
Bookmarks