2012-09-05 86 views
2

根据维基百科的网站:为什么'volatile'没有区别?

http://en.wikipedia.org/wiki/Volatile_variable

我抄袭了它的示例代码在我的笔记本电脑的测试,但两人都没有什么区别!这里是我的GCC版本信息:

i686-apple-darwin11-llvm-gcc-4.2 (GCC) 4.2.1 (Based on Apple Inc. build 5658) (LLVM build 2336.1.00) 
Copyright (C) 2007 Free Software Foundation, Inc. 
This is free software; see the source for copying conditions. There is NO 
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 

的C源代码是

/* volatile_var.c */ 
#include <stdio.h> 
int main() { 
     /* in no_volatile_var.c there is no volatile here */ 
     volatile int a = 10, b = 100, c = 0, d = 0; 
     printf("%d\n", a + b); 

     a = b; 
     c = b; 
     d = b; 

     printf("%d\n", c + d); 
     printf("%d\n", a); 
     return 0; 
} 

我编译no_volatile_var.c和volatile_var.c都与

gcc -S *.c 

,但出来的是以下同样

.section __TEXT,__text,regular,pure_instructions 
    .globl _main 
    .align 4, 0x90 
_main: 
Leh_func_begin1: 
    pushq %rbp 
Ltmp0: 
    movq %rsp, %rbp 
Ltmp1: 
    subq $32, %rsp 
Ltmp2: 
    movl $10, -12(%rbp) 
    movl $100, -16(%rbp) 
    movl $0, -20(%rbp) 
    movl $0, -24(%rbp) 
    movl -12(%rbp), %eax 
    movl -16(%rbp), %ecx 
    addl %ecx, %eax 
    xorb %cl, %cl 
    leaq L_.str(%rip), %rdx 
    movq %rdx, %rdi 
    movl %eax, %esi 
    movb %cl, %al 
    callq _printf 
    movl -16(%rbp), %ecx 
    movl %ecx, -12(%rbp) 
    movl -16(%rbp), %ecx 
    movl %ecx, -20(%rbp) 
    movl -16(%rbp), %ecx 
    movl %ecx, -24(%rbp) 
    movl -20(%rbp), %ecx 
    movl -24(%rbp), %edx 
    addl %edx, %ecx 
    xorb %dl, %dl 
    leaq L_.str(%rip), %rdi 
    movl %ecx, %esi 
    movb %dl, %al 
    callq _printf 
    movl -12(%rbp), %ecx 
    xorb %dl, %dl 
    leaq L_.str(%rip), %rdi 
    movl %ecx, %esi 
    movb %dl, %al 
    callq _printf 
    movl $0, -8(%rbp) 
    movl -8(%rbp), %eax 
    movl %eax, -4(%rbp) 
    movl -4(%rbp), %eax 
    addq $32, %rsp 
    popq %rbp 
    ret 
Leh_func_end1: 

    .section __TEXT,__cstring,cstring_literals 
L_.str: 
    .asciz "%d\n" 

    .section __TEXT,__eh_frame,coalesced,no_toc+strip_static_syms+live_support 
EH_frame0: 
Lsection_eh_frame: 
Leh_frame_common: 
Lset0 = Leh_frame_common_end-Leh_frame_common_begin 
    .long Lset0 
Leh_frame_common_begin: 
    .long 0 
    .byte 1 
    .asciz "zR" 
    .byte 1 
    .byte 120 
    .byte 16 
    .byte 1 
    .byte 16 
    .byte 12 
    .byte 7 
    .byte 8 
    .byte 144 
    .byte 1 
    .align 3 
Leh_frame_common_end: 
    .globl _main.eh 
_main.eh: 
Lset1 = Leh_frame_end1-Leh_frame_begin1 
    .long Lset1 
Leh_frame_begin1: 
Lset2 = Leh_frame_begin1-Leh_frame_common 
    .long Lset2 
Ltmp3: 
    .quad Leh_func_begin1-Ltmp3 
Lset3 = Leh_func_end1-Leh_func_begin1 
    .quad Lset3 
    .byte 0 
    .byte 4 
Lset4 = Ltmp0-Leh_func_begin1 
    .long Lset4 
    .byte 14 
    .byte 16 
    .byte 134 
    .byte 2 
    .byte 4 
Lset5 = Ltmp1-Ltmp0 
    .long Lset5 
    .byte 13 
    .byte 6 
    .align 3 
Leh_frame_end1: 


.subsections_via_symbols 

根据Wiki的解释,应该有差异,易失版本应该比不易变版本更大。我将它们编译为二进制文件,并发现其大小也相同。

问题:?

  • 请问我LLVM-GCC原因在于(后者我将测试在Linux上,这些代码)或Wiki的的解释是错误?
  • 它们都编译相同的二进制代码(只是根据它们的大小),所以它们的打印结果是相同的。但根据Wiki的解释,这两个打印不同的屏幕输出?

更新

这个问题是由于维基百科上关于优化设置错误,并有标题说的没有关联。我应该关闭这个问题吗?

+0

禁用优化并尝试。 – asawyer

+4

尝试使用gcc -O3并参见 –

+0

@asawyer:''''volatile关键字会阻止编译器执行优化' – perilbrain

回答

5

使用gcc -O3打开优化,您应该看到有所不同。

4

volatile关键字仅仅是编译器的一个提示,即外部线程可能随时更改此变量,因此应该谨慎对待依赖于不会发生这种情况的优化。但是你的应用程序没有任何其他线程,所以没有理由以不同的方式对待它。

您可以尝试使用更高优化设置(-O3命令行参数)编译程序 - 这可能会导致编译器对非易失性版本进行优化,这些版本对于易失性版本是被禁止的。

0

如果我们将变量声明为volatile,编译器将无法执行优化。