目录
What Kinds of C Statements End Up in Which Segments
How the Segments of an Executable are Laid Out in Memory
An Activation Record is Created at Runtime for Each Function Call
The following code shows an example of setjmp() and longjmp().
What Kinds of C Statements End Up in Which Segments
How the Segments of an Executable are Laid Out in Memory
Virtual Address Space Layout, Showing Shared Libraries
An Activation Record is Created at Runtime for Each Function Call
The following code shows an example of setjmp() and longjmp().
#include <setjmp.h>
jmp_buf buf;
#include <setjmp.h>
banana() {
printf("in banana()\n");
longjmp(buf, 1);
/*NOTREACHED*/
printf("you'll never see this, because I longjmp'd");
}
main()
{
if (setjmp(buf))
printf("back in main\n");
else {
printf("first time through\n");
banana();
}
}
结果:
% a.out
first time through
in banana()
back in main
或者一种牛叉的方法:
switch(setjmp(jbuf))
{
case 0:
apple = *suspicious;
break;
case 1:
printf("suspicious is indeed a bad pointer\n");
break;
default:
die("unexpected value returned by setjmp");
}
Jump to It!
Take the source of a program you have already written and add setjmp/longjmp to it, so that on receiving some particular input it will start over again.
The header file <setjmp.h> needs to be included in any source file that uses setjmp or longjmp.
Like goto's, setjmp/longjmp can make it hard to understand and debug a program. They are best avoided except in the specific situations described.