[問題] C# sizeof 函式取得 struct(內含複雜型別) 大小
我問的問題跟 4889 篇類似,不過想請教高手建議 C# 中 struct 的用法
之前使用C的結構 struct
============== C =======================
struct
{
unsigned char SlaveAddress;
unsigned char RegisterCount;
unsigned short Clock;
unsigned short line_per_frame;
unsigned short pixel_per_line;
unsigned char dummy[8];
char name[16];
} sensor_para;
int inlen = sizeof( sensor_para );
========================================
可以得到此 struct 的大小 1+1+2+2+2+8+16 = 32
但我用C#時
=================C#=====================
public struct SensorP
{
public byte SlaveAddress;
public byte RegisterCount;
public short Clock;
public short line_per_frame;
public short pixel_per_line;
public static char[] dummy =new char[8];
public static char[] name = new char[16];
};
inlen = sizeof(SensorP);
========================================
此時得到的大小為 1+1+2+2+2 = 8; (若是將 static 修飾字拿掉會有 compiler error)
原本我以為至少會有 dummy , name 的參考值大小 8+4+4 = 20
可是 sizeof 的結果只有計算簡單型別的大小
在C語言中,對struct的運算常常會有像
============== C =======================
memset(sensor_para,0,sizof(sensor_para));
========================================
memory reset 的動作。
那請問一下 C# 高手是如何做到上述的功能或是對於 struct 的使用有什麼建議
謝謝
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 220.132.97.179
→
03/24 22:13, , 1F
03/24 22:13, 1F
推
03/25 00:11, , 2F
03/25 00:11, 2F
推
03/25 07:56, , 3F
03/25 07:56, 3F
推
03/25 07:56, , 4F
03/25 07:56, 4F
→
03/25 08:57, , 5F
03/25 08:57, 5F
推
03/25 10:44, , 6F
03/25 10:44, 6F
推
03/25 11:16, , 7F
03/25 11:16, 7F