Optimizing while(1);

Discussion in 'C Programming' started by unified@ozemail.com.au, Sep 25, 2008.

  1. Guest

    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
     
    , Sep 25, 2008
    #1
    1. Advertisements

  2. On 2008-09-25, <> wrote:
    > 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.

    --
    Andrew Poelstra
    Only GOD may divide by zero. That is how he created black holes.
    -Veselin Jungic
     
    Andrew Poelstra, Sep 25, 2008
    #2
    1. Advertisements

  3. On 25 Sep, 07:01, wrote:

    > 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.

    --
    Nick Keighley
     
    Nick Keighley, Sep 25, 2008
    #3
  4. Richard Bos Guest

    Nick Keighley <> wrote:

    > On 25 Sep, 07:01, wrote:
    >
    > > 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.


    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
     
    Richard Bos, Sep 25, 2008
    #4
  5. Ian Collins Guest

    wrote:
    > Hi
    >

    Please don't multi-post on Usenet. Answered elsewhere as well as here...

    --
    Ian Collins.
     
    Ian Collins, Sep 25, 2008
    #5
  6. MisterE Guest

    wrote:
    > 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.
     
    MisterE, Sep 26, 2008
    #6
  7. Tim Rentsch Guest

    Andrew Poelstra <> writes:

    > On 2008-09-25, <> wrote:
    > > 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.


    The question is whether the Standard allows it; there
    are arguments both pro and con.
     
    Tim Rentsch, Oct 9, 2008
    #7
    1. Advertisements

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. ^CrazyCoder^
    Replies:
    3
    Views:
    7,672
    Terrence Benade
    Sep 15, 2003
  2. David
    Replies:
    1
    Views:
    637
    David
    Jul 19, 2004
  3. Steven

    while loop in a while loop

    Steven, Mar 24, 2005, in forum: Java
    Replies:
    5
    Views:
    2,499
    Tim Slattery
    Mar 30, 2005
  4. Sam
    Replies:
    0
    Views:
    555
  5. Timothy Fitz

    while 1 vs while True

    Timothy Fitz, Dec 13, 2004, in forum: Python
    Replies:
    16
    Views:
    906
    Terry Reedy
    Dec 15, 2004
  6. invni
    Replies:
    36
    Views:
    1,091
    Robert Maas, see http://tinyurl.com/uh3t
    Jul 27, 2005
  7. Uday Bidkar
    Replies:
    4
    Views:
    728
    =?ISO-8859-15?Q?Juli=E1n?= Albo
    Dec 12, 2006
  8. Shawn Minisall

    while within while

    Shawn Minisall, Oct 27, 2007, in forum: Python
    Replies:
    11
    Views:
    677
Loading...