Thread: understanding setJmp

  1. #1
    Registered User
    Join Date
    Oct 2006
    New York

    understanding setJmp


    I am trying to implement setjmp for functions.

    #include <setjmp.h>
    #include <stdio.h>
    jmp_buf arr[3];
    void DISP(int x , int i)
    	if(!setjmp(arr[i]) ) { printf("FUNC SAVED!\n"); return; }
    	printf("Func Continues here for buf : %d\n", i);
    	printf(" x :%d at buf : %d\n", x , i);
    int main(void)
    	int  value;
    	int rerun = 0;
    		value = 3;
    		DISP(0, 0);
    		value = 2;
    		DISP(1, 1);
    		value = 1;
    		DISP(2, 2);
    	rerun = 1;
    	longjmp(arr[0], 1);
    Does setjmp not save any of the parameter information?

    Also, I found this article on setjmp implemented coroutines, it seems you need to allocated memory via an array :

     void *cogo(jmp_buf here, void (*fun)(void*), void *arg) {
            if (tos == NULL) tos = (char*)&arg;
            tos += STACKDIR STACKSIZE;
            char n[STACKDIR (tos - (char*)&arg)];
            coarg = n; // ensure optimizer keeps n
            if (setjmp(here)) return(coarg);
    Could I possibly use malloc to allocate memory for the stack too?

    thank you!

  2. #2
    Registered User
    Join Date
    Sep 2007
    Once you've returned from the function that calls setjmp(), you can no longer jump to it; setjmp() relies on you to keep the function alive for a call from longjmp(). In a sense, setjmp() just creates a pointer to the current stack state, so if you invalidate that, you invalidate the value of the jmp_buf.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Question about setjmp /longjmp
    By hussein in forum C Programming
    Replies: 2
    Last Post: 08-16-2009, 03:47 AM
  2. Usage of setjmp(buf) and longjmp?
    By shwetha_siddu in forum C Programming
    Replies: 1
    Last Post: 04-07-2009, 07:41 AM
  3. Help understanding!
    By Thegnome in forum C++ Programming
    Replies: 4
    Last Post: 09-26-2007, 06:57 PM
  4. understanding
    By laasunde in forum C++ Programming
    Replies: 3
    Last Post: 11-27-2002, 01:32 PM