C言語のundefined behaviorシリーズ(2)--lifetime of object

4010 ワード

記事の転送ゲート:C言語のundefined behavior/unspecified behavior-シーケンス
嗷の话:本文の中で大きい段の大きい段の英语でさえすれば、すべてC 99の标准まで来ます.すべての中国語の注釈は私の標準に対する理解です.本文で使用するコンパイラはC 99がIntel C Compiler 11 for Windowsをサポートしている.
— An object is referred tooutside of its lifetime (6.2.4).
— The value of a pointer to anobject whose lifetime has ended is used (6.2.4).
— The value of an object withautomatic storage duration is used while it is indeterminate (6.2.4, 6.7.8,6.8).
この3つのundefined behaviorはかなり一般的な問題である.例:

  
    
char * fun(){
char p[] = " Is OK " ;
return p; // returning address of local variable or temporary
}
 
void func(){
int * p = NULL;
{
int k;
k
= 1 ;
p
= & k;
}
* p = 10 ; // refer to an object whose lifetime has ended
}
 
void fun1()
{
int i;
if (i == 0 ) // uninitialized local variable 'i' used
{
}

}