免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 1511 | 回复: 0

new的用法(编译器细节决定编程细节的一个小例子). [复制链接]

论坛徽章:
0
发表于 2009-05-18 22:46 |显示全部楼层
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    //失败直接返回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   //失败抛出异常的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  //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
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
0x00000000004009fd :   mov    0xffffffffffffffd0(%rbp),%rdi
0x0000000000400a01 :   callq  0x400828
0x0000000000400a06 :   mov    %rax,0xfffffffffffffff0(%rbp)
0x0000000000400a0a :  mov    $0x400b48,%edi
0x0000000000400a0f :  mov    $0x0,%eax
0x0000000000400a14 :  callq  0x400818
0x0000000000400a19 :  callq  0x4007d8
0x0000000000400a1e :  jmp    0x400a3a
0x0000000000400a20 :  mov    %rax,0xffffffffffffffd0(%rbp)
0x0000000000400a24 :  mov    0xffffffffffffffd0(%rbp),%rbx
0x0000000000400a28 :  callq  0x4007d8
0x0000000000400a2d :  mov    %rbx,0xffffffffffffffd0(%rbp)
0x0000000000400a31 :  mov    0xffffffffffffffd0(%rbp),%rdi
0x0000000000400a35 :  callq  0x400848  
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)
               
               
               

本文来自ChinaUnix博客,如果查看原文请点:http://blog.chinaunix.net/u2/86872/showart_1932496.html
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP