using setjmp

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • JS

    #1

    using setjmp

    When setjmp is called how can the if statement evaluate to true or false
    when setjmp only returns 0 or non-zero?


    struct pcb {
    void *(*start_routin e) (void *);
    void *arg;
    jmp_buf state;
    int stack[1024];
    };


    struct pcb *pcb_pointer;
    pcb_pointer = (struct pcb *) malloc(sizeof(s truct pcb));


    if(setjmp(pcb_p ointer->state)) {
    current->start_routine( current->arg);
    printf("Thread returned\n");
    exit(0);
    }


  • Gordon Burditt

    #2
    Re: using setjmp

    >When setjmp is called how can the if statement evaluate to true or false[color=blue]
    >when setjmp only returns 0 or non-zero?[/color]

    There is no true or false in C89. if statements execute the "then" clause
    (even though there's no "then" keyword) if the condition evaluates to
    non-zero.

    While C99 has true and false, this has nothing to do with if statements.

    Gordon L. Burditt

    Comment

    • Dave Vandervies

      #3
      Re: using setjmp

      In article <d1sjvi$5db$1@n ews.net.uni-c.dk>, JS <dsa.@asdf.co m> wrote:[color=blue]
      >When setjmp is called how can the if statement evaluate to true or false
      >when setjmp only returns 0 or non-zero?[/color]

      Have a coffee, go outside, and sit under a tree (or, if the weather
      fails to be cooperative, at least get away from the computer), and
      contemplate the difference between true/false and nonzero/zero, and you
      will be enlightened.


      dave

      --
      Dave Vandervies dj3vande@csclub .uwaterloo.ca[color=blue]
      >The Kremlin has a mother these days?[/color]
      After a fflush(stdin), it might end up having three mothers.
      --Joona I Palaste and Gordon Burditt in comp.lang.c

      Comment

      • JS

        #4
        Re: using setjmp


        "Dave Vandervies" <dj3vande@csclu b.uwaterloo.ca> skrev i en meddelelse
        news:d1sl51$u98 $1@rumours.uwat erloo.ca...[color=blue]
        > In article <d1sjvi$5db$1@n ews.net.uni-c.dk>, JS <dsa.@asdf.co m> wrote:[color=green]
        > >When setjmp is called how can the if statement evaluate to true or false
        > >when setjmp only returns 0 or non-zero?[/color]
        >
        > Have a coffee, go outside, and sit under a tree (or, if the weather
        > fails to be cooperative, at least get away from the computer), and
        > contemplate the difference between true/false and nonzero/zero, and you
        > will be enlightened.[/color]


        Well I have only Java experience and not yet found anything about this
        definition in K&R.


        Comment

        • JS

          #5
          Re: using setjmp


          "Gordon Burditt" <gordon@hammy.b urditt.org> skrev i en meddelelse
          news:4241d80d$0 $88029$16895aa@ news.airnews.ne t...[color=blue][color=green]
          > >When setjmp is called how can the if statement evaluate to true or false
          > >when setjmp only returns 0 or non-zero?[/color]
          >
          > There is no true or false in C89. if statements execute the "then" clause
          > (even though there's no "then" keyword) if the condition evaluates to
          > non-zero.
          >
          > While C99 has true and false, this has nothing to do with if statements.
          >
          > Gordon L. Burditt[/color]

          Where can I find C89 and C99??


          Comment

          • Mark Odell

            #6
            Re: using setjmp

            JS wrote:[color=blue][color=green][color=darkred]
            >>>When setjmp is called how can the if statement evaluate to true or false
            >>>when setjmp only returns 0 or non-zero?[/color]
            >>
            >>Have a coffee, go outside, and sit under a tree (or, if the weather
            >>fails to be cooperative, at least get away from the computer), and
            >>contemplate the difference between true/false and nonzero/zero, and you
            >>will be enlightened.[/color]
            >
            >
            >
            > Well I have only Java experience and not yet found anything about this
            > definition in K&R.[/color]

            I have found that 0 (zero) and !0 (not zero) work well for false and
            true checking.

            Comment

            • Ben Pfaff

              #7
              Re: using setjmp

              Mark Odell <odellmark@hotm ail.com> writes:
              [color=blue]
              > I have found that 0 (zero) and !0 (not zero) work well for false and
              > true checking.[/color]

              I have found that 1 works well as !0.
              --
              Ben Pfaff
              email: blp@cs.stanford .edu
              web: http://benpfaff.org

              Comment

              • Mark Odell

                #8
                Re: using setjmp

                Ben Pfaff wrote:[color=blue]
                > Mark Odell <odellmark@hotm ail.com> writes:
                >
                >[color=green]
                >>I have found that 0 (zero) and !0 (not zero) work well for false and
                >>true checking.[/color]
                >
                >
                > I have found that 1 works well as !0.[/color]

                Yeah but that breaks my "no numbers except zero" rule. :-) Besides,
                sometimes 1 looks like l (ell).

                Comment

                • JS

                  #9
                  Re: using setjmp

                  I found this example:

                  #include <setjmp.h>
                  #include <stdio.h>

                  jmp_buf ex;

                  static int foo (int a, int b)
                  {
                  if (!b)
                  longjmp (ex, 1); /* THROW */
                  else
                  return a/b;
                  }

                  int main (void)
                  {
                  int x = 0, y = 1, z = 0;
                  if (setjmp (ex) == 0) /* TRY : longjmp branches back to here */
                  {
                  x = foo(y, z);
                  }
                  else /* CATCH */
                  {
                  printf ("Exception: attempt to divide by zero\n");
                  }
                  }

                  When foo is called after setjmp has been called, longjmp is called. Then
                  control jumps to setjmp but this time setjmp does not return 0 and
                  therefore: printf ("Exception: attempt to divide by zero\n"); is executed.

                  But I don't understand why setjmp don't return 0 after the longjmp call. Is
                  it because the second parameter to longjmp is used as the return value for
                  setjmp?

                  Or does the second paramter replace 0 in: if (setjmp (ex) == 0)?


                  Comment

                  • Martin Ambuhl

                    #10
                    Re: using setjmp

                    JS wrote:[color=blue]
                    > When setjmp is called how can the if statement evaluate to true or false
                    > when setjmp only returns 0 or non-zero?[/color]

                    Since false is 0 and true is non-zero, I fail to see your problem.

                    Comment

                    • Eric Sosman

                      #11
                      Re: using setjmp



                      JS wrote:[color=blue]
                      > I found this example:
                      >
                      > #include <setjmp.h>
                      > #include <stdio.h>
                      >
                      > jmp_buf ex;
                      >
                      > static int foo (int a, int b)
                      > {
                      > if (!b)
                      > longjmp (ex, 1); /* THROW */
                      > else
                      > return a/b;
                      > }
                      >
                      > int main (void)
                      > {
                      > int x = 0, y = 1, z = 0;
                      > if (setjmp (ex) == 0) /* TRY : longjmp branches back to here */
                      > {
                      > x = foo(y, z);
                      > }
                      > else /* CATCH */
                      > {
                      > printf ("Exception: attempt to divide by zero\n");
                      > }
                      > }
                      >
                      > When foo is called after setjmp has been called, longjmp is called. Then
                      > control jumps to setjmp but this time setjmp does not return 0 and
                      > therefore: printf ("Exception: attempt to divide by zero\n"); is executed.
                      >
                      > But I don't understand why setjmp don't return 0 after the longjmp call. Is
                      > it because the second parameter to longjmp is used as the return value for
                      > setjmp?
                      >
                      > Or does the second paramter replace 0 in: if (setjmp (ex) == 0)?[/color]

                      setjmp() is very peculiar. You call it like an ordinary
                      function, and it returns a value of zero. But if you later
                      call longjmp(), setjmp() returns a second time even though it
                      has not been called a second time. On this second return, it
                      yields the value that was given to longjmp() (except that
                      there's a special case: if you hand a zero to longjmp(),
                      setjmp() returns a one).

                      It is even possible to call longjmp() more than once,
                      causing setjmp() to return more than twice. Such tricks are
                      probably better used for obfuscation than for real code.

                      --
                      Eric.Sosman@sun .com

                      Comment

                      • Keith Thompson

                        #12
                        Re: using setjmp

                        Mark Odell <odellmark@hotm ail.com> writes:[color=blue]
                        > JS wrote:[color=green][color=darkred]
                        >>>>When setjmp is called how can the if statement evaluate to true or false
                        >>>>when setjmp only returns 0 or non-zero?
                        >>>
                        >>>Have a coffee, go outside, and sit under a tree (or, if the weather
                        >>>fails to be cooperative, at least get away from the computer), and
                        >>>contemplat e the difference between true/false and nonzero/zero, and you
                        >>>will be enlightened.[/color]
                        >>
                        >> Well I have only Java experience and not yet found anything about this
                        >> definition in K&R.[/color]
                        >
                        > I have found that 0 (zero) and !0 (not zero) work well for false and
                        > true checking.[/color]

                        I've found that section 9 of the C FAQ works even better.

                        Anything that compares equal to 0 is considered false; anything that
                        compares unequal to 0 is considered true. Declaring your own FALSE
                        and TRUE values can be useful (if you don't have C99's <stdbool.h>),
                        but there's no point in doing anything more elaborate than 0 for FALSE
                        and 1 for TRUE. If you use FALSE and TRUE, use them only as values to
                        be assigned; never compare a logical value to FALSE or to TRUE
                        (especially to TRUE); 2 is "true", but it's not equal to TRUE. For
                        example, never write:
                        if (cond == TRUE) { ... }
                        Instead, just write:
                        if (cond) { ... }

                        Built-in operators (==, !=, <, et al) always yield 0 or 1, but
                        functions returning boolean values (like isdigit()) can only be
                        assumed to return 0 or non-0.

                        --
                        Keith Thompson (The_Other_Keit h) kst-u@mib.org <http://www.ghoti.net/~kst>
                        San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
                        We must do something. This is something. Therefore, we must do this.

                        Comment

                        • Robert Bachmann

                          #13
                          Re: using setjmp

                          JS wrote:[color=blue]
                          > Where can I find C89 and C99??[/color]

                          Latest draft of the C89 standard:

                          - Plain text:

                          - HTML:


                          Latest draft of the C99 standard:

                          - Plain text:

                          - Post script

                          - PDF:


                          You can buy the final C99 standard at:
                          http://www.iso.org/
                          http://webstore.ansi.org/

                          --
                          Robert Bachmann <news@rbach.pri v.at>, PGP-KeyID: 0x8994A748

                          Comment

                          • Chris Croughton

                            #14
                            Re: using setjmp

                            On Wed, 23 Mar 2005 18:29:31 -0500, Eric Sosman
                            <eric.sosman@su n.com> wrote:
                            [color=blue]
                            > It is even possible to call longjmp() more than once,
                            > causing setjmp() to return more than twice. Such tricks are
                            > probably better used for obfuscation than for real code.[/color]

                            I've seen it used in a type of state machine, something like:

                            jmp_buf jump;

                            void top(void)
                            {
                            switch (setjmp(jump))
                            {
                            case 0:
                            start();
                            case 1:
                            func1();
                            case 2:
                            func2();
                            /* ... */
                            case n:
                            funcn();
                            default:
                            break;
                            }
                            }

                            where each function either returned (in which case it fell through to
                            the next state) or it (or more likely a function it called) called
                            longjmp(jump, i); to go directly to state i. (In practice the states
                            were all using an enum rather than a literal number, but you get the
                            idea.) The advantage was that when calling down through a stack of
                            functions it didn't need a test after each one to see whether it should
                            return to a higher level, it just went straight there (there was no
                            local initialisation which needed tidying).

                            It's not an advised program structure, because it is very easily abused,
                            but in the circumstances it was rather elegant...

                            I've also seen it done in an implementation of exceptions in C, to
                            "re-throw" the exception at the current level, but there it was hidden
                            in the exception macros...

                            Chris C

                            Comment

                            • Lawrence Kirby

                              #15
                              Re: using setjmp

                              On Wed, 23 Mar 2005 17:17:01 -0500, Mark Odell wrote:
                              [color=blue]
                              > Ben Pfaff wrote:[color=green]
                              >> Mark Odell <odellmark@hotm ail.com> writes:
                              >>
                              >>[color=darkred]
                              >>>I have found that 0 (zero) and !0 (not zero) work well for false and
                              >>>true checking.[/color]
                              >>
                              >>
                              >> I have found that 1 works well as !0.[/color]
                              >
                              > Yeah but that breaks my "no numbers except zero" rule. :-) Besides,
                              > sometimes 1 looks like l (ell).[/color]

                              ! is also similar.

                              There are a few numbers that are OK as constants in some circumstances,
                              including 1 and 10. E.g. y = x+1 is rarely going to be made clearer by
                              hiding the 1.

                              Lawrence

                              Comment

                              Working...