退出,atexit和范围 [英] exit, atexit and scope

查看:79
本文介绍了退出,atexit和范围的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道在下面的函数

cleanup()中使用指针引用是否有效,或者如果规范中的某些东西阻止了这个

种类exit()期间的交叉引用。我没有在

规范中看到任何相反的内容,我的意思是一个as-if规则说'atexit registered

函数被执行 - 如果它们被调用从主要的,使val

在这一点上超出范围。


a +,ld。


#包括< stdio.h>

#include< stdlib.h>


int * ref;


void cleanup(void){

if(ref)fprintf(stderr," val =%d \ n",* ref);

}


无效测试(无效){

int val = 12;

ref =& val;

退出(EXIT_FAILURE);

}


int main(无效){

atexit(清理);

test();

返回0;

}

I would like to know if the use of the pointer ref in the function
cleanup() below is valid or if something in the norm prevents this
kind of cross-reference during exit(). I haven''t seen anything in the
norm against this, I mean an as-if rule saying "atexit registered
functions are executed as-if they were called from main", making val
out of scope at this point.

a+, ld.

#include <stdio.h>
#include <stdlib.h>

int *ref;

void cleanup(void) {
if (ref) fprintf(stderr, "val = %d\n", *ref);
}

void test(void) {
int val = 12;
ref = &val;
exit(EXIT_FAILURE);
}

int main(void) {
atexit(cleanup);
test();
return 0;
}

推荐答案

Laurent Deniau < La ************ @ gmail.comwrites:
Laurent Deniau <La************@gmail.comwrites:

我想知道是否使用了指针ref功能

cleanup()如下是有效的,或者如果规范中的某些内容在exit()期间阻止了这种交叉引用。我没有在

规范中看到任何相反的内容,我的意思是一个as-if规则说'atexit registered

函数被执行 - 如果它们被调用从主要的,使val

在这一点上超出范围。
I would like to know if the use of the pointer ref in the function
cleanup() below is valid or if something in the norm prevents this
kind of cross-reference during exit(). I haven''t seen anything in the
norm against this, I mean an as-if rule saying "atexit registered
functions are executed as-if they were called from main", making val
out of scope at this point.



你不是指范围。你在谈论

变量''val''的生命周期。名称''va​​l''在整个时间范围内超出了范围

退出正在执行其工作,但现在由''ref''指向的变量仍然是

存在。我认为这是安全的。

You don''t mean scope. You are talking about the lifetime of the
variable ''val''. The name ''val'' is out of scope during the whole time
exit is doing its job, but the variable, now pointed to by ''ref'', still
exists. I think it is safe.


#include< stdio.h>

#include< stdlib.h>


int * ref;


void cleanup(void){

if(ref)fprintf(stderr," val = %d \ n",* ref);

}


void test(void){

int val = 12 ;

ref =& val;

退出(EXIT_FAILURE);

}


int main(void){

atexit(cleanup);

test();

返回0;

}
#include <stdio.h>
#include <stdlib.h>

int *ref;

void cleanup(void) {
if (ref) fprintf(stderr, "val = %d\n", *ref);
}

void test(void) {
int val = 12;
ref = &val;
exit(EXIT_FAILURE);
}

int main(void) {
atexit(cleanup);
test();
return 0;
}



-

Ben。

--
Ben.


Ben Bacarisse< be ** ****** @bsb.me.ukwrites:
Ben Bacarisse <be********@bsb.me.ukwrites:

Laurent Deniau< La ************ @ gmail.comwrites :
Laurent Deniau <La************@gmail.comwrites:

>我想知道在下面的函数
cleanup()中使用指针ref是否有效或者是否符合标准在此期间防止这种交叉引用出口()。我没有在
规范中看到任何反对这一点的东西,我的意思是一个as-if规则,说atexit registered
函数被执行 - 如果它们是从main调用的,则使得val
>I would like to know if the use of the pointer ref in the function
cleanup() below is valid or if something in the norm prevents this
kind of cross-reference during exit(). I haven''t seen anything in the
norm against this, I mean an as-if rule saying "atexit registered
functions are executed as-if they were called from main", making val
out of scope at this point.



你不是指范围。你在谈论

变量''val''的生命周期。名称''va​​l''在整个时间范围内超出了范围

退出正在执行其工作,但现在由''ref''指向的变量仍然是

存在。我认为这是安全的。


You don''t mean scope. You are talking about the lifetime of the
variable ''val''. The name ''val'' is out of scope during the whole time
exit is doing its job, but the variable, now pointed to by ''ref'', still
exists. I think it is safe.


> #include< stdio.h>
#include< stdlib.h>

int * ref;

void cleanup(void){
if(ref)fprintf(stderr," val =%d \ n",* ref);
}

void test(void){
int val = 12;
ref =& val;
exit(EXIT_FAILURE);
}
>#include <stdio.h>
#include <stdlib.h>

int *ref;

void cleanup(void) {
if (ref) fprintf(stderr, "val = %d\n", *ref);
}

void test(void) {
int val = 12;
ref = &val;
exit(EXIT_FAILURE);
}



我不认为它是安全的。

I dont think it is safe.


>>
int main(void){
atexit(cleanup);
test();
>>
int main(void) {
atexit(cleanup);
test();



test()运行但是int val有效地消失了当函数退出时。

test() runs but "int val" is effectively "gone" when the function exits.


>返回0;
}
> return 0;
}



此时调用清理。


当然为此确定val需要是静态的吗?


但是,我欢迎更正。

At this point cleanup is called.

Surely for this to be OK val would need to be a static?

But, I welcome correction.


Laurent Deniau写道:
Laurent Deniau wrote:

我想知道在下面的函数

cleanup()中使用指针引用是否有效或者是否符合标准的内容这个

在exit()期间的交叉引用。我没有在

规范中看到任何相反的内容,我的意思是一个as-if规则说'atexit registered

函数被执行 - 如果它们被调用从主要的,使val

在这一点上超出范围。


a +,ld。


#包括< stdio.h>

#include< stdlib.h>


int * ref;


void cleanup(void){

if(ref)fprintf(stderr," val =%d \ n",* ref);
I would like to know if the use of the pointer ref in the function
cleanup() below is valid or if something in the norm prevents this
kind of cross-reference during exit(). I haven''t seen anything in the
norm against this, I mean an as-if rule saying "atexit registered
functions are executed as-if they were called from main", making val
out of scope at this point.

a+, ld.

#include <stdio.h>
#include <stdlib.h>

int *ref;

void cleanup(void) {
if (ref) fprintf(stderr, "val = %d\n", *ref);



如果没有初始化,那么这个测试是没有意义的。

未初始化。在这个程序中,它很好,但考虑在main()的开头将ref

初始化为NULL。

This test doesn''t make sense if it is at all possible that ref is
uninitialised. In this program, it''s fine, but consider initialising ref
to NULL at the start of main().


}


无效测试(无效){

int val = 12;

ref =& val;

退出(EXIT_FAILURE);

}


int main(无效){

atexit(cleanup);

test();

返回0;

}
}

void test(void) {
int val = 12;
ref = &val;
exit(EXIT_FAILURE);
}

int main(void) {
atexit(cleanup);
test();
return 0;
}



-

Philip Potter pgp< atdoc.ic.ac.uk


--
Philip Potter pgp <atdoc.ic.ac.uk


这篇关于退出,atexit和范围的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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