的setjmp / longjmp的和局部变量 [英] setjmp/longjmp and local variables

查看:314
本文介绍了的setjmp / longjmp的和局部变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的提问针对的setjmp / longjmp的关于局部变量的行为。

My questions aims at the behaviour of setjmp/longjmp concerning local variables.

举例code:

jmp_buf env;


void abc()
{
  int error;

  ... 
  if(error)
    longjmp(env);
}


void xyz() {
  int v1;           // non-volatile; changed between setjmp and longjmp
  int v2;           // non-volatile; not changed between setjmp and longjmp
  volatile int v3;  // volatile;     changed between setjmp and longjmp
  volatile int v4;  // volatile;     not changed between setjmp and longjmp 

  ...

  if(setjmp(env)) {
    // error handling
    ...
    return;
  }

  v1++; // change v1
  v3++; // change v3

  abc();
}


int main(...) {
  xyz();
}

的setjmp文档/ longjmp的说:

The documentation of setjmp/longjmp says:

所有可访问对象都值作为时间的longjmp的()被调用,
除了自动存储时间的对象,其数值
是本地的含有相应的调用的功能
的setjmp()不具有挥发性的限定类型和改变
在的setjmp()调用和longjmp()调用之间是不确定的。

"All accessible objects have values as of the time longjmp() was called, except that the values of objects of automatic storage duration which are local to the function containing the invocation of the corresponding setjmp() which do not have volatile-qualified type and which are changed between the setjmp() invocation and longjmp() call are indeterminate."

我看到下面的两个可能的跨pretations:

I see following two possible interpretations:

INTE pretation1:

intepretation1:

局部变量被恢复,除了那些既

Local variables are restored, except those that are both


  • 非挥发性和

  • 修改

INTE pretation2:

intepretation2:

局部变量被恢复,除了


  • 那些非挥发性和

  • 那些改变

据内部pretation1后的longjmp只有V1是不确定的。 V2,V3,V4进行定义。
据内部pretation2后的longjmp只有V4定义。 V1,V2,V3是不确定的。

According to interpretation1 after longjmp only v1 is undefined. v2, v3, v4 are defined. According to interpretation2 after longjmp only v4 is defined. v1, v2, v3 are undefined.

哪一个是正确的?

BTW:我需要一个一般的(便携式)答案是适用于所有的编译器,即尝试与一个特定的编译器并不能帮助

BTW: I need a general ("portable") answer that is valid for all compilers, i.e. trying out with one particular compiler doesn't help.

推荐答案

国米pretation 1是正确的。如果国米pretation 2之意,原文将使用这是改变,而不是和

Interpretation 1 is correct. If the Interpretation 2 was intended, the original text would have used "or which are changed" instead of "and".

这篇关于的setjmp / longjmp的和局部变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆