Chinaunix首页 | 论坛 | 博客
  • 博客访问: 25910
  • 博文数量: 5
  • 博客积分: 1780
  • 博客等级: 上尉
  • 技术积分: 80
  • 用 户 组: 普通用户
  • 注册时间: 2008-11-28 22:37
文章分类

全部博文(5)

文章存档

2009年(5)

分类: LINUX

2009-05-18 22:46:26

t1.cpp  //C++
//.....................
int main()
{
  int* p;
  int* p1;

  p1=new (std::nothrow)int[-1];  //失败后返回NULL
  if(p1==NULL)
  {
    printf("\r\n p1 error....");
  }

  p = new int[-1];   //失败后抛出异常
  if(p==NULL)  //当失败后实际上执行不到这.
  {
     printf("\r\n p error....");
  }
  return(0);
}


gridserver1:/share/suse linux/root/c # g++ -g t1.cpp

gridserver1:/share/suse linux/root/c # gdb a.out

This GDB was configured as "x86_64-suse-linux"...
Using host libthread_db library "/lib64/libthread_db.so.1".

(gdb) b main
Breakpoint 1 at 0x40083c: file t1.cpp, line 10.
(gdb) disass main   //如下看到,实际上编译器把c/c++源代码中的不同new的用法处理成了不同的调用.
Dump of assembler code for function main:
0x0000000000400834 :    push   %rbp
0x0000000000400835 :    mov    %rsp,%rbp
0x0000000000400838 :    sub    $0x10,%rsp
0x000000000040083c :    mov    $0x500d50,%esi
0x0000000000400841 :   mov    $0xfffffffffffffffc,%rdi
0x0000000000400848 :   callq  0x4006d8 <_ZnamRKSt9nothrow_t@plt>   //失败直接返回NULL的new
0x000000000040084d :   mov    %rax,0xfffffffffffffff8(%rbp)
0x0000000000400851 :   cmpq   $0x0,0xfffffffffffffff8(%rbp)
0x0000000000400856 :   jne    0x400867
0x0000000000400858 :   mov    $0x400988,%edi
0x000000000040085d :   mov    $0x0,%eax
0x0000000000400862 :   callq  0x4006c8
0x0000000000400867 :   mov    $0xfffffffffffffffc,%rdi
0x000000000040086e :   callq  0x4006b8 <_Znam@plt>  //失败抛出异常的new
0x0000000000400873 :   mov    %rax,0xfffffffffffffff0(%rbp)
0x0000000000400877 :   cmpq   $0x0,0xfffffffffffffff0(%rbp)
0x000000000040087c :   jne    0x40088d
0x000000000040087e :   mov    $0x400998,%edi
0x0000000000400883 :   mov    $0x0,%eax
0x0000000000400888 :   callq  0x4006c8
0x000000000040088d :   mov    $0x0,%eax
0x0000000000400892 :   leaveq
0x0000000000400893 :   retq
End of assembler dump.
(gdb) n
The program is not being run.
(gdb) r
Starting program: /share/suse linux/root/c/a.out

Breakpoint 1, main () at t1.cpp:10
10        p1=new (std::nothrow)int[-1];
(gdb) n
11        if(p1==NULL)
(gdb) n
13          printf("\r\n p1 error....");
(gdb) n

16        p = new int[-1];
(gdb) n   
terminate called after throwing an instance of 'std::bad_alloc'
  what():  St9bad_alloc
 p1 error....
Program received signal SIGABRT, Aborted.
0x00002b591b4bcbb5 in raise () from /lib64/libc.so.6
(gdb)


t2.cpp //c++

int main()
{
  int* p;
  int* p1;

  p1=new (std::nothrow)int[-1];
  if(p1==NULL)
  {
    printf("\r\n p1 error....");
  }
try{
    p = new int[-1];
   }catch(const std::bad_alloc&e){
    printf("\r\n p error............");
}
  return(0);
}


gridserver1:/share/suse linux/root/c # g++ -g t2.cpp
gridserver1:/share/suse linux/root/c # gdb a.out

This GDB was configured as "x86_64-suse-linux"...
Using host libthread_db library "/lib64/libthread_db.so.1".

(gdb) disass main
Dump of assembler code for function main:
0x00000000004009a4 :    push   %rbp
0x00000000004009a5 :    mov    %rsp,%rbp
0x00000000004009a8 :    push   %rbx
0x00000000004009a9 :    sub    $0x28,%rsp
0x00000000004009ad :    mov    $0x500f58,%esi
0x00000000004009b2 :   mov    $0xfffffffffffffffc,%rdi
0x00000000004009b9 :   callq  0x400838 <_ZnamRKSt9nothrow_t@plt> //new
0x00000000004009be :   mov    %rax,0xffffffffffffffe8(%rbp)
0x00000000004009c2 :   cmpq   $0x0,0xffffffffffffffe8(%rbp)
0x00000000004009c7 :   jne    0x4009d8
0x00000000004009c9 :   mov    $0x400b38,%edi
0x00000000004009ce :   mov    $0x0,%eax
0x00000000004009d3 :   callq  0x400818
0x00000000004009d8 :   mov    $0xfffffffffffffffc,%rdi
0x00000000004009df :   callq  0x400808 <_Znam@plt>
0x00000000004009e4 :   mov    %rax,0xffffffffffffffe0(%rbp)
0x00000000004009e8 :   jmp    0x400a3a
0x00000000004009ea :   mov    %rax,0xffffffffffffffd0(%rbp)
0x00000000004009ee :   cmp    $0x1,%rdx
0x00000000004009f2 :   je     0x4009fd
0x00000000004009f4 :   mov    0xffffffffffffffd0(%rbp),%rdi
0x00000000004009f8 :   callq  0x400848 <_Unwind_Resume@plt>
0x00000000004009fd :   mov    0xffffffffffffffd0(%rbp),%rdi
0x0000000000400a01 :   callq  0x400828 <__cxa_begin_catch@plt>
0x0000000000400a06 :   mov    %rax,0xfffffffffffffff0(%rbp)
0x0000000000400a0a :  mov    $0x400b48,%edi
0x0000000000400a0f :  mov    $0x0,%eax
0x0000000000400a14 :  callq  0x400818
0x0000000000400a19 :  callq  0x4007d8 <__cxa_end_catch@plt>
0x0000000000400a1e :  jmp    0x400a3a
0x0000000000400a20 :  mov    %rax,0xffffffffffffffd0(%rbp)
0x0000000000400a24 :  mov    0xffffffffffffffd0(%rbp),%rbx
0x0000000000400a28 :  callq  0x4007d8 <__cxa_end_catch@plt>
0x0000000000400a2d :  mov    %rbx,0xffffffffffffffd0(%rbp)
0x0000000000400a31 :  mov    0xffffffffffffffd0(%rbp),%rdi
0x0000000000400a35 :  callq  0x400848 <_Unwind_Resume@plt>
0x0000000000400a3a :  mov    $0x0,%eax
0x0000000000400a3f :  add    $0x28,%rsp
0x0000000000400a43 :  pop    %rbx
0x0000000000400a44 :  leaveq
0x0000000000400a45 :  retq
End of assembler dump.

(gdb) b main
Breakpoint 1 at 0x4009ad: file t2.cpp, line 10.
(gdb) n
The program is not being run.
(gdb) r
Starting program: /share/suse linux/root/c/a.out

Breakpoint 1, main () at t2.cpp:10
10        p1=new (std::nothrow)int[-1];
(gdb) n
11        if(p1==NULL)
(gdb) n
13          printf("\r\n p1 error....");
(gdb) n

16          p = new int[-1];
(gdb) n
 p1 error....
 p error............
Program exited normally.
(gdb)

阅读(2474) | 评论(1) | 转发(0) |
0

上一篇:没有了

下一篇:经典爱情短信

给主人留下些什么吧!~~

chinaunix网友2009-11-14 02:05:32

windows下也会是这样吗?