ddl
ddl

Reputation: 329

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?

Upvotes: 1

Views: 151

Answers (4)

Hassan
Hassan

Reputation: 742

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.

Upvotes: -1

Uri Y
Uri Y

Reputation: 850

The function returns int, the allocated memory will be sizeof(int), regardless of the actual value stored in it.

Upvotes: 7

Girish
Girish

Reputation: 1717

Yes friend it will use whole 32 bit because the memory allocation to the primitive types is done at compile time.

Upvotes: 0

Ed Heal
Ed Heal

Reputation: 59987

I will use the full 32 bits (assuming that an int is 32 bits on this architecture).

It is defined at compile time

Upvotes: 4

Related Questions