【发布时间】:2014-01-12 11:53:14
【问题描述】:
我想知道在这种情况下,编译器是否会将 int 变量大小调整为最大可能值?还是会使用整个 32 位整数?
pseudocode:
int func()
{
if (statement)
return 10;
else if (statement2)
return 50;
else
return 100;
}
// how much memory will be alocated as it needs only 1 byte?
【问题讨论】:
-
所需的内存取决于函数结果的使用位置。
-
int使用实现定义的固定字节长度(通常为 4)。 -
@OliCharlesworth 因此,如果我将其分配给 short (short i = func()),它将分配 int 大小,然后将其转换为 short?还是只会分配短尺寸?
-
@user2843974,前者,但你必须意识到寄存器中的内容比
short大。