Are externs automatically volatile?
Allan Pratt
apratt at atari.UUCP
Tue Jun 12 06:33:11 AEST 1990
Are all externs volatile? Forgive me if this is obvious from the ANSI
spec, somebody took mine. I compiled this code and got an unexpected
result:
int this_is_extern;
int my_array[MY_ARRAY_SIZE];
int silly(void)
{
int i;
for (i=0; i<this_is_extern; i++) {
if (my_array[i] == 0) return i;
}
return -1;
}
I had hoped, especially with -fstrength-reduce, that this_is_extern
would be read into a register once, rather than having the memory
reference at the bottom of every loop, but that's not what happened. If
this_is_extern were also volatile, I would understand, and if it is
somehow implicitly volatile, that's the explanation.
Here's the assembly output from GCC 1.37.1 on a VAX:
#NO_APP
gcc_compiled.:
.text
.align 1
.globl _silly
_silly:
.word 0x0
clrl r0
cmpl r0,_this_is_extern
jgeq L6
movab _my_array,r1
L5:
tstl (r1)
jneq L3
ret
L3:
addl2 $4,r1
incl r0
cmpl r0,_this_is_extern
jlss L5
L6:
mcoml $0,r0
ret
.comm _my_array,400
.comm _this_is_extern,4
============================================
Opinions expressed above do not necessarily -- Allan Pratt, Atari Corp.
reflect those of Atari Corp. or anyone else. ...ames!atari!apratt
More information about the Comp.std.c
mailing list