Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

c#

unsafe

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.

like image 761
angdroid Avatar asked Jul 03 '18 11:07

angdroid


People also ask

What C is used for?

C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...

What is the full name of C?

In the real sense it has no meaning or full form. It was developed by Dennis Ritchie and Ken Thompson at AT&T bell Lab. First, they used to call it as B language then later they made some improvement into it and renamed it as C and its superscript as C++ which was invented by Dr.

Is C language easy?

C is a general-purpose language that most programmers learn before moving on to more complex languages. From Unix and Windows to Tic Tac Toe and Photoshop, several of the most commonly used applications today have been built on C. It is easy to learn because: A simple syntax with only 32 keywords.

Is C programming hard?

C is more difficult to learn than JavaScript, but it's a valuable skill to have because most programming languages are actually implemented in C. This is because C is a “machine-level” language. So learning it will teach you how a computer works and will actually make learning new languages in the future easier.


2 Answers

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.

like image 110
bommelding Avatar answered Oct 13 '22 09:10

bommelding


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.

like image 37
500 - Internal Server Error Avatar answered Oct 13 '22 07:10

500 - Internal Server Error