首页 > 解决方案 > 表达式是否分配内存?

问题描述

考虑两个代码片段:

if (1 + 2 == 3) {
}

int a = 1;
int b = 2;
int result = 3;
int addition_result = a + b;
bool comparison = addition_result == result;

if (comparison) {
}

这些片段本质上是一样的吗?在第一种情况下,编译器是否在执行时为所有操作数分配内存:

还是第一个片段更有效,因为编译器不必为该表达式分配任何内存?

编译语言和解释语言有什么不同吗?

标签: performancecompilationcoding-stylevariable-assignment

解决方案


推荐阅读