简体   繁体   中英

Is the final memory address of declared functions in any programming language (C++, C#, Java, C…) relative or absolute?

Is the final memory address of declared functions in any programming language (C++, C#, Java, C...) relative or absolute? (I am not asking the addressing mode of machine code language s relative or absolute)

(I would say it is relative but I need expert to comment it) if it is relative, this means no matter where we call the function, the function will be copied and put into a stack based on program counter position. Therefore, two threads calling the same function at the same time, they will not affect with each other if they do not share the same variable.

if it is absolute, this means two threads call the same function, they will enter the same address!!(does it depend on static or non-static function) Besides, every time we create a class object, a certain size of each object may be used to store methods(functions). Then it will make the size of an object very large!

You are wrong.

The virtual memory address of function in the main executable is absolute, the physical memory address can even change while the function is running (if the OS swaps out the process).

For libraries, the virtual address may be absolute or relative. If it is relative, this is called "position-independent code", and is useful if the library can't load at its preferred address.

Code is not placed on the stack. Local (automatic duration) variables usually are, which means separate copies for concurrent and recursive/re-entrant invocations. Since code is read-only, having multiple threads accessing the same address is no problem.

JITted languages determine the address of each function at runtime, during conversion from intermediate language (such as Java bytecode) to machine code.

Functions are not stored inside objects either. Oftentimes an object has a pointer to an array of pointers to functions, for polymorphism support (this is called virtual functions). These function pointers are accessed relative to the object, but the final code address is absolute (again, only the virtual address).

And if the function gets inlined, then it may exist only as pieces mixed into other functions.

In the case of the Java programming language you cannot modify the bytecode of a function at runtime (although you can dynamically create and load new classes). On many modern machines you cannot both execute and modify the same location in memory so this is again impossible. Even when it is allowed by the operating system no sane code is going to do it.

Local variables are typically stored on the stack, not with the function instructions. In the case of static local variables in C these will be stored in the global program text, again not with the function.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM