我对这样的 C 代码很好奇:
void test(int y){
if (y) {
int x = 1;
printf("Test: %d", x + y);
}
// other important code (don`t use x var)
}
x
变量的必要性取决于y
。
例如:如果我们调用test(0)
,我们不需要为x
变量分配空间,因为test(1)
调用x
变量必须存在于内存中。
现代编译器是否使用这种技术?
最佳答案
编译器更可能的优化是
void test(int y){
if (y) {
printf("Test: %d", 1 + y);
}
// other important code (don`t use x var)
}
然后在任何一种情况下我们都不需要 x
。
关于c - 在 C 函数中延迟创建自动变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33654641/