开发者

Difference between memory of int type in C++ and C# [closed]

开发者 https://www.devze.com 2023-03-25 03:40 出处:网络
It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical andcannot be reasonably answered in its current form. For help clari
It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center. Closed 11 years ago.

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号