auto:
A nested function always has internal linkage. Declaring one with extern
is erroneous. If you need to declare the nested function before its definition, use auto
(which is otherwise meaningless for function declarations).
int bar (int *array, int offset, int size) { __label__ failure; auto int access (int *, int); /* ... */ int access (int *array, int index) { if (index > size) goto failure; return array[index + offset]; } /* ... */ }GNU 风格
volatile:
static int foo; void bar(void) { foo = 0; while (foo != 255) ; }
An optimizing compiler will notice that no other code can possibly change the value stored in foo
, and will assume that it will remain equal to 0
at all times. The compiler will therefore replace the function body with an infinite loop similar to this:
void bar_optimized(void) { foo = 0; while (true) ; }
However, foo
might represent a location that can be changed by other elements of the computer system at any time, such as a hardware register of a device connected to the CPU. The above code would never detect such a change; without the volatile
keyword, the compiler assumes that the current program is the only part of the system that could change the value (which is by far the most common situation).
To prevent the compiler from optimizing code as above, the volatile
keyword is used:
static volatile int foo; void bar (void) { foo = 0; while (foo != 255) ; }
register:
register int *foo asm ("a3");
指定寄存器给变量