Onelong

分享知识,与你一起进步......
RSS icon Home icon
  • c++ 析构函数

    post by onelong / 2012-12-22 17:51 Saturday [C++]

    c++代码:

     

    class Object

    {

    private:

    int num;

    public:

    Object()

    {

    num = 50;

    }

     

    int getNum()

    {

    return num;

    }

     

    ~Object()

    {

    num = 0;

    }

    }; 

     

    int main()

    {

    Object *p = new Object();

    p->getNum();

    delete p;//注意,不写这个默认是不会调用析构函数

    return 0;

    }

    汇编代码:

     

    .arch armv4t

    .fpu softvfp

    .eabi_attribute 20, 1

    .eabi_attribute 21, 1

    .eabi_attribute 23, 3

    .eabi_attribute 24, 1

    .eabi_attribute 25, 1

    .eabi_attribute 26, 2

    .eabi_attribute 30, 6

    .eabi_attribute 18, 4

    .file "person.cpp"

    .section .text._ZN6ObjectC1Ev,"axG",%progbits,_ZN6ObjectC1Ev,comdat

    .align 2

    .weak _ZN6ObjectC1Ev

    .type _ZN6ObjectC1Ev, %function

    _ZN6ObjectC1Ev:

    .fnstart

    .LFB2:

    @ Function supports interworking.

    @ args = 0, pretend = 0, frame = 8

    @ frame_needed = 1, uses_anonymous_args = 0

    @ link register save eliminated.

    str fp, [sp, #-4]!

    .LCFI0:

    add fp, sp, #0

    .LCFI1:

    sub sp, sp, #12

    .LCFI2:

    str r0, [fp, #-8]

    ldr r3, [fp, #-8]

    mov r2, #50

    str r2, [r3, #0]

    ldr r3, [fp, #-8]

    mov r0, r3

    add sp, fp, #0

    ldmfd sp!, {fp}

    bx lr

    .LFE2:

    .cantunwind

    .fnend

    .size _ZN6ObjectC1Ev, .-_ZN6ObjectC1Ev

    .section .text._ZN6Object6getNumEv,"axG",%progbits,_ZN6Object6getNumEv,comdat

    .align 2

    .weak _ZN6Object6getNumEv

    .type _ZN6Object6getNumEv, %function

    _ZN6Object6getNumEv:

    .fnstart

    .LFB3:

    @ Function supports interworking.

    @ args = 0, pretend = 0, frame = 8

    @ frame_needed = 1, uses_anonymous_args = 0

    @ link register save eliminated.

    str fp, [sp, #-4]!

    .LCFI3:

    add fp, sp, #0

    .LCFI4:

    sub sp, sp, #12

    .LCFI5:

    str r0, [fp, #-8]

    ldr r3, [fp, #-8]

    ldr r3, [r3, #0]

    mov r0, r3

    add sp, fp, #0

    ldmfd sp!, {fp}

    bx lr

    .LFE3:

    .cantunwind

    .fnend

    .size _ZN6Object6getNumEv, .-_ZN6Object6getNumEv

    .section .text._ZN6ObjectD1Ev,"axG",%progbits,_ZN6ObjectD1Ev,comdat

    .align 2

    .weak _ZN6ObjectD1Ev

    .type _ZN6ObjectD1Ev, %function

    _ZN6ObjectD1Ev:

    .fnstart

    .LFB6:

    @ Function supports interworking.

    @ args = 0, pretend = 0, frame = 8

    @ frame_needed = 1, uses_anonymous_args = 0

    @ link register save eliminated.

    str fp, [sp, #-4]!

    .LCFI6:

    add fp, sp, #0

    .LCFI7:

    sub sp, sp, #12

    .LCFI8:

    str r0, [fp, #-8]

    ldr r3, [fp, #-8]

    mov r2, #0

    str r2, [r3, #0]

    ldr r3, [fp, #-8]

    mov r0, r3

    add sp, fp, #0

    ldmfd sp!, {fp}

    bx lr

    .LFE6:

    .cantunwind

    .fnend

    .size _ZN6ObjectD1Ev, .-_ZN6ObjectD1Ev

    .text

    .align 2

    .global main

    .type main, %function

    main:

    .fnstart

    .LFB7:

    @ Function supports interworking.

    @ args = 0, pretend = 0, frame = 8

    @ frame_needed = 1, uses_anonymous_args = 0

    stmfd sp!, {r4, fp, lr}

    .save {r4, fp, lr}

    .LCFI9:

    .setfp fp, sp, #8

    add fp, sp, #8

    .LCFI10:

    .pad #12

    sub sp, sp, #12

    .LCFI11:

    mov r0, #4

    bl _Znwj //申请内存

    mov r3, r0

    mov r4, r3

    mov r3, r4

    mov r0, r3

    bl _ZN6ObjectC1Ev //调用构造函数

    str r4, [fp, #-16]

    ldr r0, [fp, #-16]

    bl _ZN6Object6getNumEv //

    ldr r4, [fp, #-16]

    cmp r4, #0

    beq .L11

    .L12:

    .L10:

    mov r0, r4

    bl _ZN6ObjectD1Ev //调用析构函数

    mov r0, r4

    bl _ZdlPv //释放内存

    .L11:

    mov r3, #0

    mov r0, r3

    sub sp, fp, #8

    ldmfd sp!, {r4, fp, lr}

    bx lr

    .LFE7:

    .fnend

    .size main, .-main

    .ident "GCC: (ctng-1.6.1) 4.4.3"

    .section .note.GNU-stack,"",%progbits

    关于析构函数,不是必须的。当代码里面有实现析构函数的时候,delete对象的时候,会先调用析构函数,然后再释放内存。如果没有实现析构函数的话,就不会调用析构函数了,这点和构造函数不同,是类就有构造函数。

    引用地址:
     

    我要评论