Is class member function code memory allocated once or at every instantiation of objects?

荒凉一梦 提交于 2020-01-04 13:31:13

问题


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


回答1:


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.



来源:https://stackoverflow.com/questions/5770151/is-class-member-function-code-memory-allocated-once-or-at-every-instantiation-of

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!