简体   繁体   中英

Does compiler adjust int size?

I wonder if in that case, compiller will adjust int variable size to its maximum possible value? Or will it use whole 32 bit 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).

It is defined at compile time

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

Int32 is value type. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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