Is class member function code memory allocated onc

2019-08-11 14:17发布

问题:

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.