需要澄清数据类型的实际大小 [英] Need a clarification regarding actual size of the datatypes

查看:93
本文介绍了需要澄清数据类型的实际大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述





我的问题是关于包含任何变量的类的大小。

如果我的班级只包含Int类型的变量,我的班级大小是4.

你可以在这里查看:



Hi,

My question is regarding the size of the class when it contains any variables.
If my class is containing only a variable of Int type ,my class size is of 4.
you can check here:

using System.Runtime.InteropServices;
   class Program
    {
        
        static void Main(string[] args)
        {
                test p = new test();
            Console.WriteLine("Number of bytes needed by a Point object: {0}",
                Marshal.SizeOf(p));
            Console.ReadLine();

         }
    }
    [StructLayout(LayoutKind.Sequential)]
    class  test 
    {
     int i;
     double d;    
    }





当我在班级中只有int时显示4,如果它只包含双变量则显示8 。

但如果有一个双变量和一个Int变量,则显示16个。

为什么显示16?



我需要澄清。请帮助我!!



在此先感谢

Kabita Nayak



It shows 4 when I've only int in my class, and shows 8 if it contains only double variable.
But it shows 16 if one double and one Int variable is there.
Why it showing 16?

I need a clarification .please help me !!

Thanks in Advance
Kabita Nayak

推荐答案

因为实际内存不是以字节为单位组织的:它以处理器/操作系统大小为单位进行组织:32位或64位。

数据类型必须启动在它的自然边界:对于一个字节,地址可以以任何结尾。对于短(16位),它必须以二进制零结束。一个double(需要64位,或8个字节,宽)需要从一个以二进制000结尾的内存位置开始,以便在它的自然边界内访问。

所以你放一个int在它前面,int被填充以允许双击以达到自然边界。

尝试:在其间放入另一个int:

Because "real" memory isn't organised in bytes: it's organised in units of the processor / OS size: 32 bits or 64 bits.
And a datatype must start on it's "natural" boundary: for a byte, the address can end with anything. For a short (16 bits) it must end with a binary zero. A double (which needs 64 bits, or 8 bytes, wide) needs to start on a memory location that ends with binary 000 in order to be accessed within it's natural boundary.
So when you put an int in front of it, the int is "padded" to allow teh double to hit teh natural boundary.
Try it: put another int in between:
[StructLayout(LayoutKind.Sequential)]
class test
    {
    int i;
    double d;
    }
[StructLayout(LayoutKind.Sequential)]
class test2
    {
    int i;
    int j;
    double d;
    }

你会发现它们都是相同的大小,而这个:

You'll find they are both the same size, while this:

[StructLayout(LayoutKind.Sequential)]
class test3
    {
    int i;
    double d;
    int j;
    }

需要另外8个字节。


这是因为填充。



类实例的大小由以下因素确定:



- 实例中实际存储的数据量

- 值之间需要填充

- 内存管理使用的一些额外内部数据



请查看此anwer: c#中类的大小 [ ^ ]
This is because of padding.

The size of a class instance is determined by:

- The amount of data actually stored in the instance
- The padding needed between the values
- Some extra internal data used by the memory management

Please take a look at this anwer: Size of a class in c#[^]


除了解决方案1-2:在运行时,您始终可以使用 System.Runtime.InteropServices.Marshal.SizeOf(System.Object)来检查结构分配的数量:

http://msdn.microsoft。 COM / EN-US /库/ y3ybkfb 3%28v = vs.100%29.ASPX [ ^ ]。



它将基本上为您提供所需的大小: umnanaged大小以字节为单位。这是您需要在非托管内存中分配的大小,以适合您的对象。这是适合它所需的整个连续内存块的大小。



所以,你需要小心:这个尺寸赢了如果将任何对象放入结构中,则不要考虑引用类型对象的大小;但是,它会给出你在结构中引用这些对象所需的大小。



-SA
In addition to Solutions 1-2: during runtime, you can always check up how much a structure allocates using System.Runtime.InteropServices.Marshal.SizeOf(System.Object):
http://msdn.microsoft.com/en-us/library/y3ybkfb3%28v=vs.100%29.ASPX[^].

It will give you, essentially, the size you need: umnanaged size in byte. This is the size you would need to allocate in unmanaged memory to fit your object in it. This is a size of a whole continuous block of memory needed to fit it.

So, you need to be careful: this size won't take into account the size of reference-type objects, if you put any of them in the structure; however, it will give your the size needed to have references to those objects in the structure.

—SA


这篇关于需要澄清数据类型的实际大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆