110,538
社区成员
发帖
与我相关
我的任务
分享
string a = "hello!";
unsafe
{
fixed (char* p = a)
{
Console.WriteLine("Ptr: " + ((int)p).ToString());
Console.WriteLine("Len: " + (*((int*)p - 1) & 0x3fffffff).ToString());
}
}
string str = "hello!";
//获取大小
int size = str.Length * 2;//内存中肯定是Unicode存放的,所以占用的内存为字符串长度乘以2
GCHandle handle = GCHandle.Alloc(str, GCHandleType.Pinned);
//获取地址
IntPtr address = handle.AddrOfPinnedObject();