日韩黑丝制服一区视频播放|日韩欧美人妻丝袜视频在线观看|九九影院一级蜜桃|亚洲中文在线导航|青草草视频在线观看|婷婷五月色伊人网站|日本一区二区在线|国产AV一二三四区毛片|正在播放久草视频|亚洲色图精品一区

分享

setjmp 和 longjmp,以及對變量的影響

 WUCANADA 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);
}

 

運(yùn)行結(jié)果如下

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
這個結(jié)果跟我想象的有點(diǎn)不一樣。本來以為會恢復(fù)成,1,2,3,4,5的。

 

用優(yōu)化編譯后得到的另一個結(jié)果

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. 優(yōu)化不影響 global, static, volatile變量。

2. 標(biāo)出來的那句話意思是,保存在內(nèi)存中的變量,longjmp返回后,保持了longjmp時的值。

    而在cpu中的值將會退回到setjmp時的值。

3. 沒有優(yōu)化時global, static, volatile, auto, register都存在內(nèi)存中。

    返回的時候,這些值仍然是longjmp時候的值。

    但在有優(yōu)化的時候, auto, register的值是在寄存器的。

    所以返回的時候,這兩個值是在setjmp時候的值。

 

    本站是提供個人知識管理的網(wǎng)絡(luò)存儲空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點(diǎn)。請注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購買等信息,謹(jǐn)防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請點(diǎn)擊一鍵舉報。
    轉(zhuǎn)藏 分享 獻(xiàn)花(0

    0條評論

    發(fā)表

    請遵守用戶 評論公約

    類似文章 更多