简体   繁体   English

编译器会调整int大小吗?

[英]Does compiler adjust int size?

I wonder if in that case, compiller will adjust int variable size to its maximum possible value? 我想知道在这种情况下,编译器是否会将int变量的大小调整为最大可能值? Or will it use whole 32 bit int? 还是会使用整个32位int?

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 ,无论存储在其中的实际值如何,分配的内存将为sizeof(int)

I will use the full 32 bits (assuming that an int is 32 bits on this architecture). 我将使用完整的32位(假设int在此体系结构上为32位)。

It is defined at compile time 它在编译时定义

是的朋友,它将使用整个32位,因为对原始类型的内存分配是在编译时完成的。

Int32 is value type. Int32是值类型。 It is stored on stack on compile time. 它在编译时存储在堆栈中。 If it is inside any object then it will go to heap which is dynamic memory. 如果它在任何对象内,它将进入动态内存堆。

In your case, for any return value, compiler will allocate fixed bits on stack to store your return integer value, according to the size of int32 that is 32 bits, which can have range –2,147,483,648 to 2,147,483,647 if singed and 0 to 4,294,967,295 if unsigned. 在您的情况下,对于任何返回值,编译器都会根据int32的大小(32位)在堆栈上分配固定位来存储您的返回整数值,如果是32位,则范围为–2,147,483,648到2,147,483,647,如果为无符号,则范围是0到4,294,967,295 。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM