跪求大神!使用C++ string 时出现了memory corruption!只是简单的把char数组赋值给string而已啊

socusbuf 2015-02-25 11:10:51
废话不多说,在一个函数里出现了上述问题,出现问题的代码如下:
char * newUrl=new char[urlEndPos-urlStartPos+1];
sscanf(urlStartPos,"%[^\"]",newUrl);
string tempUrl=newUrl;

最后会delete newUrl的。

每次运行到string tempUrl=newUrl; 这里就出错,出错的具体信息如下:
*** Error in `/home/zieng/Desktop/spider': malloc(): memory corruption: 0x000000000060a520 ***

Program received signal SIGABRT, Aborted.
0x00007ffff7530cc9 in __GI_raise (sig=sig@entry=6)
at ../nptl/sysdeps/unix/sysv/linux/raise.c:56
56 ../nptl/sysdeps/unix/sysv/linux/raise.c: No such file or directory.
(gdb) bt
#0 0x00007ffff7530cc9 in __GI_raise (sig=sig@entry=6)
at ../nptl/sysdeps/unix/sysv/linux/raise.c:56
#1 0x00007ffff75340d8 in __GI_abort () at abort.c:89
#2 0x00007ffff756df24 in __libc_message (do_abort=1,
fmt=fmt@entry=0x7ffff767c6c8 "*** Error in `%s': %s: 0x%s ***\n")
at ../sysdeps/posix/libc_fatal.c:175
#3 0x00007ffff757bac6 in malloc_printerr (ptr=0x60a520,
str=0x7ffff767883a "malloc(): memory corruption", action=<optimized out>)
at malloc.c:4996
#4 _int_malloc (av=0x7ffff78b9760 <main_arena>, bytes=72) at malloc.c:3447
#5 0x00007ffff757d340 in __GI___libc_malloc (bytes=72) at malloc.c:2891
#6 0x00007ffff7b34f2d in operator new(unsigned long) ()
from /usr/lib/x86_64-linux-gnu/libstdc++.so.6
#7 0x00007ffff7b903b9 in std::string::_Rep::_S_create(unsigned long, unsigned long, std::allocator<char> const&) ()
from /usr/lib/x86_64-linux-gnu/libstdc++.so.6
#8 0x00007ffff7b91ae1 in char* std::string::_S_construct<char const*>(char const*, char const*, std::allocator<char> const&, std::forward_iterator_tag) ()
from /usr/lib/x86_64-linux-gnu/libstdc++.so.6
#9 0x00007ffff7b91ef8 in std::basic_string<char, std::char_traits<char>, std::allocator<char> >::basic_string(char const*, std::allocator<char> const&) ()
from /usr/lib/x86_64-linux-gnu/libstdc++.so.6
#10 0x000000000040297b in html_parse (content=..., imgUrl=...)
at spider_v2.0.cpp:253
#11 0x0000000000402cc4 in BFS (url=...) at spider_v2.0.cpp:312
#12 0x0000000000401e36 in main (argc=1, argv=0x7fffffffdf68)
at spider_v2.0.cpp:78
(gdb) frame 10
#10 0x000000000040297b in html_parse (content=..., imgUrl=...)
at spider_v2.0.cpp:253
253 const string tempUrl=newUrl;
(gdb) p tempUrl
$1 = {static npos = <optimized out>,
_M_dataplus = {<std::allocator<char>> = {<__gnu_cxx::new_allocator<char>> = {<No data fields>}, <No data fields>},
_M_p = 0x7fffffffdd60 "\200\335\377\377\377\177"}}
(gdb) p newUrl
$2 = 0x60a500 "http://s9.qhimg.com/static/8a60aae81b5f422b.css"

可以看到,newUrl是没问题的,但是tempUrl就是npos,而:
npos is a static member constant value with the greatest possible value for an element of type size_t.

This value, when used as the value for a len (or sublen) parameter in string's member functions, means "until the end of the string".

As a return value, it is usually used to indicate no matches.

这是怎么回事呢?
为什么从一个char数组转换string就出问题呢?

...全文
519 13 打赏 收藏 转发到动态 举报
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
Johnblx 2017-10-11
  • 打赏
  • 举报
回复
遇到同样问题。 171 buffer[size] = '\0'; 172 printf("----test [%s]\n", buffer); 173 printf("----test size [%d]\n", strlen(buffer)); 174 string str(buffer); 打印出的数值都没有任何问题
helloworldyu 2015-03-24
  • 打赏
  • 举报
回复
我已经解决了。是之前就已经越界。直到这一句,才检查出,越界问题。程序终止。所以 尝试一下内存检测工具。 例如此处,就检测到在 main.cpp 的53 行。MsgPack.cpp 109 行,AttrPack::Pack() 函数有 2字节的无效写内存问题。 我这里的问题是,有一个 if分支  malloc 分配的内存不够用。希望对你有启发。 valgrind --leak-check=yes ./a.out ==16528== ==16528== 1 errors in context 1 of 3: ==16528== Invalid write of size 2 ==16528== at 0x4017E1: AttrPack::Pack() (MsgPack.cpp:109) ==16528== by 0x403929: main (main.cpp:53)
helloworldyu 2015-03-23
  • 打赏
  • 举报
回复
我也是遇到了这样的问题。
	char tmp[200] = { 0 };
	uint16_t headLen = ATTR_TYPE_LEN + ATTR_LEN_LEN;
	//memset( tmp, 0 , 200 );
	memcpy( tmp, pMsg+headLen , m_attrLen-headLen );

	DEBUGOUT("tmp", tmp);
	DEBUGOUT("len", strlen(tmp));

	m_value = tmp;
楼主解决没哟
ztenv 版主 2015-02-26
  • 打赏
  • 举报
回复
换个编译器试试吧,或使用成员函数来赋值吧;
赵4老师 2015-02-26
  • 打赏
  • 举报
回复
崩溃的时候在弹出的对话框按相应按钮进入调试,按Alt+7键查看Call Stack里面从上到下列出的对应从里层到外层的函数调用历史。双击某一行可将光标定位到此次调用的源代码或汇编指令处,看不懂时双击下一行,直到能看懂为止。
ztenv 版主 2015-02-25
  • 打赏
  • 举报
回复
什么编译器?不能单步进去吗?
socusbuf 2015-02-25
  • 打赏
  • 举报
回复
引用 5 楼 vcmmx 的回复:
string str=NULL
不是因为这个出错的,这个之前也试过了,还是同样的错误
socusbuf 2015-02-25
  • 打赏
  • 举报
回复
引用 4 楼 tangtangtangbaoli 的回复:
int sscanf( string str, string fmt.............. );原型 第一个参数是string 你用的urlStartPos是整型把 后面肯定出错阿
strstr函数返回的是指针,这里应该是没有问题的
socusbuf 2015-02-25
  • 打赏
  • 举报
回复
引用 2 楼 zhao4zhong1 的回复:
小心驶得万年船:
int L=urlEndPos-urlStartPos;
if (L<=0) L=1;
char * newUrl=new char[L+1];
char fmt[20];
sprintf(fmt,"%%%d[^\"]",L);
sscanf(urlStartPos,fmt,newUrl);
string tempUrl=string(newUrl);
有道理,但是问题出在不在这里,加上了这些判断还是会有同样的问题
vcmmx 2015-02-25
  • 打赏
  • 举报
回复
string str=NULL
tangtangtangbaoli 2015-02-25
  • 打赏
  • 举报
回复
int sscanf( string str, string fmt.............. );原型 第一个参数是string 你用的urlStartPos是整型把 后面肯定出错阿
sjlwtf 2015-02-25
  • 打赏
  • 举报
回复
char,string 区分一下这俩吧,两个时代的产物。
赵4老师 2015-02-25
  • 打赏
  • 举报
回复
小心驶得万年船:
int L=urlEndPos-urlStartPos;
if (L<=0) L=1;
char * newUrl=new char[L+1];
char fmt[20];
sprintf(fmt,"%%%d[^\"]",L);
sscanf(urlStartPos,fmt,newUrl);
string tempUrl=string(newUrl);

64,639

社区成员

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

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