Difference between memory of int type in C++ and C# [closed]
In C#
int type variable take 4 byte
m开发者_如何学JAVAemory and in c++
it take 2 byte
memory. Even in c++ short
and int
both take 2 byte
memory and long
takes 4 byte
. And in C# the short and int just take 4 byte. Why is this difference of memory in both languages while both follow the OOPS?
An integer that takes 2 bytes can only have 65536 different values. They are just different views on the same thing, this would mean that in the implementation of C++ that you are using:
C++ C#
1 byte byte byte
2 bytes int/short short
4 bytes long int
etc.
C++ is not more memory efficient on these numbers, the keywords just have different meaning.
In C#:
- int is always System.Int32
- short is always System.Int16
- long is always System.Int64
And this is because C# is compiled to CIL.
In C++ it depends on the architecture:
On 32-bit:
int and long are usually 4-bytes
short is usually 2-bytes
On 64-bit depends on the platform but I haven't ever seen 2-byte int in C++.
The main difference is compilation into native code in C++ and compilation into CIL in C#.
On platforms where C# has a 4 byte int, C++ has that as well.
The difference is that C++ can also run on different platforms, with other sizes for the built in types.
精彩评论