继续前文,加入list 对象。
1 #include<list>
2 #include<iostream>
3
4 using namespace std;
5
6 struct particle
7 {
8 int id;
9 };
10
11
12 int main()
13 {
14 int count = 4;
15 particle* pp = new particle[count];
16 list<particle> lsp;
17
18 for(int i=0; i<count; i++)
19 {
20 pp[i].id = i+1;
21 lsp.push_back(pp[i]);
22 pp++;
23 }
24
25 for(list<particle>::iterator it=lsp.begin(); it !=lsp.end(); it++)
26 {
27 cout << "current particle id is " << it->id << endl;
28 }
29
30 return 0;
31 }
~
分别使用 apple llvm 5.1(clang-503.0.40) + lldb 平台 和 red hat 4.47-4(gcc) + gdb 平台
lldb运行 :
(lldb) run
Process 8437 launched: '/Users/Desktop/cpp 2014/basic_100/LLDB/PA' (x86_64)
current particle id is 1
current particle id is 1504
Process 8437 stopped
* thread #1: tid = 0xb865b, 0x0000000100001658 PA`main + 984 at ParticleAssignment.cpp:41, queue = 'com.apple.main-thread', stop reason = EXC_BAD_ACCESS (code=1, address=0x1100000012)
frame #0: 0x0000000100001658 PA`main + 984 at ParticleAssignment.cpp:41
gdb运行:
(gdb) run
Starting program: /ifs/user/GNU/PA
current particle id is 1
current particle id is 2
current particle id is 3
current particle id is 4
Program received signal SIGSEGV, Segmentation fault.
0x0000003bfd07b55c in free () from /lib64/libc.so.6
首先,lldb内存溢出检查更严格?输出循环第二步就报错了。redhat gdb 程序结束才返回错误。
两个gdb最终都指向程序的最后一行(cpp:41),但是这行 "return 0;" 不会错啊!
另外,lldb给出 stop reason = exc_bad_access, 程序里是有内存越界访问的;同样check一下 free() from /lib64/libc.so.6,是说“指针越界,析构无法完成”,即,同样指出内存越界访问。
lldb走不下去了,继续在 gdb中调试, 在 cpp:41 往前看,实际修改内存的部分设置断点 ( b 27)
(gdb) b 27
Breakpoint 1 at 0x400b28: file ParticleAssignment.cpp, line 27.
(gdb) run
Starting program: /ifs/user/MPITest/GNU/PA
particle->id 1
particle->id 0
particle->id 2
particle->id 0
这个输出,显然不是想要的。第二个就错了,check list的单元组成, --line21 lsp.push_back(pp[i]);
(gdb) b 22
Breakpoint 1 at 0x400b09: file ParticleAssignment.cpp, line 22.
(gdb) run
Starting program: /ifs/user/zhengjia/MPITest/GNU/PA
particle[i].id 1
Breakpoint 1, main () at ParticleAssignment.cpp:22
22 pp++;
Missing separate debuginfos, use: debuginfo-install glibc-2.12-1.132.el6_5.4.x86_64 libgcc-4.4.7-4.el6.x86_64 libstdc++-4.4.7-4.el6.x86_64
(gdb) print/x *pp
$1 = {id = 0x1}
(gdb) continue
Continuing.
particle[i].id 2
Breakpoint 1, main () at ParticleAssignment.cpp:22
22 pp++;
(gdb) print/x *pp
$2 = {id = 0x0}
(gdb) print/x pp
$3 = 0x602014
(gdb) continue
Continuing.
particle[i].id 3
Breakpoint 1, main () at ParticleAssignment.cpp:22
22 pp++;
(gdb) print/x *pp
$4 = {id = 0x2}
(gdb) print/x pp
$5 = 0x602018
pp->id = i+1;
lsp.push_back(*pp);
pp++;
current particle id is 1
current particle id is 2
current particle id is 3
current particle id is 4
this is a stupid sample, just have fun with gdb, while what about lldb, i even can't find continual breakpoint there, shit !