c# why is "unsafe" out of range between Application Address
Asked Answered
F

2

6

While studying the pointer through unsafe, I noticed something strange.

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

1st, why is it outside the start and end addresses of the applications?
I know it's divided into a heap and a stack, but I've added a class, but the results are the same. It's out of range.

2nd, Why is so much memory used?
When I added one int, I found that the amount of memory added is 24. Because all types inherit objects?

Please let me know if there is a problem with the above code.

Frederique answered 3/7, 2018 at 11:48 Comment(3)
"When I added one int," needs a better specification.Martingale
You are displaying data addresses, specifically addresses located on the main thread's stack. MainModule.BaseAddress is only relevant to code addresses. Double whammy for a C# program btw, its code is dynamically generated and can be located anywhere. It also will not repeat between runs, a strong anti-malware feature.Gettysburg
ModuleMemorySize does not include any additional memory allocations that the module makes once it is running; it includes only the size of the static code and data in the module file. - So this doesn't include the stack.Ladino
M
4

1st, why is it outside the start and end addresses of the applications?

You only show it is out of range for the Main Module. A process can have more modules.
And I don't think that the stack is inside any module's 'memory range'.

2nd, Why is so much memory used?

Why not? It's all virtual.

Martingale answered 3/7, 2018 at 11:57 Comment(2)
2nd thing, i mean why "int" used 24byte. i just added in Main Func() int x = 122423. could you tell me how it is allocated in memory.Frederique
quoting this answer For 32-bit, each int is 4 byte and 4 byte also for reference to the object, that makes 4 * 4 + 4 = 20 byte For 64-bit, each int is 4 byte and 8 byte also for reference to the object, that makes 4 * 4 + 8 = 24 bytePhiz
S
1

The module's size is 32K. That largely comprises of headers and code.

The non-static variables you declare here are allocated from the stack, which is set up dynamically when the process starts.

Sura answered 3/7, 2018 at 11:56 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.