C#结构体大小如何计算?结构体中有个数不确定的List
//结构体ScheduleFile[StructLayout(LayoutKind.Sequential,Pack=1)]structScheduleFile{publi...
//结构体ScheduleFile
[StructLayout (LayoutKind.Sequential,Pack =1)]
struct ScheduleFile
{
public Int32 schedule_id;
public byte[] basic_schedule_name=new byte [BASIC_SCHEDULE_NAME_LEN];
public Int32 table_count;//服务个数(表个数)
public Table []table_list;//服务列表
}
//结构体Table
[StructLayout(LayoutKind.Sequential,Pack =1)]
struct Table
{
public Int32 table_id;
public Int32 trip_cnt;
public Trip []trip_list;
}
//结构体Trip
[StructLayout(LayoutKind.Sequential,Pack =1)]
struct Trip//单程
{
public byte[] local_sub_id=new byte [LOCAL_SUB_ID_LEN];
public byte[] global_sub_id=new byte [GLOBAL_SUB_ID_LEN];
public ushort trip_flag;
public ushort route_id;
public ushort loop_id;
public byte[] destination_code=new byte [DESTINATION_CODE_LEN];
public ushort record_cnt;
public Record []record_list;
}
//结构体Record
[StructLayout(LayoutKind.Sequential,Pack =1)]
struct Record//记录
{
public ushort station_id;
public ushort platform_id;
public DateTime arrival_time;
public DateTime departure_time;
public Int32 performance_level;
public ushort record_flag;
}
如何计算这样结构体的大小?各列表的个数通过读文件获得 展开
[StructLayout (LayoutKind.Sequential,Pack =1)]
struct ScheduleFile
{
public Int32 schedule_id;
public byte[] basic_schedule_name=new byte [BASIC_SCHEDULE_NAME_LEN];
public Int32 table_count;//服务个数(表个数)
public Table []table_list;//服务列表
}
//结构体Table
[StructLayout(LayoutKind.Sequential,Pack =1)]
struct Table
{
public Int32 table_id;
public Int32 trip_cnt;
public Trip []trip_list;
}
//结构体Trip
[StructLayout(LayoutKind.Sequential,Pack =1)]
struct Trip//单程
{
public byte[] local_sub_id=new byte [LOCAL_SUB_ID_LEN];
public byte[] global_sub_id=new byte [GLOBAL_SUB_ID_LEN];
public ushort trip_flag;
public ushort route_id;
public ushort loop_id;
public byte[] destination_code=new byte [DESTINATION_CODE_LEN];
public ushort record_cnt;
public Record []record_list;
}
//结构体Record
[StructLayout(LayoutKind.Sequential,Pack =1)]
struct Record//记录
{
public ushort station_id;
public ushort platform_id;
public DateTime arrival_time;
public DateTime departure_time;
public Int32 performance_level;
public ushort record_flag;
}
如何计算这样结构体的大小?各列表的个数通过读文件获得 展开
1个回答
展开全部
计算这些结构体时,注意以下几点:
1)结构体成员如果类实例、数组,则其在结构体中保存的引用。
在32位模式下,每个引用占用4个字节(32位);
在64位模式下,每个引用占用8个字节(64位)
2)标准数据类型
short/ushort:2字节
int/int32/uint:4字节
DateTime: 8字节
============
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;
namespace ConsoleSizeOf
{
[StructLayout (LayoutKind.Sequential,Pack =1)]
struct ScheduleFile
{
public Int32 schedule_id;
public byte[] basic_schedule_name;
public Int32 table_count;//服务个数(表个数)
public Table []table_list;//服务列表
DateTime t;
}
//结构体Table
[StructLayout(LayoutKind.Sequential,Pack =1)]
struct Table
{
public Int32 table_id;
public Int32 trip_cnt;
public Trip []trip_list;
}
//结构体Trip
[StructLayout(LayoutKind.Sequential,Pack =1)]
struct Trip//单程
{
public byte[] local_sub_id;
public byte[] global_sub_id;
public ushort trip_flag;
public ushort route_id;
public ushort loop_id;
public byte[] destination_code;
public ushort record_cnt;
public Record []record_list;
}
//结构体Record
[StructLayout(LayoutKind.Sequential,Pack =1)]
struct Record//记录
{
public ushort station_id;
public ushort platform_id;
public DateTime arrival_time;
public DateTime departure_time;
public Int32 performance_level;
public ushort record_flag;
}
class Program
{
static void Main(string[] args)
{
ScheduleFile sf = new ScheduleFile();
Console.WriteLine("SizeOf(ScheduleFile)={0}", Marshal.SizeOf(sf));
sf.basic_schedule_name = new byte[100];
Console.WriteLine("SizeOf(ScheduleFile)={0}, 数组basic_schedule_name已赋值!",
Marshal.SizeOf(sf));
sf.table_list = new Table[100];
Console.WriteLine("SizeOf(ScheduleFile)={0}, 数组table_list已赋值!",
Marshal.SizeOf(sf));
Table tb = new Table();
Console.WriteLine("SizeOf(Table)={0}", Marshal.SizeOf(tb));
Trip tp = new Trip();
Console.WriteLine("SizeOf(Trip)={0}", Marshal.SizeOf(tp));
Record rc = new Record();
Console.WriteLine("SizeOf(Trip)={0}", Marshal.SizeOf(rc));
}
}
}
以下结果是在64位系统运行的结果
注意红色方框中的计算结果!
光点科技
2023-08-15 广告
2023-08-15 广告
通常情况下,我们会按照结构模型把系统产生的数据分为三种类型:结构化数据、半结构化数据和非结构化数据。结构化数据,即行数据,是存储在数据库里,可以用二维表结构来逻辑表达实现的数据。最常见的就是数字数据和文本数据,它们可以某种标准格式存在于文件...
点击进入详情页
本回答由光点科技提供
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询