看C++Primer的时候看到的,然后去百度了一下;
比较明白的解释:
1、区别在于:\n只代表换行的转义字符endl除了代表换行,还紧跟着清出缓冲槽2、接下来我们看一下具体内容的辨析:要明白\n和endl的区别,首先要明白一个概念:输出流。目前输出流的类型很多,有对文件作为目标输出的,有对显示屏(Console间接输出)进行输出的。也有输出到其他抽象结构的。cout的意思是console-output:控制台输出.但是它的机制还远没有我们想的那么简单,< <后面跟着写什么就直接输出到屏幕什么?不是这样的。就拿cout> <<"Hi,zw.\n"< 后面跟着写什么就直接输出到屏幕什么?不是这样的。就拿cout>只要程序不蹦,还是没什么区别的,万一程序崩溃,\n可能会留在缓冲区里;
贴两段代码(及其汇编):
#includeusing namespace std;int main(){ cout<<"a"<
.file "\316\264\303\374\303\3731.cpp".lcomm __ZStL8__ioinit,1,1 .def ___main; .scl 2; .type 32; .endef .section .rdata,"dr"LC0: .ascii "a\0" .text .globl _main .def _main; .scl 2; .type 32; .endef_main:LFB971: .cfi_startproc leal 4(%esp), %ecx .cfi_def_cfa 1, 0 andl $-16, %esp pushl -4(%ecx) pushl %ebp .cfi_escape 0x10,0x5,0x2,0x75,0 movl %esp, %ebp pushl %ecx .cfi_escape 0xf,0x3,0x75,0x7c,0x6 subl $20, %esp call ___main movl $LC0, 4(%esp) movl $__ZSt4cout, (%esp) call __ZStlsISt11char_traitsIcEERSt13basic_ostreamIcT_ES5_PKc movl $__ZSt4endlIcSt11char_traitsIcEERSt13basic_ostreamIT_T0_ES6_, (%esp) movl %eax, %ecx call __ZNSolsEPFRSoS_E subl $4, %esp movl $0, %eax movl -4(%ebp), %ecx .cfi_def_cfa 1, 0 leave .cfi_restore 5 leal -4(%ecx), %esp .cfi_def_cfa 4, 4 ret .cfi_endprocLFE971: .def ___tcf_0; .scl 3; .type 32; .endef___tcf_0:LFB981: .cfi_startproc pushl %ebp .cfi_def_cfa_offset 8 .cfi_offset 5, -8 movl %esp, %ebp .cfi_def_cfa_register 5 subl $8, %esp movl $__ZStL8__ioinit, %ecx call __ZNSt8ios_base4InitD1Ev leave .cfi_restore 5 .cfi_def_cfa 4, 4 ret .cfi_endprocLFE981: .def __Z41__static_initialization_and_destruction_0ii; .scl 3; .type 32; .endef__Z41__static_initialization_and_destruction_0ii:LFB980: .cfi_startproc pushl %ebp .cfi_def_cfa_offset 8 .cfi_offset 5, -8 movl %esp, %ebp .cfi_def_cfa_register 5 subl $24, %esp cmpl $1, 8(%ebp) jne L4 cmpl $65535, 12(%ebp) jne L4 movl $__ZStL8__ioinit, %ecx call __ZNSt8ios_base4InitC1Ev movl $___tcf_0, (%esp) call _atexitL4: leave .cfi_restore 5 .cfi_def_cfa 4, 4 ret .cfi_endprocLFE980: .def __GLOBAL__sub_I_main; .scl 3; .type 32; .endef__GLOBAL__sub_I_main:LFB982: .cfi_startproc pushl %ebp .cfi_def_cfa_offset 8 .cfi_offset 5, -8 movl %esp, %ebp .cfi_def_cfa_register 5 subl $24, %esp movl $65535, 4(%esp) movl $1, (%esp) call __Z41__static_initialization_and_destruction_0ii leave .cfi_restore 5 .cfi_def_cfa 4, 4 ret .cfi_endprocLFE982: .section .ctors,"w" .align 4 .long __GLOBAL__sub_I_main .ident "GCC: (GNU) 4.8.1" .def __ZStlsISt11char_traitsIcEERSt13basic_ostreamIcT_ES5_PKc; .scl 2; .type 32; .endef .def __ZSt4endlIcSt11char_traitsIcEERSt13basic_ostreamIT_T0_ES6_; .scl 2; .type 32; .endef .def __ZNSolsEPFRSoS_E; .scl 2; .type 32; .endef .def __ZNSt8ios_base4InitD1Ev; .scl 2; .type 32; .endef .def __ZNSt8ios_base4InitC1Ev; .scl 2; .type 32; .endef .def _atexit; .scl 2; .type 32; .endef
#includeusing namespace std; int main(){ cout<<"a"<<"\n"; return 0; }
.file "\316\264\303\374\303\3732.cpp".lcomm __ZStL8__ioinit,1,1 .def ___main; .scl 2; .type 32; .endef .section .rdata,"dr"LC0: .ascii "a\0"LC1: .ascii "\12\0" .text .globl _main .def _main; .scl 2; .type 32; .endef_main:LFB971: .cfi_startproc pushl %ebp .cfi_def_cfa_offset 8 .cfi_offset 5, -8 movl %esp, %ebp .cfi_def_cfa_register 5 andl $-16, %esp subl $16, %esp call ___main movl $LC0, 4(%esp) movl $__ZSt4cout, (%esp) call __ZStlsISt11char_traitsIcEERSt13basic_ostreamIcT_ES5_PKc movl $LC1, 4(%esp) movl %eax, (%esp) call __ZStlsISt11char_traitsIcEERSt13basic_ostreamIcT_ES5_PKc movl $0, %eax leave .cfi_restore 5 .cfi_def_cfa 4, 4 ret .cfi_endprocLFE971: .def ___tcf_0; .scl 3; .type 32; .endef___tcf_0:LFB976: .cfi_startproc pushl %ebp .cfi_def_cfa_offset 8 .cfi_offset 5, -8 movl %esp, %ebp .cfi_def_cfa_register 5 subl $8, %esp movl $__ZStL8__ioinit, %ecx call __ZNSt8ios_base4InitD1Ev leave .cfi_restore 5 .cfi_def_cfa 4, 4 ret .cfi_endprocLFE976: .def __Z41__static_initialization_and_destruction_0ii; .scl 3; .type 32; .endef__Z41__static_initialization_and_destruction_0ii:LFB975: .cfi_startproc pushl %ebp .cfi_def_cfa_offset 8 .cfi_offset 5, -8 movl %esp, %ebp .cfi_def_cfa_register 5 subl $24, %esp cmpl $1, 8(%ebp) jne L4 cmpl $65535, 12(%ebp) jne L4 movl $__ZStL8__ioinit, %ecx call __ZNSt8ios_base4InitC1Ev movl $___tcf_0, (%esp) call _atexitL4: leave .cfi_restore 5 .cfi_def_cfa 4, 4 ret .cfi_endprocLFE975: .def __GLOBAL__sub_I_main; .scl 3; .type 32; .endef__GLOBAL__sub_I_main:LFB977: .cfi_startproc pushl %ebp .cfi_def_cfa_offset 8 .cfi_offset 5, -8 movl %esp, %ebp .cfi_def_cfa_register 5 subl $24, %esp movl $65535, 4(%esp) movl $1, (%esp) call __Z41__static_initialization_and_destruction_0ii leave .cfi_restore 5 .cfi_def_cfa 4, 4 ret .cfi_endprocLFE977: .section .ctors,"w" .align 4 .long __GLOBAL__sub_I_main .ident "GCC: (GNU) 4.8.1" .def __ZStlsISt11char_traitsIcEERSt13basic_ostreamIcT_ES5_PKc; .scl 2; .type 32; .endef .def __ZNSt8ios_base4InitD1Ev; .scl 2; .type 32; .endef .def __ZNSt8ios_base4InitC1Ev; .scl 2; .type 32; .endef .def _atexit; .scl 2; .type 32; .endef
本弱汇编还在学= = 所以很多看不懂=- =~
等能看懂了,再回来研究研究