由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
Programming版 - 这段C++代码有啥问题
相关主题
C++一个string的小问题c++ string 一问
问一个C++ set和unordered_set iterator的问题关于 exception 的一个问题
a string define question (c++)一个popen加gzip的问题
C++ Strategies and Tactics 书上一个问题求助为啥gcc找不到类的构造函数?
typedef basic_string string;一个c++小问题
请教boost::any compile错误。请问以下代码有什么错误?
c++ iterator 弱问compile error
C++ syntax question内存管理的问题
相关话题的讨论汇总
话题: lib话题: string话题: int2strmap话题: libstd话题: usr
进入Programming版参与讨论
1 (共1页)
e*********k
发帖数: 12
1
下面的代码有时候会crash在LINE_A或LINE_B这两行,哪儿不对?
class A{
//...
map m_int2StrMap;
//...
void problematic(){
//...insert and erase of m_int2StrMap are involved
char temp[6] = {0};
int key = 12345;
//...temp's elements and key might be changed
map::iterator iter = this->m_int2StrMap.find(key);
if(iter != this->m_int2StrMap.end())
{
/*LINE_A*/ this->m_int2StrMap.erase(iter);
}
/*LINE_B*/ this->m_int2StrMap.insert(pair(key,
string ((const char*)temp, 6)));
//...
}
//...
}
用gdb bt coredump,结果如下:
#0 0x00ae0410 in __kernel_vsyscall ()
#1 0x00138df0 in vfprintf () from /lib/libc.so.6
#2 0x0013a701 in vfprintf () from /lib/libc.so.6
#3 0x0017128b in __wcstod_internal () from /lib/libc.so.6
#4 0x00179595 in ____wcstof_l_internal () from /lib/libc.so.6
#5 0x001799d9 in ____wcstof_l_internal () from /lib/libc.so.6
#6 0x05bfd581 in operator delete () from /usr/lib/libstdc++.so.6
#7 0x05bda14d in std::string::_Rep::_M_destroy () from /usr/lib/libstdc++
.so.6
#8 0x08084bf7 in problematic() at /usr/lib/gcc/i386-redhat-linux/4.1.2/..
/../../../include/c++/4.1.2/bits/basic_string.h:233
或者
#0 0x00b81410 in __kernel_vsyscall ()
#1 0x009ccdf0 in vfprintf () from /lib/libc.so.6
#2 0x009ce701 in vfprintf () from /lib/libc.so.6
#3 0x00a0528b in __wcstod_internal () from /lib/libc.so.6
#4 0x00a0e250 in ____wcstof_l_internal () from /lib/libc.so.6
#5 0x00a0fd87 in wcsxfrm_l () from /lib/libc.so.6
#6 0x05bfeab7 in operator new () from /usr/lib/libstdc++.so.6
#7 0x05bda0fb in std::string::_Rep::_S_create () from /usr/lib/libstdc++.
so.6
#8 0x05bdaef5 in ?? () from /usr/lib/libstdc++.so.6
#9 0x05bdaff1 in std::basic_string, std::
allocator >::basic_string () from /usr/lib/libstdc++.so.6
#10 0x080843e4 in problematic() at bad.cpp:LINE_B
N******K
发帖数: 10202
2
多线程?

【在 e*********k 的大作中提到】
: 下面的代码有时候会crash在LINE_A或LINE_B这两行,哪儿不对?
: class A{
: //...
: map m_int2StrMap;
: //...
: void problematic(){
: //...insert and erase of m_int2StrMap are involved
: char temp[6] = {0};
: int key = 12345;
: //...temp's elements and key might be changed

c*******u
发帖数: 1269
3
Make pair

★ 发自iPhone App: ChineseWeb 8.7

【在 e*********k 的大作中提到】
: 下面的代码有时候会crash在LINE_A或LINE_B这两行,哪儿不对?
: class A{
: //...
: map m_int2StrMap;
: //...
: void problematic(){
: //...insert and erase of m_int2StrMap are involved
: char temp[6] = {0};
: int key = 12345;
: //...temp's elements and key might be changed

n*****u
发帖数: 465
4
加个 string 怎么弄得那么别扭?

【在 e*********k 的大作中提到】
: 下面的代码有时候会crash在LINE_A或LINE_B这两行,哪儿不对?
: class A{
: //...
: map m_int2StrMap;
: //...
: void problematic(){
: //...insert and erase of m_int2StrMap are involved
: char temp[6] = {0};
: int key = 12345;
: //...temp's elements and key might be changed

k**********g
发帖数: 989
5
Please allocate an extra char element for temp (i.e. increase its length by
1), and set it to 0. It doesn't hurt to do so.
In other words, make temp length 7, but pass in the same value of 6 to the
string constructor.
Based on just the code shown above, this suggestion probably shouldn't
matter, because std::string::string(const char*, size_t count) should copy
exactly "count" characters, and should not read any bytes beyond that.
http://www.cplusplus.com/reference/string/string/string/
However, the characters in temp will not necessarily be null-terminated.
This would be the case if you set all 6 elements to non-zero values. If you
pass temp into any function that expects null-terminated string, this will
crash of course.
A*********l
发帖数: 2005
6
如果是多线程的话,在两个线程里同时作map deletion/insersion 就会出问题,

【在 e*********k 的大作中提到】
: 下面的代码有时候会crash在LINE_A或LINE_B这两行,哪儿不对?
: class A{
: //...
: map m_int2StrMap;
: //...
: void problematic(){
: //...insert and erase of m_int2StrMap are involved
: char temp[6] = {0};
: int key = 12345;
: //...temp's elements and key might be changed

1 (共1页)
进入Programming版参与讨论
相关主题
内存管理的问题typedef basic_string string;
make 时候遇到 undefined reference 怎么办?请教boost::any compile错误。
Pointer to iterator?c++ iterator 弱问
Question about C++ faqC++ syntax question
C++一个string的小问题c++ string 一问
问一个C++ set和unordered_set iterator的问题关于 exception 的一个问题
a string define question (c++)一个popen加gzip的问题
C++ Strategies and Tactics 书上一个问题求助为啥gcc找不到类的构造函数?
相关话题的讨论汇总
话题: lib话题: string话题: int2strmap话题: libstd话题: usr