为什么`volatile`没有区别?

时间:2012-09-05 15:42:42

标签: c 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

根据维基的解释,应该存在差异,易变版本应该大于非易失性版本。我把它们编译成二进制文件,发现大小也一样。

问题:

  • 我的llvm-gcc会导致这种情况吗?(后者我将在Linux上测试这些代码)或Wiki的解释是错误的?
  • 他们都编译了相同的二进制代码(只是根据它们的大小),因此它们的打印结果是相同的。但根据维基的解释,两者是否打印出不同的屏幕输出?

更新

这个问题归因于维基百科在优化设置上的错误,与标题所说的内容无关。我应该关闭这个问题吗?

3 个答案:

答案 0 :(得分:5)

使用gcc -O3启用优化功能,您应该会看到差异。

答案 1 :(得分:4)

volatile关键字只是编译器的提示,外部线程可能随时更改此变量,因此应该小心优化,而这些优化依赖于不会发生这种情况的假设。但是您的应用程序没有任何其他线程,因此没有理由以不同的方式对待它。

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

答案 2 :(得分:0)

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