volatile Keyword Question

N

Nomen Nescio

Harald said:
How about this, then?

void f(void) {
int x;
x = 2;

}

int main(void) {
for (unsigned int i = 0; ++i; )
f();

}

Is the compiler allowed to remove this loop? Modifying an object is a side
effect, but I expect any decent compiler to treat this as an unnecessary
side effect.

Well, let's have a look at how gcc treats it with -O3.

gcc 2.95.x
----------

f:
pushl %ebp
movl %esp,%ebp
leave
ret

Leaves in the prolog and epilog code for f but optimizes
out the variable x.

main:
pushl %ebp
movl %esp,%ebp
movl $1,%eax
L7:
incl %eax
jnz .L7
xorl %eax,%eax
leave
ret

Does the full loop but optimizes out the call to f.

latest gcc
----------

Same code produced for f.

main:
pushl %ebp
movl %esp, %ebp
xorl %eax, %eax
leave
ret

But main is smarter. So it would appear that gcc believes it's
allowed to remove that loop, and I doubt anyone would complain.

Yours,
Han from China
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,982
Messages
2,570,185
Members
46,736
Latest member
AdolphBig6

Latest Threads

Top