🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Thread View: comp.unix.internals
8 messages
8 total messages Started by Sivarama Subramanian Fri, 08 Jan 1993 17:23
Question about longjmp ()
#2622
Author: Sivarama Subramanian
Date: Fri, 08 Jan 1993 17:23
18 lines
748 bytes
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
Re: Question about longjmp ()
#2623
Author: Ji-Tzay Yang
Date: Sat, 09 Jan 1993 12:43
31 lines
1558 bytes
Sivarama Subramanian (subra@bynar.austin.ibm.com) wrote:
: 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.

  After running the program, the value 'i' is changed, and it should be
  changed.

  But is there any consideration for 'i' to keep its orignal value?


--
                                         With regard,

 ============================================================================
 Ji-Tzay Yang (·¨ °ò ¸ü)              |  E-mail : cp78065@csie.nctu.edu.tw
 Dept. of Comp. Science & Info. Eng.  |           u7817065@cc.nctu.edu.tw 
 National Chiao-Tung University       |  Tel    : 886-35-712121 ext 6430 
 Hsin-Chu, Taiwan ROC                 |  Fax    : 886-35-721486 
 ============================================================================
Re: Question about longjmp ()
#2624
Author: Minas Spetsakis
Date: Sun, 10 Jan 1993 02:45
21 lines
1186 bytes
In article <1993Jan9.124323.13018@csie.nctu.edu.tw>, cp78065@csie.nctu.edu.tw (Ji-Tzay Yang) writes:
|> Sivarama Subramanian (subra@bynar.austin.ibm.com) wrote:

|> : 	My question is, should the value of i change or not.
|> 
|>   After running the program, the value 'i' is changed, and it should be
|>   changed.
|> 
|>   But is there any consideration for 'i' to keep its orignal value?
|> 


It all depends if i is a register. C decides what to store in a register. The
register directive is only a hint.

The only way I can think that enforces i not to be a register is to have at
some point in your code a &i. But then, I never tested that. Or have it global.


Minas
Re: Question about longjmp ()
#2625
Author: Jim "Throw it over the wall" Was...
Date: Sun, 10 Jan 1993 14:56
24 lines
1042 bytes
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
Re: Question about longjmp ()
#2628
Author: Andrew M. Dunn
Date: Sun, 10 Jan 1993 17:31
23 lines
1558 bytes
In article <1639@yetti.UUCP> minas@cs.yorku.ca (Minas Spetsakis) writes:
>
>In article <1993Jan9.124323.13018@csie.nctu.edu.tw>, cp78065@csie.nctu.edu.tw (Ji-Tzay Yang) writes:
>|> : 	My question is, should the value of i change or not.
>It all depends if i is a register. C decides what to store in a register. The
>register directive is only a hint.
>
>The only way I can think that enforces i not to be a register is to have at
>some point in your code a &i. But then, I never tested that. Or have it global.

Even that's not a guarantee.  Some processors map the registers to low
address space.  A C compiler on one of those beasts could correctly produce
&i even if i was a register variable!



Andy

-- 
:-------------------------------------------------------------------------:
:  Andy Dunn           <amdunn@mongrel.UUCP> or <dunn5177@mach1.WLU.CA>   :
:  "AT&T thinks Usenet is an Underground organization" - are we really?   :
:-------------------------------------------------------------------------:
Re: Question about longjmp ()
#2627
Author: John Muth
Date: Sun, 10 Jan 1993 19:07
50 lines
2609 bytes
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
Re: Question about longjmp ()
#2629
Author: Blair P. Houghton
Date: Sun, 10 Jan 1993 23:05
25 lines
1606 bytes
In article <184875@pyramid.pyramid.com> jmuth@pyrps5.eng.pyramid.com (John Muth) writes:
>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.

The standard (ANSI X3.159-1989) says, after much waving of
hands and servicing of lips, that the value of `i' in this
program is "indeterminate" after the call to longjmp (because
it's an auto variable changed between the calls etc. etc.).

>My suggestion to the original poster is "don't do that". Setjmp defines
>a return value in order to avoid this situation. Use it.

Um, yes, theoretically you can save `i' by passing it back
through longjmp, but you only get to do that to one
variable.

The real value of setjmp/longjmp is that it restores the
signal mask and can pull you out of a deep recursion if a
minor occurrence results in a rare and complicated
problem.  You shouldn't use it as part of handling
predictable errors; then it'd be just another `goto'.

				--Blair
				  "etc., etc..."
Re: Question about longjmp ()
#2630
Author: Hans Bos
Date: Mon, 11 Jan 1993 11:08
35 lines
1371 bytes
cp78065@csie.nctu.edu.tw (Ji-Tzay Yang) writes:

>Sivarama Subramanian (subra@bynar.austin.ibm.com) wrote:
>: 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.

>  After running the program, the value 'i' is changed, and it should be
>  changed.

>  But is there any consideration for 'i' to keep its orignal value?

I believe that in ANSI C nothing is guaranteed for local variables 
after a longjmp, unless they are declared volatile.

So if you use 'volatile int i = 0;', the value of i should be 1 after
the longjmp.


-- 
Hans Bos                     	   domain : hans@xelion.nl
Xelion BV                          uucp   : ...!sun4nl!xelion!hans
Postbus 88                         phone  : +31 15 622121
2600 AB Delft, The Netherlands     fax    : +31 15 621760
Thread Navigation

This is a paginated view of messages in the thread with full content displayed inline.

Messages are displayed in chronological order, with the original post highlighted in green.

Use pagination controls to navigate through all messages in large threads.

Back to All Threads