[C++源码] #include using namespace std; class MyClass { public: MyClass(){ data1 =1; data2 = 2;}; ~MyClass(){}; int data1; int data2; void print() { cout << "hello! data1: " << data1 << " data2: " << data2 << endl; } virtual void test1() { data2 = 3; } }; int main() { MyClass * pMyClass; pMyClass = new MyClass; cout << "sizeof(MyClass) = " << sizeof(MyClass) << endl; pMyClass->test1(); pMyClass->print(); // 1: 正确调用 pMyClass[0].print(); // 2: 正确调用 pMyClass[1].print(); // 3: 错误调用, 但结果正确 pMyClass[10000000].print(); // 4: 错误调用, 但结果正确 return 0; } [ASM源码] .file "testfunc.cpp" .section .ctors,"aw",@progbits .align 4 .long _GLOBAL__I_main .section .gnu.linkonce.t._ZN7MyClassC1Ev,"ax",@progbits .align 2 .weak _ZN7MyClassC1Ev .type _ZN7MyClassC1Ev, @function _ZN7MyClassC1Ev: .LFB1423: pushl %ebp .LCFI0: movl %esp, %ebp .LCFI1: movl $_ZTV7MyClass+8, %edx movl 8(%ebp), %eax movl %edx, (%eax) movl 8(%ebp), %eax movl $1, 4(%eax) movl 8(%ebp), %eax movl $2, 8(%eax) popl %ebp ret .LFE1423: .size _ZN7MyClassC1Ev, .-_ZN7MyClassC1Ev .globl __gxx_personality_v0 .section .gnu.linkonce.t._ZN7MyClass5test1Ev,"ax",@progbits .align 2 .weak _ZN7MyClass5test1Ev .type _ZN7MyClass5test1Ev, @function _ZN7MyClass5test1Ev: .LFB1428: pushl %ebp .LCFI2: movl %esp, %ebp .LCFI3: movl 8(%ebp), %eax movl $3, 8(%eax) popl %ebp ret .LFE1428: .size _ZN7MyClass5test1Ev, .-_ZN7MyClass5test1Ev .section .gnu.linkonce.t._ZSt3minIjERKT_S2_S2_,"ax",@progbits .align 2 .weak _ZSt3minIjERKT_S2_S2_ .type _ZSt3minIjERKT_S2_S2_, @function _ZSt3minIjERKT_S2_S2_: .LFB1430: pushl %ebp .LCFI4: movl %esp, %ebp .LCFI5: subl $4, %esp .LCFI6: movl 12(%ebp), %eax movl (%eax), %edx movl 8(%ebp), %eax movl (%eax), %eax cmpl %eax, %edx jae .L6 movl 12(%ebp), %eax movl %eax, -4(%ebp) jmp .L8 .L6: movl 8(%ebp), %eax movl %eax, -4(%ebp) .L8: movl -4(%ebp), %eax leave ret .LFE1430: .size _ZSt3minIjERKT_S2_S2_, .-_ZSt3minIjERKT_S2_S2_ .text .align 2 .type _ZSt17__verify_groupingPKcjRKSs, @function _ZSt17__verify_groupingPKcjRKSs: .LFB1342: pushl %ebp .LCFI7: movl %esp, %ebp .LCFI8: pushl %ebx .LCFI9: subl $52, %esp .LCFI10: movl 16(%ebp), %eax movl %eax, (%esp) call _ZNKSs4sizeEv subl $1, %eax movl %eax, -28(%ebp) movl 12(%ebp), %eax subl $1, %eax movl %eax, -24(%ebp) leal -24(%ebp), %eax movl %eax, 4(%esp) leal -28(%ebp), %eax movl %eax, (%esp) call _ZSt3minIjERKT_S2_S2_ movl (%eax), %eax movl %eax, -20(%ebp) movl -28(%ebp), %eax movl %eax, -16(%ebp) movb $1, -9(%ebp) movl $0, -8(%ebp) jmp .L11 .L12: movl -16(%ebp), %eax movl %eax, 4(%esp) movl 16(%ebp), %eax movl %eax, (%esp) call _ZNKSsixEj movzbl (%eax), %edx movl -8(%ebp), %eax addl 8(%ebp), %eax movzbl (%eax), %eax cmpb %al, %dl sete %al movb %al, -9(%ebp) subl $1, -16(%ebp) addl $1, -8(%ebp) .L11: movl -8(%ebp), %eax cmpl -20(%ebp), %eax jae .L15 cmpb $0, -9(%ebp) jne .L12 jmp .L15 .L16: movl -16(%ebp), %eax movl %eax, 4(%esp) movl 16(%ebp), %eax movl %eax, (%esp) call _ZNKSsixEj movzbl (%eax), %edx movl -20(%ebp), %eax addl 8(%ebp), %eax movzbl (%eax), %eax cmpb %al, %dl sete %al movb %al, -9(%ebp) subl $1, -16(%ebp) .L15: cmpl $0, -16(%ebp) je .L17 cmpb $0, -9(%ebp) jne .L16 .L17: movl -20(%ebp), %eax addl 8(%ebp), %eax movzbl (%eax), %eax testb %al, %al jle .L19 movzbl -9(%ebp), %ebx movl $0, 4(%esp) movl 16(%ebp), %eax movl %eax, (%esp) call _ZNKSsixEj movzbl (%eax), %edx movl -20(%ebp), %eax addl 8(%ebp), %eax movzbl (%eax), %eax cmpb %al, %dl setle %al movzbl %al, %eax andl %ebx, %eax testl %eax, %eax setne %al movb %al, -9(%ebp) .L19: movzbl -9(%ebp), %eax addl $52, %esp popl %ebx popl %ebp ret .LFE1342: .size _ZSt17__verify_groupingPKcjRKSs, .-_ZSt17__verify_groupingPKcjRKSs .align 2 .type _Z41__static_initialization_and_destruction_0ii, @function _Z41__static_initialization_and_destruction_0ii: .LFB1460: pushl %ebp .LCFI11: movl %esp, %ebp .LCFI12: subl $24, %esp .LCFI13: cmpl $1, 8(%ebp) jne .L26 cmpl $65535, 12(%ebp) jne .L26 movl $_ZSt8__ioinit, (%esp) call _ZNSt8ios_base4InitC1Ev movl $__dso_handle, 8(%esp) movl $0, 4(%esp) movl $__tcf_0, (%esp) call __cxa_atexit .L26: leave ret .LFE1460: .size _Z41__static_initialization_and_destruction_0ii, .-_Z41__static_initialization_and_destruction_0ii .align 2 .type _GLOBAL__I_main, @function _GLOBAL__I_main: .LFB1462: pushl %ebp .LCFI14: movl %esp, %ebp .LCFI15: subl $8, %esp .LCFI16: movl $65535, 4(%esp) movl $1, (%esp) call _Z41__static_initialization_and_destruction_0ii leave ret .LFE1462: .size _GLOBAL__I_main, .-_GLOBAL__I_main .align 2 .type __tcf_0, @function __tcf_0: .LFB1461: pushl %ebp .LCFI17: movl %esp, %ebp .LCFI18: subl $8, %esp .LCFI19: movl $_ZSt8__ioinit, (%esp) call _ZNSt8ios_base4InitD1Ev leave ret .LFE1461: .size __tcf_0, .-__tcf_0 .section .rodata .LC0: .string "hello! data1: " .LC1: .string " data2: " .section .gnu.linkonce.t._ZN7MyClass5printEv,"ax",@progbits .align 2 .weak _ZN7MyClass5printEv .type _ZN7MyClass5printEv, @function _ZN7MyClass5printEv: .LFB1427: pushl %ebp .LCFI20: movl %esp, %ebp .LCFI21: pushl %esi .LCFI22: pushl %ebx .LCFI23: subl $16, %esp .LCFI24: movl 8(%ebp), %eax movl 8(%eax), %esi movl 8(%ebp), %eax movl 4(%eax), %ebx movl $.LC0, 4(%esp) movl $_ZSt4cout, (%esp) call _ZStlsISt11char_traitsIcEERSt13basic_ostreamIcT_ES5_PKc movl %ebx, 4(%esp) movl %eax, (%esp) call _ZNSolsEi movl $.LC1, 4(%esp) movl %eax, (%esp) call _ZStlsISt11char_traitsIcEERSt13basic_ostreamIcT_ES5_PKc movl %esi, 4(%esp) movl %eax, (%esp) call _ZNSolsEi movl $_ZSt4endlIcSt11char_traitsIcEERSt13basic_ostreamIT_T0_ES6_, 4(%esp) movl %eax, (%esp) call _ZNSolsEPFRSoS_E addl $16, %esp popl %ebx popl %esi popl %ebp ret .LFE1427: .size _ZN7MyClass5printEv, .-_ZN7MyClass5printEv .section .rodata .LC2: .string "sizeof(MyClass) = " .text .align 2 .globl main .type main, @function main: .LFB1429: leal 4(%esp), %ecx .LCFI25: andl $-16, %esp pushl -4(%ecx) .LCFI26: pushl %ebp .LCFI27: movl %esp, %ebp .LCFI28: pushl %ebx .LCFI29: pushl %ecx .LCFI30: subl $32, %esp .LCFI31: movl $12, (%esp) call _Znwj movl %eax, %ebx movl %ebx, (%esp) call _ZN7MyClassC1Ev movl %ebx, -12(%ebp) movl $.LC2, 4(%esp) movl $_ZSt4cout, (%esp) call _ZStlsISt11char_traitsIcEERSt13basic_ostreamIcT_ES5_PKc movl $12, 4(%esp) movl %eax, (%esp) call _ZNSolsEj movl $_ZSt4endlIcSt11char_traitsIcEERSt13basic_ostreamIT_T0_ES6_, 4(%esp) movl %eax, (%esp) call _ZNSolsEPFRSoS_E movl -12(%ebp), %eax movl (%eax), %eax movl (%eax), %edx movl -12(%ebp), %eax movl %eax, (%esp) call *%edx movl -12(%ebp), %eax movl %eax, (%esp) call _ZN7MyClass5printEv movl -12(%ebp), %eax movl %eax, (%esp) call _ZN7MyClass5printEv movl -12(%ebp), %eax addl $12, %eax movl %eax, (%esp) call _ZN7MyClass5printEv movl -12(%ebp), %eax addl $120000000, %eax movl %eax, (%esp) call _ZN7MyClass5printEv movl $0, %eax addl $32, %esp popl %ecx popl %ebx popl %ebp leal -4(%ecx), %esp ret .LFE1429: .size main, .-main .local _ZSt8__ioinit .comm _ZSt8__ioinit,1,1 .weak _ZTV7MyClass .section .gnu.linkonce.r._ZTV7MyClass,"a",@progbits .align 8 .type _ZTV7MyClass, @object .size _ZTV7MyClass, 12 _ZTV7MyClass: .long 0 .long _ZTI7MyClass .long _ZN7MyClass5test1Ev .weak _ZTI7MyClass .section .gnu.linkonce.r._ZTI7MyClass,"a",@progbits .align 4 .type _ZTI7MyClass, @object .size _ZTI7MyClass, 8 _ZTI7MyClass: .long _ZTVN10__cxxabiv117__class_type_infoE+8 .long _ZTS7MyClass .weak _ZTS7MyClass .section .gnu.linkonce.r._ZTS7MyClass,"a",@progbits .type _ZTS7MyClass, @object .size _ZTS7MyClass, 9 _ZTS7MyClass: .string "7MyClass" .section .eh_frame,"a",@progbits .Lframe1: .long .LECIE1-.LSCIE1 .LSCIE1: .long 0x0 .byte 0x1 .string "zP" .uleb128 0x1 .sleb128 -4 .byte 0x8 .uleb128 0x5 .byte 0x0 .long __gxx_personality_v0 .byte 0xc .uleb128 0x4 .uleb128 0x4 .byte 0x88 .uleb128 0x1 .align 4 .LECIE1: .LSFDE7: .long .LEFDE7-.LASFDE7 .LASFDE7: .long .LASFDE7-.Lframe1 .long .LFB1342 .long .LFE1342-.LFB1342 .uleb128 0x0 .byte 0x4 .long .LCFI7-.LFB1342 .byte 0xe .uleb128 0x8 .byte 0x85 .uleb128 0x2 .byte 0x4 .long .LCFI8-.LCFI7 .byte 0xd .uleb128 0x5 .byte 0x4 .long .LCFI10-.LCFI8 .byte 0x83 .uleb128 0x3 .align 4 .LEFDE7: .LSFDE9: .long .LEFDE9-.LASFDE9 .LASFDE9: .long .LASFDE9-.Lframe1 .long .LFB1460 .long .LFE1460-.LFB1460 .uleb128 0x0 .byte 0x4 .long .LCFI11-.LFB1460 .byte 0xe .uleb128 0x8 .byte 0x85 .uleb128 0x2 .byte 0x4 .long .LCFI12-.LCFI11 .byte 0xd .uleb128 0x5 .align 4 .LEFDE9: .LSFDE11: .long .LEFDE11-.LASFDE11 .LASFDE11: .long .LASFDE11-.Lframe1 .long .LFB1462 .long .LFE1462-.LFB1462 .uleb128 0x0 .byte 0x4 .long .LCFI14-.LFB1462 .byte 0xe .uleb128 0x8 .byte 0x85 .uleb128 0x2 .byte 0x4 .long .LCFI15-.LCFI14 .byte 0xd .uleb128 0x5 .align 4 .LEFDE11: .LSFDE13: .long .LEFDE13-.LASFDE13 .LASFDE13: .long .LASFDE13-.Lframe1 .long .LFB1461 .long .LFE1461-.LFB1461 .uleb128 0x0 .byte 0x4 .long .LCFI17-.LFB1461 .byte 0xe .uleb128 0x8 .byte 0x85 .uleb128 0x2 .byte 0x4 .long .LCFI18-.LCFI17 .byte 0xd .uleb128 0x5 .align 4 .LEFDE13: .LSFDE15: .long .LEFDE15-.LASFDE15 .LASFDE15: .long .LASFDE15-.Lframe1 .long .LFB1427 .long .LFE1427-.LFB1427 .uleb128 0x0 .byte 0x4 .long .LCFI20-.LFB1427 .byte 0xe .uleb128 0x8 .byte 0x85 .uleb128 0x2 .byte 0x4 .long .LCFI21-.LCFI20 .byte 0xd .uleb128 0x5 .byte 0x4 .long .LCFI24-.LCFI21 .byte 0x83 .uleb128 0x4 .byte 0x86 .uleb128 0x3 .align 4 .LEFDE15: .LSFDE17: .long .LEFDE17-.LASFDE17 .LASFDE17: .long .LASFDE17-.Lframe1 .long .LFB1429 .long .LFE1429-.LFB1429 .uleb128 0x0 .byte 0x4 .long .LCFI25-.LFB1429 .byte 0xc .uleb128 0x1 .uleb128 0x0 .byte 0x9 .uleb128 0x4 .uleb128 0x1 .byte 0x4 .long .LCFI26-.LCFI25 .byte 0xc .uleb128 0x4 .uleb128 0x4 .byte 0x4 .long .LCFI27-.LCFI26 .byte 0xe .uleb128 0x8 .byte 0x85 .uleb128 0x2 .byte 0x4 .long .LCFI28-.LCFI27 .byte 0xd .uleb128 0x5 .byte 0x4 .long .LCFI30-.LCFI28 .byte 0x84 .uleb128 0x4 .byte 0x83 .uleb128 0x3 .align 4 .LEFDE17: .ident "GCC: (GNU) 4.2.0 20060617 (experimental)" .section .note.GNU-stack,"",@progbits