类成员函数代码内存是分配一次还是在对象的每次实例化时分配?
我对这个问题有疑问,而不是相对于特定的语言:当我编写一个类时,也许是用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?
thanks for replies
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
不,当操作系统将程序加载到内存中时,代码的数据部分与可执行部分分开加载。它们通常驻留在不同的内存区域(通常,可执行部分是只读部分)。
因此,当调用方法时,它基本上会跳转到可执行部分,并在堆栈上使用不同的上下文。
您可以查看这篇关于链接器的优秀文章:
http://www.lurklurk.org/linkers/linkers.html
您将了解如何程序被加载到内存中并执行。
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.
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.