哪里.NET将字符串值? [英] Where does .NET place the String value?
问题描述
我使用SOS调试扩展DLL检查String类型的内存布局。而下面是结果。
I am using SOS debug extension dll to check the memory layout of a String type. And below is the result.
!DSO
ESP/REG Object Name
0015EFC0 01c6b9cc System.String hello,world
!做01c6b9cc
Name: System.String
MethodTable: 6de3f9ac
EEClass: 6db78bb0
Size: 36(0x24) bytes
File: C:\Windows\Microsoft.Net\assembly\GAC_32\mscorlib\v4.0_4.0.0.0__b77a5c561934e089>\mscorlib.dll
String: hello,world
Fields:
MT Field Offset Type VT Attr Value Name
6de42978 40000ed 4 System.Int32 1 instance 11 m_stringLength
6de41dc8 40000ee 8 System.Char 1 instance 68 m_firstChar
6de3f9ac 40000ef 8 System.String 0 shared static Empty
>> Domain:Value 00331488:01c61228 <<
现在我想知道,到底是哪里的字符串值Hello World的存储?
Now I am wondering, where exactly is the string value "hello world" is stored?
感谢。
推荐答案
目前m_firstChar。堆分配足够大,以适应整个字符串,不只是第一个字符。易在Visual Studio中看到还有:
At m_firstChar. The heap allocation is large enough to fit the entire string, not just the first character. Easy to see in Visual Studio as well:
class Program {
static void Main(string[] args) {
string s = "hello" + "world";
} // <=== Breakpoint here
}
在断点命中,使用调试+的Windows +内存+内存1。在地址框中键入秒。你会看到:
When the breakpoint hits, use Debug + Windows + Memory + Memory1. In the Address box type s. You'll see:
0x01B3F6BC e8 0a 67 6e 0b 00 00 00 0a 00 00 00 68 00 65 00 è.gn........h.e.
0x01B3F6CC 6c 00 6c 00 6f 00 77 00 6f 00 72 00 6c 00 64 00 l.l.o.w.o.r.l.d.
- 的对象开始在地址 - 4,syncblk被存储在那里(不可见) 。
- 在接下来的4个字节的方法表指针(0x6e670ae8,又名型手柄)。
- 在接下来的4个字节是m_arrayLength成员,该字符串(0x0B中)的分配大小。
- 接下来的4个字节是m_stringLength构件,字符的串(的0x0A)中的实际数目。
- 在接下来的字节存储字符串,起始于m_firstChar。
- The object starts at the address - 4, the syncblk is stored there (not visible).
- Next 4 bytes is the method table pointer (0x6e670ae8, aka type handle).
- Next 4 bytes is the m_arrayLength member, the allocated size of the string (0x0b).
- Next 4 bytes is the m_stringLength member, the actual number of characters in the string (0x0a).
- Next bytes store the string, starting at m_firstChar.
这是.NET 3.5 SP1。不知道为什么m_arrayLength成员是不是在你的输出中可见。
This is for .NET 3.5 SP1. Not sure why the m_arrayLength member isn't visible in your output.
这篇关于哪里.NET将字符串值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!