Why are C and C++ different even after compilation? -
i guessed still surprised see output of these 2 programs, written in c , c++, when compiled different. makes me think concept of objects still exist @ lowest level. add overhead? if impossible optimization convert object oriented code procedural style or hard do?
helloworld.c
#include <stdio.h> int main(void) { printf("hello world!\n"); return 0; }
helloworld.cpp
#include <iostream> int main() { std::cout << "hello world!" << std::endl; return 0; }
compiled this:
gcc helloworld.cpp -o hwcpp.s -s -o2 gcc helloworld.c -o hwc.s -s -o2
produced code:
c assembly
.file "helloworld.c" .section .rodata.str1.1,"ams",@progbits,1 .lc0: .string "hello world!\n" .text .p2align 4,,15 .globl main .type main, @function main: pushl %ebp movl %esp, %ebp andl $-16, %esp subl $16, %esp movl $.lc0, 4(%esp) movl $1, (%esp) call __printf_chk xorl %eax, %eax leave ret .size main, .-main .ident "gcc: (ubuntu 4.4.3-4ubuntu5) 4.4.3" .section .note.gnu-stack,"",@progbits
c++ assembly
.file "helloworld.cpp" .text .p2align 4,,15 .type _global__i_main, @function _global__i_main: .lfb1007: .cfi_startproc .cfi_personality 0x0,__gxx_personality_v0 pushl %ebp .cfi_def_cfa_offset 8 movl %esp, %ebp .cfi_offset 5, -8 .cfi_def_cfa_register 5 subl $24, %esp movl $_zstl8__ioinit, (%esp) call _znst8ios_base4initc1ev movl $__dso_handle, 8(%esp) movl $_zstl8__ioinit, 4(%esp) movl $_znst8ios_base4initd1ev, (%esp) call __cxa_atexit leave ret .cfi_endproc .lfe1007: .size _global__i_main, .-_global__i_main .section .ctors,"aw",@progbits .align 4 .long _global__i_main .section .rodata.str1.1,"ams",@progbits,1 .lc0: .string "hello world!" .text .p2align 4,,15 .globl main .type main, @function main: .lfb997: .cfi_startproc .cfi_personality 0x0,__gxx_personality_v0 pushl %ebp .cfi_def_cfa_offset 8 movl %esp, %ebp .cfi_offset 5, -8 .cfi_def_cfa_register 5 andl $-16, %esp pushl %ebx subl $28, %esp movl $12, 8(%esp) movl $.lc0, 4(%esp) movl $_zst4cout, (%esp) .cfi_escape 0x10,0x3,0x7,0x55,0x9,0xf0,0x1a,0x9,0xfc,0x22 call _zst16__ostream_inserticst11char_traitsiceerst13basic_ostreamit_t0_es6_pks3_i movl _zst4cout, %eax movl -12(%eax), %eax movl _zst4cout+124(%eax), %ebx testl %ebx, %ebx je .l9 cmpb $0, 28(%ebx) je .l5 movzbl 39(%ebx), %eax .l6: movsbl %al,%eax movl %eax, 4(%esp) movl $_zst4cout, (%esp) call _znso3putec movl %eax, (%esp) call _znso5flushev addl $28, %esp xorl %eax, %eax popl %ebx movl %ebp, %esp popl %ebp ret .p2align 4,,7 .p2align 3 .l5: movl %ebx, (%esp) call _znkst5ctypeice13_m_widen_initev movl (%ebx), %eax movl $10, 4(%esp) movl %ebx, (%esp) call *24(%eax) jmp .l6 .l9: call _zst16__throw_bad_castv .cfi_endproc .lfe997: .size main, .-main .local _zstl8__ioinit .comm _zstl8__ioinit,1,1 .weakref _zl20__gthrw_pthread_oncepipfvve,pthread_once .weakref _zl27__gthrw_pthread_getspecificj,pthread_getspecific .weakref _zl27__gthrw_pthread_setspecificjpkv,pthread_setspecific .weakref _zl22__gthrw_pthread_createpmpk14pthread_attr_tpfpvs3_es3_,pthread_create .weakref _zl20__gthrw_pthread_joinmppv,pthread_join .weakref _zl21__gthrw_pthread_equalmm,pthread_equal .weakref _zl20__gthrw_pthread_selfv,pthread_self .weakref _zl22__gthrw_pthread_detachm,pthread_detach .weakref _zl22__gthrw_pthread_cancelm,pthread_cancel .weakref _zl19__gthrw_sched_yieldv,sched_yield .weakref _zl26__gthrw_pthread_mutex_lockp15pthread_mutex_t,pthread_mutex_lock .weakref _zl29__gthrw_pthread_mutex_trylockp15pthread_mutex_t,pthread_mutex_trylock .weakref _zl31__gthrw_pthread_mutex_timedlockp15pthread_mutex_tpk8timespec,pthread_mutex_timedlock .weakref _zl28__gthrw_pthread_mutex_unlockp15pthread_mutex_t,pthread_mutex_unlock .weakref _zl26__gthrw_pthread_mutex_initp15pthread_mutex_tpk19pthread_mutexattr_t,pthread_mutex_init .weakref _zl29__gthrw_pthread_mutex_destroyp15pthread_mutex_t,pthread_mutex_destroy .weakref _zl30__gthrw_pthread_cond_broadcastp14pthread_cond_t,pthread_cond_broadcast .weakref _zl27__gthrw_pthread_cond_signalp14pthread_cond_t,pthread_cond_signal .weakref _zl25__gthrw_pthread_cond_waitp14pthread_cond_tp15pthread_mutex_t,pthread_cond_wait .weakref _zl30__gthrw_pthread_cond_timedwaitp14pthread_cond_tp15pthread_mutex_tpk8timespec,pthread_cond_timedwait .weakref _zl28__gthrw_pthread_cond_destroyp14pthread_cond_t,pthread_cond_destroy .weakref _zl26__gthrw_pthread_key_createpjpfvpve,pthread_key_create .weakref _zl26__gthrw_pthread_key_deletej,pthread_key_delete .weakref _zl30__gthrw_pthread_mutexattr_initp19pthread_mutexattr_t,pthread_mutexattr_init .weakref _zl33__gthrw_pthread_mutexattr_settypep19pthread_mutexattr_ti,pthread_mutexattr_settype .weakref _zl33__gthrw_pthread_mutexattr_destroyp19pthread_mutexattr_t,pthread_mutexattr_destroy .ident "gcc: (ubuntu 4.4.3-4ubuntu5) 4.4.3" .section .note.gnu-stack,"",@progbits
different compilers produce different code. version of gcc versus current version of gcc produce different code.
more importantly, iostream
handles lot of things stdio
doesn't, there's going substantial overhead. understand that, in theory, these compiled down indentical code, they're doing not technically identical.
Comments
Post a Comment