home *** CD-ROM | disk | FTP | other *** search
- Path: sparky!uunet!ferkel.ucsb.edu!taco!gatech!paladin.american.edu!howland.reston.ans.net!usc!cs.utexas.edu!sun-barr!olivea!gossip.pyramid.com!pyramid!pyrps5.eng.pyramid.com!jmuth
- From: jmuth@pyrps5.eng.pyramid.com (John Muth)
- Newsgroups: comp.unix.internals
- Subject: Re: Question about longjmp ()
- Message-ID: <184875@pyramid.pyramid.com>
- Date: 10 Jan 93 19:07:40 GMT
- References: <C0JpMo.1rGr@austin.ibm.com> <1993Jan10.145640.3647@sequent.com>
- Sender: news@pyramid.pyramid.com
- Organization: Pyramid Technology Corporation, Mountain View
- Lines: 50
-
- In article <1993Jan10.145640.3647@sequent.com> washer@sequent.com (Jim "Throw it over the wall" Washer) writes:
- >In article <C0JpMo.1rGr@austin.ibm.com> subra@bynar.austin.ibm.com (Sivarama Subramanian) writes:
- >>Hi folks,
- >> I have the following code segment.
- >> main ()
- >> {
- >> int i = 0;
- >> jmp_buf buf;
- >> setjmp (buf);
- >> printf ("%d\n", i);
- >> if ( i == 1 )
- >> exit;
- >> i = 1;
- >> longjmp (buf, 1);
- >> }
- >> My question is, should the value of i change or not.
- >>
- >>Thank you
- >>
- >>subra
- >>
- >Absolutely!. There is no magic with setjmp/longjmp.. It simply sets the pc
- >and the stack pointer back to the setjmp call and returns again.. 'Both'
- >intances of main share a common stack.
- > - jim
-
- Don't be so quick Jim. The answer is really:
-
- It depends on the machine architecture and the compiler.
-
- For example, the last compiler I knew well enough to answer this question
- would have had problems with this code if optimization was on. The machine
- was a CISC with a set of 16 registers. Some of these registers where defined
- by the compiler to contain things like the stack pointer, the return address,
- etc. Where it could the compiler allocated local varibables to registers.
- Setjmp would save the entire register set. Since "i" would be in a register,
- it's value at that point of the setjmp call would be saved along with the
- rest of the registers. The longjmp simply restored the register set saved
- by setjmp (the entire set). Under these circumstances, the code segment
- would result in an infinite loop. If on the other hand, optimization was
- disabled and "i" was allocated on the stack, the code segment would terminate.
-
- I'm not sure what the C langauge spec. says about the semantics of this sort
- of construct or even if it addresses it at all. After all, K&R left lots of
- things like this up to the imagination of the complier writer.
-
- My suggestion to the original poster is "don't do that". Setjmp defines
- a return value in order to avoid this situation. Use it.
-
- -John Muth
-