sizeof运算符在C#中提供了结构的额外大小 [英] sizeof operator gives extra size of a struct in C#

查看:49
本文介绍了sizeof运算符在C#中提供了结构的额外大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用sizeof运算符检查所有变量(值类型)的大小.我浏览了 msdn文章之一 msdn文章写在哪里

I am trying to check size of all of my variables (value types) using sizeof operator. I gone through one of the msdn article where it is written that

对于所有其他类型(包括结构),sizeof运算符只能在不安全的代码块中使用

For all other types, including structs, the sizeof operator can be used only in unsafe code blocks

结构也不应包含任何引用类型的字段或属性

and also structs should not contain any fields or properties that are reference types

为此,我在项目属性中启用了不安全的编译并按如下所示创建了结构-

For this, I enabled unsafe compilation in my project properties and created structure as follows-

struct EmployeeStruct
{
    int empId;
    long salary;       
}

并按如下方式使用它-

unsafe
{
   size = sizeof(EmployeeStruct);
}

Console.WriteLine("Size of type in bytes is: {0}", size);

在这里,我得到的输出结果是类型的大小(以字节为单位):16但是,通过查看结构,它应该为12(int为4,long为8).有人可以在这里帮助我理解为什么我要多得到4个字节吗?

Here I am getting output as Size of type in bytes is: 16 however by looking at structure it should be 12 (4 for int and 8 for long). Can someone help me understand here that why I am getting 4 byte extra size?

推荐答案

您不需要使用不安全的代码.建议使用System.Runtime.InteropServices.Marshal.SizeOf()

You don´t need to use unsafe code. Is recommended to use System.Runtime.InteropServices.Marshal.SizeOf()

例如:Marshal.SizeOf(new EmployeeStruct());

eg: Marshal.SizeOf(new EmployeeStruct());

返回16而不是12,因为内存中的默认包大小为8.

That return 16 instead of 12, because the default pack size in memory is 8.

因此,针对:

struct EmployeeStruct
{
    int empId; // 4 bytes
    long salary;  8 bytes
}

//返回16而不是12(因为最小单位为8)

//return 16 instead 12 (because de min unit is 8)

针对:

 struct EmployeeStruct
 {
    int empId; // 4 bytes
    int empAge; // 4 bytes
    long salary;  8 bytes
  }

//也返回16

   struct EmployeeStruct
   {
      int empId; // 4 bytes
      int empAge; // 4 bytes
      int IdCompany; // 4 bytes
      long salary;  8 bytes
   }

返回24而不是20(因为最小单位为8)

return 24 instead 20 (because de min unit is 8)

我不知道您想要什么,但是如果您需要每个字段大小的总和,则可以尝试使用此功能:

I don't know what you want, but if you need the sum of each field size, you can try with this function:

    public int SizeOf(Type t)
    {
        int s = 0;
        var fields = t.GetFields(BindingFlags.Public | BindingFlags.Instance | BindingFlags.NonPublic);
        foreach (var f in fields)
        {
            var x = f.FieldType;
            s += x.IsPrimitive ? Marshal.SizeOf(x) : SizeOf(x);
        }

        return s;
    }

对于您的情况,它返回12而不是16,并且您可以将其用于复杂的结构,例如:

It returns 12 instead 16, for your case, and you can use it for complex structures, eg:

    struct EmployeeStruct
    {
        int field1; // 4 bytes
        long field2; // 8 bytes
        Person p; // 12 bytes
    }

    struct Person
    {
        int field1; // 4 bytes
        long field2; // 8 bytes
    }  

SizeOf(typeof(EmployeeStruct)将返回24而不是32,但请记住,MEMORY的REAL SIZE是32,编译器使用32字节分配内存.

SizeOf(typeof(EmployeeStruct) will return 24 instead 32, but remember, the REAL SIZE ON MEMORY is 32, the compiler use 32 bytes to assign memory.

致谢.

这篇关于sizeof运算符在C#中提供了结构的额外大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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