zoukankan      html  css  js  c++  java
  • C#语言的结构体布局

    C#语言的结构体是一个比较复杂的东西,在此之上有很多需要设置的参数,否则用起来就很容易出错。

    通过使用属性可以自定义结构在内存中的布局方式。例如,可以使用 StructLayoutLayoutKind.Explicit) 和 FieldOffset 属性创建在 C/C++ 中称为联合的布局。

    Code
    [System.Runtime.InteropServices.StructLayout(LayoutKind.Explicit)]  
    struct TestUnion  
    {  
    [System.Runtime.InteropServices.FieldOffset(
    0)]  
    public int i;  
    [System.Runtime.InteropServices.FieldOffset(
    0)]  
    public double d;  
    [System.Runtime.InteropServices.FieldOffset(
    0)]  
    public char c;  
    System.Runtime.InteropServices.FieldOffset(
    0)]  
    public byte b;  


     

    在上一个代码段中,TestUnion 的所有字段都从内存中的同一位置开始。

    以下是字段从其他显式设置的位置开始的另一个示例。

    Code
    [System.Runtime.InteropServices.StructLayout(LayoutKind.Explicit)] 
    struct TestExplicit  
    {  
    [System.Runtime.InteropServices.FieldOffset(
    0)]  
    public long lg;  
    [System.Runtime.InteropServices.FieldOffset(
    0)]  
    public int i1;  
    [System.Runtime.InteropServices.FieldOffset(
    4)]  
    public int i2;  
    [System.Runtime.InteropServices.FieldOffset(
    8)]  
    public double d;  
    [System.Runtime.InteropServices.FieldOffset(
    12)]  
    public char c;  
    [System.Runtime.InteropServices.FieldOffset(
    14)]  
    public byte b;  


     

    i1 i2 这两个 int 字段共享与 lg 相同的内存位置。使用平台调用时,这种结构布局控制很有用。

  • 相关阅读:
    Java8新特性
    中文乱码常见解决方案
    Btrace的使用方法
    jquery获取的html元素和document获取的元素的区别
    Easy UI分页控件修改刷新方法后触发两次请求
    js获取字符串的实际长度并截断实际长度
    js生成唯一的uuid
    easy UI动态赋值
    springmvc+shiro认证框架配置
    shiro配置unauthorizedUrl,无权限抛出无权限异常,但是不跳转
  • 原文地址:https://www.cnblogs.com/myparamita/p/1568893.html
Copyright © 2011-2022 走看看