STL中的string的问题

Alan S1 2009-02-27 11:41:43
调试一看,这里的Bad Ptr是什么意思呢?
+ _Bx {_Buf=0x00ea948c "disconnect" _Ptr=0x63736964 <Bad Ptr> } std::basic_string<char,std::char_traits<char>,std::allocator<char> >::_Bxty

并且我这里导致了内存泄漏,
const char *pstr;
string s;
s = pstr;
这句话会让s另外单独开空间拷贝一段字符串吧?
...全文
192 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
matrixdwy 2009-02-28
  • 打赏
  • 举报
回复
关注一下
Sco_field 2009-02-28
  • 打赏
  • 举报
回复
[Quote=引用 9 楼 boyplayee 的回复:]
请大家看看清楚啊,我已经说了,我打字打错了,看来得重新开贴
[/Quote]


希望下面的一段文本能对你有所帮助:

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


----

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.

疯哥哥 2009-02-28
  • 打赏
  • 举报
回复
正好我前几天研究过string,呵呵
楼主可参看VC下的string的实现,
_Bx实际上是个union:

union _Bxty
{
char _Buf[16];
char* _Ptr;
}_Bx;

_Buf和_Ptr共享了一块内存.当字符串小的时候使用_Buf,太大存储不下就用_Ptr指向堆内存.
所以BadPtr的地方没有问题._Buf和_Ptr只要有一个显示正常,一般就没有问题.
const char *pstr;
string s;
s = pstr;
如果pstr是个正常的字符串那么情况同上,pstr如果比较小,就拷贝到string的缓冲里,在栈上(因为string s是栈变量).pstr太大时候s另外单独开空间拷贝.
至于内存泄露,单从上面代码看不出问题...
Alan S1 2009-02-28
  • 打赏
  • 举报
回复
请大家看看清楚啊,我已经说了,我打字打错了,看来得重新开贴
  • 打赏
  • 举报
回复
const char *pstr;
string s;
s = pstr;
这样pstr不初始化,肯定不行,pstr值未定义,则行为未定义。
oyljerry 2009-02-28
  • 打赏
  • 举报
回复
[Quote=引用楼主 boyplayee 的帖子:]
调试一看,这里的Bad Ptr是什么意思呢?
+ _Bx {_Buf=0x00ea948c "disconnect" _Ptr=0x63736964 <Bad Ptr> } std::basic_string <char,std::char_traits <char>,std::allocator <char> >::_Bxty

并且我这里导致了内存泄漏,
const char *pstr;
string s;
s = pstr;
这句话会让s另外单独开空间拷贝一段字符串吧?
[/Quote]
pstr要指向一个常量字符串...
s 会开辟一个空间拷贝字符串
waizqfor 2009-02-28
  • 打赏
  • 举报
回复
[Quote=引用楼主 boyplayee 的帖子:]
调试一看,这里的Bad Ptr是什么意思呢?
+ _Bx {_Buf=0x00ea948c "disconnect" _Ptr=0x63736964 <Bad Ptr> } std::basic_string <char,std::char_traits <char>,std::allocator <char> >::_Bxty

并且我这里导致了内存泄漏,
const char *pstr;
string s;
s = pstr;
这句话会让s另外单独开空间拷贝一段字符串吧?
[/Quote]
const char *pstr; 常量定义的时候 是要直接赋值的 你这样编译器肯定会报错的
Alan S1 2009-02-28
  • 打赏
  • 举报
回复
不好意思,const char *pstr = "a";
Alan S1 2009-02-27
  • 打赏
  • 举报
回复
pCfmt->sName.reserve(32);加上这一句居然没有内存泄漏了
yangch_nhcmo 2009-02-27
  • 打赏
  • 举报
回复

#include <iostream>
#include <string>
using namespace std;

int main()
{
const char *pstr = "123456" ; //pstr 要赋值
string s;
s = pstr;

cout << s << endl;

return 0;
}



如果不赋值运行在VS2008中出错,GCC4.3.2中输出乱码,没有出现LZ所说的那种提示,LZ用哪种编译器?
HelloDan 2009-02-27
  • 打赏
  • 举报
回复
const char *pstr;
指针向的是一个未知的地方啊。
Sco_field 2009-02-27
  • 打赏
  • 举报
回复

const char *pstr;

能这么写吗?不报错?

65,211

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧