main()
{
while(1)
{
if(j==7)
{
unsigned short Alphabet[10]; //what is size of this variable
/* if i came out of this loop will the memory allocated be cleared? if not then how to clear it? */
}
}
}
字母的大小是多少?
main()
{
while(1)
{
if(j==7)
{
unsigned short Alphabet[10]; //what is size of this variable
/* if i came out of this loop will the memory allocated be cleared? if not then how to clear it? */
}
}
}
字母的大小是多少?
Alphabet
unsigned short
是一个包含 10 个元素的数组。它的大小是10 * sizeof (unsigned short)
,sizeof (unsigned short)
通常是 2,但它可能因不同的实现而异。
在标准使用的术语中,Alphabet
具有自动存储期限。这意味着它是在进入最里面的包含块时创建的(在这种情况下,由最近的{
and分隔}
)并且在退出该块时不再存在。(通常它将分配在“堆栈”上,但语言标准实际上并未使用该术语,并且某些编译器可能使用不同的策略。)
当我说它“不再存在”时,我并不是说它一定会被清除或从堆栈中弹出。这只是意味着您不再被允许尝试访问它。(您可以尝试通过保存指向对象的指针并在块外取消引用它来做到这一点;如果这样做,您的代码的行为是未定义的。)
基本上,Alphabet
仅在最里面的封闭块正在执行时才存在。编译器可以以任何它喜欢的方式实现它。通常在调用函数时发生实际分配,并且在函数返回时弹出堆栈,但从概念上讲Alphabet
,即使内存没有被显式释放,在块的末尾也不存在。