Marshal.SizeOf返回值不正确?

zhangzhentao 2013-03-11 08:29:28
using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;

namespace MarshalTest
{
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)]
public struct FileInfo_s
{
public Int32 mBlockNum; //4 Bytes

[MarshalAs(UnmanagedType.ByValArray, SizeConst = 32)] //32 Bytes
public char[] mFileName;

[MarshalAs(UnmanagedType.ByValArray, SizeConst = 33)] //33 Bytes
public char[] mMd5;
};

class Program
{
static void Main(string[] args)
{
FileInfo_s fileInfo = new FileInfo_s();
Console.WriteLine(Marshal.SizeOf(fileInfo)); //72 ?
}
}
}

很奇怪为什么输出的是72个字节,而不是4+32+33 = 69个字节?
...全文
254 8 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
真相重于对错 2013-03-11
  • 打赏
  • 举报
回复
http://baike.baidu.com/view/4786260.htm
gxingmin 2013-03-11
  • 打赏
  • 举报
回复
一个char只占1个字节
zhangzhentao 2013-03-11
  • 打赏
  • 举报
回复
public Int32 mBlockNum正好为4个字节,且位置在该结构中的第一个,按理说不该影响后面的char?
zhangzhentao 2013-03-11
  • 打赏
  • 举报
回复
引用 2 楼 hjywyj 的回复:
http://blog.csdn.net/wyqlxy/article/details/6608819
谢谢hjywyj和gxingmin,基本明白了。 但有一个小疑惑,如果我去掉public Int32 mBlockNum; ,按照引用文章“C#中alignment的字节数为4个字节"所述,推理结果应该依然是4的倍数(即68),然而实际却正常输出了65 ? using System; using System.Collections.Generic; using System.Text; using System.Runtime.InteropServices; namespace MarshalTest { [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)] public struct FileInfo_s { //public Int32 mBlockNum; //4 Bytes [MarshalAs(UnmanagedType.ByValArray, SizeConst = 32)] //32 Bytes public char[] mFileName; [MarshalAs(UnmanagedType.ByValArray, SizeConst = 33)] //33 Bytes public char[] mMd5; }; class Program { static void Main(string[] args) { FileInfo_s fileInfo = new FileInfo_s(); Console.WriteLine(Marshal.SizeOf(fileInfo)); //65 } } }
Monkey_ye 2013-03-11
  • 打赏
  • 举报
回复
第一次知道还有内存对齐的情况
wetcom 2013-03-11
  • 打赏
  • 举报
回复
引用 2 楼 hjywyj 的回复:
http://blog.csdn.net/wyqlxy/article/details/6608819
楼上正解
gxingmin 2013-03-11
  • 打赏
  • 举报
回复
ini读写代码详解实例,有很详细注释,保证满意; 预览截取一部分代码: /// mary>   /// 读取INI文件中指定INI文件中的所有节点名称(Section)   /// mary>   /// Ini文件   /// 所有节点,没有内容返回string[0]   public static string[] INIGetAllSectionNames(string iniFile) { uint MAX_BUFFER = 32767; //默认为32767   string[] sections = new string[0]; //返回值   //申请内存   IntPtr pReturnedString = Marshal.AllocCoTaskMem((int)MAX_BUFFER * sizeof(char)); uint bytesReturned = GetPrivateProfileSectionNames(pReturnedString, MAX_BUFFER, iniFile); if (bytesReturned != 0) { //读取指定内存的内容   string local = Marshal.PtrToStringAuto(pReturnedString, (int)bytesReturned).ToString(); //每个节点之间用\0分隔,末尾有一个\0   sections = local.Split(new char[] { '\0' }, StringSplitOptions.RemoveEmptyEntries); } //释放内存   Marshal.FreeCoTaskMem(pReturnedString); return sections; } /// mary>   /// 获取INI文件中指定节点(Section)中的所有条目(key=value形式)   /// mary>   /// Ini文件   /// 节点名称   /// 指定节点中的所有项目,没有内容返回string[0]   public static string[] INIGetAllItems(string iniFile, string section) { //返回值形式为 key=value,例如 Color=Red   uint MAX_BUFFER = 32767; //默认为32767   string[] items = new string[0]; //返回值   //分配内存   IntPtr pReturnedString = Marshal.AllocCoTaskMem((int)MAX_BUFFER * sizeof(char)); uint bytesReturned = GetPrivateProfileSection(section, pReturnedString, MAX_BUFFER, iniFile); if (!(bytesReturned == MAX_BUFFER - 2) || (bytesReturned == 0)) { string returnedString = Marshal.PtrToStringAuto(pReturnedString, (int)bytesReturned); items = returnedString.Split(new char[] { '\0' }, StringSplitOptions.RemoveEmptyEntries); } Marshal.FreeCoTaskMem(pReturnedString); //释放内存   return items; }

111,092

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • AIGC Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧