c# 为什么 "unsafe" 超出应用地址范围
c# why is "unsafe" out of range between Application Address
在通过unsafe研究指针的过程中,我发现了一些奇怪的东西。
unsafe class Program
{
static unsafe void Main(string[] args)
{
int A = 111;
int B = 222;
int* C = &A;
Console.WriteLine("{0} A", (int)&A);
Console.WriteLine("{0} B", (int)&B);
Console.WriteLine("{0} *C", (int)*C);
Console.WriteLine("{0} &C", (int)&C);
Console.WriteLine("{0} C", (int)C);
Process proc = Process.GetCurrentProcess();
IntPtr startOffset = proc.MainModule.BaseAddress;
IntPtr endOffset = IntPtr.Add(startOffset, proc.MainModule.ModuleMemorySize);
Console.WriteLine("{0} ~ {1} original", startOffset, endOffset);
Console.WriteLine("{0}", (int)endOffset-(int)startOffset);
long memory = GC.GetTotalMemory(true);
Console.WriteLine("{0} memory", memory);
}
}
result
11530536 A
11530532 B
111 *C
11530528 &C
11530536 C
7143424 ~ 7176192 original
32768
33448 memory
1、为什么在应用程序的起始地址和结束地址之外?
我知道它分为堆和栈,但是我加了一个class,但是结果是一样的。它超出了范围。
2、为什么占用这么多内存?
当我加一个int的时候,发现加的内存量是24。
因为所有类型都继承对象?
如果上面的代码有问题,请告诉我。
模块大小为32K。这主要包括 headers 和代码。
你在这里声明的non-static变量是从栈中分配的,栈是在进程启动时动态设置的。
1st, why is it outside the start and end addresses of the applications?
你只是表明它超出了 Main 模块的范围。一个进程可以有多个模块。
而且我认为堆栈不在任何模块的 'memory range' 中。
2nd, Why is so much memory used?
为什么不呢?都是虚拟的。
在通过unsafe研究指针的过程中,我发现了一些奇怪的东西。
unsafe class Program
{
static unsafe void Main(string[] args)
{
int A = 111;
int B = 222;
int* C = &A;
Console.WriteLine("{0} A", (int)&A);
Console.WriteLine("{0} B", (int)&B);
Console.WriteLine("{0} *C", (int)*C);
Console.WriteLine("{0} &C", (int)&C);
Console.WriteLine("{0} C", (int)C);
Process proc = Process.GetCurrentProcess();
IntPtr startOffset = proc.MainModule.BaseAddress;
IntPtr endOffset = IntPtr.Add(startOffset, proc.MainModule.ModuleMemorySize);
Console.WriteLine("{0} ~ {1} original", startOffset, endOffset);
Console.WriteLine("{0}", (int)endOffset-(int)startOffset);
long memory = GC.GetTotalMemory(true);
Console.WriteLine("{0} memory", memory);
}
}
result
11530536 A
11530532 B
111 *C
11530528 &C
11530536 C
7143424 ~ 7176192 original
32768
33448 memory
1、为什么在应用程序的起始地址和结束地址之外?
我知道它分为堆和栈,但是我加了一个class,但是结果是一样的。它超出了范围。
2、为什么占用这么多内存?
当我加一个int的时候,发现加的内存量是24。
因为所有类型都继承对象?
如果上面的代码有问题,请告诉我。
模块大小为32K。这主要包括 headers 和代码。
你在这里声明的non-static变量是从栈中分配的,栈是在进程启动时动态设置的。
1st, why is it outside the start and end addresses of the applications?
你只是表明它超出了 Main 模块的范围。一个进程可以有多个模块。
而且我认为堆栈不在任何模块的 'memory range' 中。
2nd, Why is so much memory used?
为什么不呢?都是虚拟的。