方法的代码块在执行时是否存在于堆栈或堆中? [英] Does the Code Block of a method live in the stack or heap at the moment of execution?

查看:218
本文介绍了方法的代码块在执行时是否存在于堆栈或堆中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对学习编程语言还比较陌生,并且我对面向对象编程语言(尤其是C#语言)有20%到25%的理解.因此,我确实陈述了这个问题,却不知道答案对于我学习语言的实际意义(如果有的话),但是我真的觉得我需要提出这个问题.

I'm relatively new to learning programming languages, and I feel I have 20 to 25% of understanding of Object Oriented Programming Language, more specifically C# language. So I really state this question without knowing the actual significance of its answer, if any, to my process of learning the language, but I really felt I need to ask it.

当一个方法被调用执行时,我知道它的所有局部变量及其参数和返回值实际上都存在于堆栈存储器中.虽然方法本身是作为实例化对象或静态类的成员,还是作为非静态类的a的静态成员从堆内存中调用的.

When a method is called for execution, I know that all its local variables and its parameters and return value are actually present in the stack memory. While the method itself is called from the heap memory as a member of an instantiated object or a static class, or as a static member of a of a non static class.

现在我的问题是,当该方法被调用到堆栈中时,是只是将要存在于堆栈中的变量和参数,还是整个方法及其所有代码块都将存在于堆栈中? (执行)在那个时候堆栈?

Now my question here is, when the method is called into the stack, is it only the variables and parameters that are going to exist in the stack, or is it the whole method with all its code block will be existing in the stack at that moment ( of execution )?

此查询来自于实例化方法(或静态方法)中代码块的本质的相似比较,与之相比,在调用该方法的同时,在未调用该方法的情况下,非静态类的成员,而该类被实例化为一个对象,而不是实例化.

This query has arisen from the analogous comparison of nature of the code block inside an instantiated method ( or a static method ), while the method is being called and while it isn't being called, when compared to the nature of the members of a non static class while the class is instantiated into an object, and while not.

现在,非静态类的成员被认为是一个蓝图,即它们以不可接近且不起作用的形式存在(无法调用字段和方法,并且字段的值不能更改时,方法不能更改值),但是此蓝图可以实例化为具有其成员的具体功能对象.

Now, the members of a nonstatic class, are thought of like a blue print, i.e. they are existing in a form that is non-approachable and non-functional ( fields and methods cannot be called, and the values of fields cannot get changed, the methods cannot change values ), but this blueprint is rather instantiable into a concrete functioning object with its members.

现在,如果堆中实例化方法内的代码块不过是一个蓝图,那么当调用该方法时,该蓝图实际上将在堆栈中得到实例化",以执行其在堆栈中的任务,然后完成任务后,将其从堆栈中删除.在这里,栈可以看作是程序实际执行的地方,而另一方面,堆中的所有东西(包括静态类和对象)以及堆本身都将被视为仅仅是存储数据和指令的地方.堆栈时不时地借用和利用,堆栈实际上执行了我们整个程序的任务.

Now if the code block inside an instantiated method in the heap is nothing but a blueprint, a blueprint that will practically get "instantiated" in the stack when the method is being called, to perform the task their in the stack, then get deleted off the stack when the task is accomplished. Here the stack can be seen as the place of actual execution of the program, while on the other hand everything in the heap including the static classes and the objects and the heap itself will be seen as a mere storage place for data and instructions for the stack to borrow and utilise every now and then, the stack actually performs the tasks of our whole program.

但是,如果堆栈内存实际上不包含正在执行的方法的代码,并且堆栈仅采用该方法的局部变量和参数的临时值,而该方法本身在堆中并发从堆位置执行编码指令,在过程中仅将值借给堆栈.在这里,堆栈看起来就像一个变量的值持有者,而对象和静态类及其方法是它们在堆本身中的程序的实际执行者,并且具体存在带有其代码的实例化方法(或静态方法)并在堆中发挥作用.

If, however, the stack memory doesn't actually contain the code of a method that is getting executed, and the stack only takes the temporary values of the method's local variables and parameters, while the method itself in the heap and concurrently performing the coded instructions from its heap position, loaning only the values to the stack in the process. Here the stack will look like a mere variable's value holder, while the object and static classes with their methods are the actual performers of the program their in the heap itself, and an instantiated method ( or a static one ) with its code is concretely present and functioning in the heap.

第三种可能性是两个内存(堆栈和堆)都不是代码执行的实际位置,而是在处理器本身中发生数据执行和更改的某个地方,同时堆和堆栈都在仅仅是在接受,保存和清除数据和指令方面用于不同使用方式的存储位置,就是这样.

A third possibility is that neither of the two memories ( stack and heap ) are the actual place of code execution, rather somewhere in the processor itself that the execution and changing of data is taking place, with both the heap and stack being mere storage places for different patterns of utilisation in terms of accepting, preserving, and cleaning data and instructions, and that's it.

对于这么长的问题,我感到抱歉,我不知道如何以程序员的身份为我解答,但确实让我头疼了几天,而且我找不到答案.专为初学者设计的文字,让我真的不知所措!

I'm sorry for such a long question, I don't know how helpful to gain its answer for me as a programmer, but it really caused me a headache for couple of days and I couldn't find an answer in the text that are designed for beginners so I got really overwhelmed!

推荐答案

未实例化方法 .实例化 类以创建对象.

Methods are not instantiated. Classes are instantiated in order to create objects.

对象由数据成员方法组成.动态地仅将数据成员分配到进程内存中的某个位置.所有方法的代码都静态地位于称为代码段"的内存区域中.任何方法的代码都不会被复制.因为它是完全恒定的,所以不需要它.

Objects consist of data members and methods. Only the data members are allocated either somewhere in the process'es memory dynamically. The code of all methods is statically located in a section of memory called 'code segment'. No code of any method is ever copied. It is not needed since it's perfectly constant.

堆栈与代码无关.只有局部变量和参数存在于堆栈中.请注意,如果变量/参数的类型是引用,则仅引用(指针)的值存在于堆栈中,但其指向的实际对象位于其他位置.

Stack has nothing to do with code. Only local variables and parameters live on the stack. Note that if the type of the variable / parameter is a reference, then just the value of the reference (pointer) lives on the stack, but the actual object it's pointing to is located somewhere else.

一篇介绍.NET内存管理基础知识的文章

An article providing an introduction into .NET's memory management basics can be found here.

注意:这有点简化了视图,但对于您的知识水平却是准确的.

Note: This is a little bit simplifying view, but accurate for your level of knowledge.

这篇关于方法的代码块在执行时是否存在于堆栈或堆中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆