是类成员函数代码内存分配一次还是每次实例化对象? [英] Is class member function code memory allocated once or at every instantiation of objects?

查看:175
本文介绍了是类成员函数代码内存分配一次还是每次实例化对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个疑问,不是相对于一个特定的语言:当我写一个类,也许在C ++或Java,成员函数代码的内存分配一次或每个实例?

I've a doubt about this question, not relatively to a specific language: when I write a class, maybe in C++ or Java, the memory for member function code is allocated once or at every instance? So, in certain cases, is it better to write them as static?

感谢回复

推荐答案

不,当操作系统将程序加载到内存中时,代码的数据部分将与可执行部分分开加载。它们通常驻留在不同的内存区域(通常,可执行部分是只读部分)。

Nope, the data portion of the code is loaded separately from the executable section when the OS loads your program into memory. They reside usually into different memory regions (typically, the executable section is a read-only section).

因此,当调用方法时,它基本上跳转到可执行部分,在堆栈上有不同的上下文。

So it basically jumps to the executable portion when a method is called, with a different context on the stack.

您可以查看关于链接器的这篇优秀文章:
http://www.lurklurk.org/linkers/linkers.html

You may check this excellent article on linkers: http://www.lurklurk.org/linkers/linkers.html

您将了解如何将程序加载到内存中并执行。

You will understand how a program is loaded into memory, and executed.

这篇关于是类成员函数代码内存分配一次还是每次实例化对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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