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.

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章