▷ c - 如何释放已用于 C 中变量的内存?

假设您没有使用 malloc,并且正在使用创建字符串或缓冲区
char array[100];
但是你知道你只是将它用于长函数的一小部分,你可以将函数的那部分放在另一组 { }
int reallylongfunction() {
// Do a lot of stuff
{
char stringbuffer[100];
// Do stuff with the buffer...
// Ok, we're done with the buffer, and don't want it anymore
}
// Do a lot more stuff
return;
}
这将导致缓冲区超出范围并被释放。不过要小心,你在额外的 { } 中声明的任何其他内容都会超出范围并消失!
编辑:该死的,评论是对的。超出范围的静态字符串没有任何好处。编辑所以它只是一个数组。