Optimizing while(1);

U

unified

Hi

Looking for opinions on whether an optimizer could remove

while(1) {};

This is often used in embedded systems when it is desired to "commit
suicide" by waiting for a watchdog reset.

Thanks
Colin
 
A

Andrew Poelstra

Hi

Looking for opinions on whether an optimizer could remove

while(1) {};

This is often used in embedded systems when it is desired to "commit
suicide" by waiting for a watchdog reset.

Nope. Optimizers are not allowed to change the effect of code,
and an infinite loop is certainly not a no-op.
 
N

Nick Keighley

Looking for opinions on whether an optimizer could remove

while(1) {};

This is often used in embedded systems when it is desired to "commit
suicide" by waiting for a watchdog reset.

yes.
 
R

Richard Bos

Nick Keighley said:

No. Removing that line would change the semantics of the code (rather
drastically), so that's not allowed.
Of course, an optimiser still _could_ remove that line, just as an
optimiser _could_ remove all printf() calls in your program. But neither
would be a very good optimiser.

Richard
 
M

MisterE

Hi

Looking for opinions on whether an optimizer could remove

while(1) {};

This is often used in embedded systems when it is desired to "commit
suicide" by waiting for a watchdog reset.

Some compilers will appear to have removed while(1); if its the only
thing before the end of main. This is because if you go to the full
listing of your program you will typically find after main returns that
compilers on embedded systems will then go into there own while(1) loop.
Check your program output listing.
 
T

Tim Rentsch

Andrew Poelstra said:
Nope. Optimizers are not allowed to change the effect of code,
and an infinite loop is certainly not a no-op.

The question is whether the Standard allows it; there
are arguments both pro and con.
 

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,754
Messages
2,569,527
Members
44,998
Latest member
MarissaEub

Latest Threads

Top