sizeof(int) on x64?

0 votes
asked Mar 16, 2009 by jms10

When I do sizeof(int) in my C#.NET project I get a return value of 4. I set the project type to x64, so why does it say 4 instead of 8? Is this because I'm running managed code?

6 Answers

0 votes
answered Jan 16, 2009 by brian-rasmussen

Remember int is just a compiler alias for the basic type Int32. Given that it should be obvious why int is only 32 bits on a 64 bit platform.

0 votes
answered Jan 16, 2009 by ben-s

int means Int32 in .NET languages. This was done for compatibility between 32- and 64-bit architectures.

Here's the table of all the types in C# and what they map to .NET wise.

0 votes
answered Jan 16, 2009 by brian-r-bondy

You may be thinking of an int pointer or System.IntPtr. This would be 8 bytes on an x64 and 4 bytes on an x86. The size of a pointer shows that you have 64-bit addresses for your memory. (System.IntPtr.Size == 8 on x64)

The meaning of int is still 4 bytes whether you are on an x86 or an x64. That is to say that an int will always correspond to System.Int32.

0 votes
answered Jan 16, 2009 by andleer

An Int32 is 4 bytes on x86 and x64. An Int64 is 8 bytes either case. The C# int type is just an alias for System.Int32. Same under both runtime environments. The only type that does change depending on the runtime environment is an IntPtr:

        var size = sizeof(IntPtr); // 4 on x86 bit machines. 8 on x64
0 votes
answered Mar 16, 2009 by andrew-hare

The keyword int aliases System.Int32 which still requires 4 bytes, even on a 64-bit machine.

0 votes
answered Jan 15, 2013 by shafeeq
int i;
int size = BitConverter.GetBytes(i).Lenght;
Welcome to Q&A, where you can ask questions and receive answers from other members of the community.
Website Online Counter