打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
setjmp 和 longjmp,以及对变量的影响
userphoto

2013.01.14

关注

setjmp 和 longjmp,以及对变量的影响

分类: APUE 182人阅读 评论(0) 收藏 举报

setjmp/longjmp是用来做non-local goto的。 一般情况下不建议使用。

 

先来看个例子。

#include <sys/types.h>  /* for socket(2) and related bits and pieces */
#include <sys/socket.h> /* for socket(2) */
#include <net/if.h>     /* for struct ifreq */
#include <net/if_arp.h> /* for ARPHRD_ETHER */
#include <sys/ioctl.h>  /* for IOCTL's */
#include <stdio.h>      /* for fprintf etc */
#include <unistd.h>     /* for close */
#include <stdlib.h>
#include <setjmp.h>

static void f1(int, int, int, int, int*);
static void f2(void);

static jmp_buf jmpbuffer;
static int     globval;

int main(void)
{
    int            autoval;
    register int   regival;
    volatile int   volaval;
    static   int   statval;
    int            autoval2;

    globval =1; autoval = 2; regival = 3; volaval = 4; statval = 5;
    autoval2 = 0;

    if (setjmp(jmpbuffer) != 0)
    {
        printf("after longjmp:/n");
        printf("globval = %d, autoval = %d, regival = %d,"
                " volaval = %d, statval = %d, autoval2 = %d/n",
                globval, autoval, regival, volaval, statval, autoval2);
        exit(0);
    }

    globval =95; autoval = 96; regival = 97; volaval = 98; statval = 99;

    f1( autoval, regival, volaval, statval, &autoval2);
    exit(0);
}

static void f1(int i, int j, int k, int l, int* m)
{
    printf("in f1():/n");
    printf("globval = %d, autoval = %d, regival = %d,"
            " volaval = %d, statval = %d, autoval2 = %d/n",
            globval, i, j, k, l, *m);
    *m = 11;
    f2();
}

static void f2(void)
{
    longjmp(jmpbuffer, 1);
}

 

运行结果如下

in f1():
globval = 95, autoval = 96, regival = 97, volaval = 98, statval = 99, autoval2 = 0
after longjmp:
globval = 95, autoval = 96, regival = 97, volaval = 98, statval = 99, autoval2 = 11
这个结果跟我想象的有点不一样。本来以为会恢复成,1,2,3,4,5的。

 

用优化编译后得到的另一个结果

gcc -O test.c

in f1():
globval = 95, autoval = 96, regival = 97, volaval = 98, statval = 99, autoval2 = 0
after longjmp:
globval = 95, autoval = 2, regival = 3, volaval = 98, statval = 99, autoval2 = 0

 

书上的解释如下:

Note that the optimizations don't affect the global, static, and volatile variables;
their values after the longjmp are the last values that they assumed.
The setjmp(3) manual page on one system states that variables stored in memory will
have values as of the time of the longjmp, whereas variables in the CPU and floating-point
registers are restored to their values when setjmp was called.
This is indeed what we see
when we run the program in Figure 7.13. Without optimization, all five variables are stored in memory
(the register hint is ignored for regival). When we enable optimization,
both autoval and regival go into registers, even though the former wasn't declared register,
and the volatile variable stays in memory. The thing to realize with this example is that you
must use the volatile attribute if you're writing portable code that uses nonlocal jumps.
Anything else can change from one system to the next.

 

1. 优化不影响 global, static, volatile变量。

2. 标出来的那句话意思是,保存在内存中的变量,longjmp返回后,保持了longjmp时的值。

    而在cpu中的值将会退回到setjmp时的值。

3. 没有优化时global, static, volatile, auto, register都存在内存中。

    返回的时候,这些值仍然是longjmp时候的值。

    但在有优化的时候, auto, register的值是在寄存器的。

    所以返回的时候,这两个值是在setjmp时候的值。

 

本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
c语言中setjmp与longjmp(3)
c函数库中的longjmp与setjmp函数的用法
程序的运行时 数据结构
Stack Clashing for Fun and Profit « null program
使用setjmp和longjmp实现程序非本地跳转
C标准库函数浅析
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服