c - 为什么 `volatile` 没有区别?

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

根据Wiki的解释,应该是有区别的,volatile版本应该比non-volatile版本大。我把它们编译成二进制,发现它们的大小也是一样的。

问题:

  • 是我的 llvm-gcc 导致的吗?(稍后我将在 Linux 上测试这些代码)还是 Wiki 的解释有误?
  • 它们都编译了相同的二进制代码(只是根据它们的大小),所以它们的打印结果是一样的。但是根据wiki的解释,两者打印出不同的屏幕输出吗?

更新

这个问题是维基百科优化设置的错误,和标题说的无关。我应该关闭这个问题吗?

最佳答案

使用 gcc -O3 开启优化,您应该会看到不同之处。

关于c - 为什么 `volatile` 没有区别?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12285118/

相关文章:

c - 写入视频内存(0xB8000)和 volatile 指针

在 boost::shared_ptr operator bool() 上旋转时需要 C++ volatile?

Java在线程之间共享对象

c - LLVM:为什么在构建调用时会出现段错误?

c - volatile 变量作为函数的参数

c# - 为什么(或不是)在构造函数中设置字段是线程安全的?

c++ - 条件运算符的奇怪使用

c - C 语言新手,需要结构相关函数的帮助

c - Linux 文件创建大小

c - 为 Intel PT 注册自定义中断处理程序