简体   繁体   English

引用空间创建的变量将存储在哪里? 它们将如何被引用到它们的参考空间?

[英]Where will the variables created by reference space be stored? How will they be referenced to their reference space?

I read that local variables will be stored in the stack memory and the reference variables will be stored in the heap memory.我读到局部变量将存储在堆栈内存中,而引用变量将存储在堆内存中。

Now I have a case where a local object is being created in a reference space.现在我有一个在参考空间中创建本地对象的情况。 So where will that object be saved, and how will that object be referenced?那么该对象将保存在哪里,以及如何引用该对象?

I'm trying to find out what variables are stored in stack memory and heap memory.我试图找出哪些变量存储在堆栈内存和堆内存中。

Employee.cs员工.cs

        class Employee
        {
            int EmpId;
            public int GetEmpId()
            {
                 return this.EmpId ;
            }

            public void SetEmpId(int EmpId)
            {
                this.EmpId = EmpId;
            }

            public Job j = new Job();
        }

Job.cs作业.cs

    class Job
    {
        int JobId;

        public int GetJobId()
        {
             return this.JobId ;
        }

        public void SetJobId(int JobId)
        {
            this.JobId = JobId;
        }

    }

Program.cs程序.cs

    class Program
    {
        static int a =1;
        static void Main(string[] args)
        {
            Employee E1 = new Employee();
            Employee E2 = new Employee();

            E1.SetEmpId(2595);
            E1.j.SetJobId(25);

            Console.WriteLine(E1.GetEmpId());
            Console.WriteLine(E1.j.GetJobId());

        }

    }

I'm using Visual Studio Code.我正在使用 Visual Studio 代码。

Can anyone please help me in finding what goes into stack memory and what goes into heap memory?任何人都可以帮助我找到进入堆栈内存的内容以及进入堆内存的内容吗?

Stack space is used for passing arguments to a method and for local variables defined within a method.堆栈空间用于将参数传递给方法和在方法中定义的局部变量 Local variables of a reference type will be stored on the heap and pointer to that objects will be stored on stack.引用类型的局部变量将存储在堆中,指向该对象的指针将存储在堆栈中。 But, local variables of a value type will be stored on stack.但是,值类型的局部变量将存储在堆栈中。

By the way, stack space is owned by threads.顺便说一下,堆栈空间由线程拥有。 The default stack size is 1MB.默认堆栈大小为 1MB。 Each thread gets a stack, while there's typically only one heap for the application.每个线程都有一个堆栈,而应用程序通常只有一个堆。

And objects are stored on the heap alongside with all their fields and properties.对象与其所有字段和属性一起存储在堆中。

Now, let's go line by line.现在,让我们一行一行地进行。

static int a = 1;

Static fields will be stored in the heap alongside with Type object.静态字段将与Type对象一起存储在堆中。 Because, static fields actually related to Type object which will be created automatically by CLR on first access to the instance of that Type or directly to the Type .因为,静态字段实际上与Type ,它会自动由CLR在第一次访问该实例被创建的对象Type或直接Type That Type object contains all type related information.Type对象包含所有与类型相关的信息。

Then:然后:

  Employee E1 = new Employee();
  Employee E2 = new Employee();

You have two Employee objects on the heap.堆上有两个Employee对象。 And addresses of that objects in the stack( E1 and E2 ).以及堆栈中该对象的地址( E1E2 )。 But, if Employee was a struct, then E1 and E2 will be stored on stack alongside with all information.但是,如果Employee是一个结构体,那么E1E2将与所有信息一起存储在堆栈中。 If that struct has some field of a reference type, then data of that field will be stored on heap and address of that field will be stored on stack inside Employee .如果该结构具有某个引用类型的字段,则该字段的数据将存储在堆中,而该字段的地址将存储在Employee内部的堆栈中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM