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另外单独开空间拷贝一段字符串吧?
...全文
190 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;

能这么写吗?不报错?
内容概要:SSD2828QN4是一款MIPI主桥接芯片,用于连接应用处理器与传统并行LCD接口及支持MIPI从属接口的LCD驱动器。该芯片支持最高每通道1Gbps的串行链路速度,最多可配置4个数据通道,显著减少了信号数量。它支持多种接口模式,包括RGB+SPI组合接口,适用于驱动智能或非智能显示面板,并能通过命令模式和视频模式传输数据。芯片内置时钟和复位模块、外部接口、协议控制单元(PCU)、包处理单元(PPU)、错误校正码/循环冗余校验(ECC/CRC)模块、长包和命令缓冲区、D-PHY控制器、模拟收发器以及内部锁相环(PLL),确保了高效的数据传输和系统稳定性。此外,文档详细描述了芯片的引脚分配、寄存器设置、操作模式、电源序列、时序特性等关键参数,为开发者提供了全面的技术指导。 适合人群:具备一定硬件设计基础,从事嵌入式系统开发、显示技术研究的研发人员。 使用场景及目标:①实现应用处理器与MIPI兼容显示屏之间的高速数据传输;②优化显示系统的功耗表现,减少电磁干扰(EMI);③通过灵活配置不同接口模式来适应各种显示设备的需求。 阅读建议:此文档面向具有一定电子工程背景的专业人士,建议读者结合实际项目需求深入理解各章节内容,特别是关于寄存器配置、时序要求等方面的具体说明。对于初次接触此类技术的开发者而言,建议先熟悉基本概念再逐步掌握高级功能的应用方法。

65,210

社区成员

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

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