c - 在 C 函数中延迟创建自动变量

标签 c memory assembly compiler-optimization dynamic-allocation

我对这样的 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/

相关文章:

c - 如何将 C#defines 包含在 .icf 链接器文件中?

c - 设置 n 个最高位

c - (作业)将 C 递归函数翻译为汇编(AT&T 语法)

字符处理和字符串操作

c - Flex 和 Bison,检测宏语句(新手)

linux - 内存调试

c - 什么语言允许我与操作系统的内核交互

assembly - push myVar , push [myVar] 和 push OFFSET myVar 之间的区别

memory - 如何处理 iOS 8 照片扩展中的内存限制?

python - 我想读取大量图像进行深度学习,但是内存不足时如何解决?