segment段,setjmp和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().

Jump to It!


What Kinds of C Statements End Up in Which Segments

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.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值