C语言中的undefined behavior系列(2)-- lifetime of object

文章的传送门: C语言中的undefined behavior/unspecified behavior - 序

嗷嗷的话: 本文里只要是大段大段的英文,都来至C99标准。所有的中文注释都是我对标准的理解。在文中使用的编译器是开了C99支持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).

这三种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
{
}

}

 

 

 

你可能感兴趣的:(undefined)