home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1993 #1 / NN_1993_1.iso / spool / comp / unix / internal / 2104 < prev    next >
Encoding:
Internet Message Format  |  1993-01-10  |  2.5 KB

  1. 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
  2. From: jmuth@pyrps5.eng.pyramid.com (John Muth)
  3. Newsgroups: comp.unix.internals
  4. Subject: Re: Question about longjmp ()
  5. Message-ID: <184875@pyramid.pyramid.com>
  6. Date: 10 Jan 93 19:07:40 GMT
  7. References: <C0JpMo.1rGr@austin.ibm.com> <1993Jan10.145640.3647@sequent.com>
  8. Sender: news@pyramid.pyramid.com
  9. Organization: Pyramid Technology Corporation, Mountain View
  10. Lines: 50
  11.  
  12. In article <1993Jan10.145640.3647@sequent.com> washer@sequent.com (Jim "Throw it over the wall" Washer) writes:
  13. >In article <C0JpMo.1rGr@austin.ibm.com> subra@bynar.austin.ibm.com (Sivarama Subramanian) writes:
  14. >>Hi folks,
  15. >>    I have the following code segment.
  16. >>    main ()
  17. >>    {
  18. >>        int i = 0;
  19. >>        jmp_buf buf;
  20. >>        setjmp (buf);
  21. >>        printf ("%d\n", i);
  22. >>        if ( i == 1 )
  23. >>            exit;
  24. >>        i = 1;
  25. >>        longjmp (buf, 1);
  26. >>    }
  27. >>    My question is, should the value of i change or not.
  28. >>
  29. >>Thank you
  30. >>
  31. >>subra
  32. >>
  33. >Absolutely!. There is no magic with setjmp/longjmp.. It simply sets the pc
  34. >and the stack pointer back to the setjmp call and returns again.. 'Both'
  35. >intances of main share a common stack.
  36. > - jim
  37.  
  38. Don't be so quick Jim. The answer is really:
  39.  
  40.     It depends on the machine architecture and the compiler.
  41.  
  42. For example, the last compiler I knew well enough to answer this question
  43. would have had problems with this code if optimization was on. The machine
  44. was a CISC with a set of 16 registers. Some of these registers where defined
  45. by the compiler to contain things like the stack pointer, the return address,
  46. etc. Where it could the compiler allocated local varibables to registers.
  47. Setjmp would save the entire register set. Since "i" would be in a register, 
  48. it's value at that point of the setjmp call would be saved along with the 
  49. rest of the registers. The longjmp simply restored the register set saved 
  50. by setjmp (the entire set). Under these circumstances, the code segment
  51. would result in an infinite loop. If on the other hand, optimization was 
  52. disabled and "i" was allocated on the stack, the code segment would terminate.
  53.  
  54. I'm not sure what the C langauge spec. says about the semantics of this sort
  55. of construct or even if it addresses it at all. After all, K&R left lots of 
  56. things like this up to the imagination of the complier writer.
  57.  
  58. My suggestion to the original poster is "don't do that". Setjmp defines
  59. a return value in order to avoid this situation. Use it.
  60.  
  61. -John Muth
  62.