when we get the address of a function or any object for that matter,is it the virtual address or physical address of that object??
+7
A:
Are you asking about pointers in general?
On most operating systems, they are logical addresses.
The operating system is responsible for translating them into physical addresses through the virtual memory and paging mechanism. This is transparent to the program. That's why a misguided program "hits the boundaries" and GPFs.
On some old systems (e.g., DOS), they would be physical, allowing you to overwrite stuff in other parts of memory.
Uri
2009-08-24 18:28:00
+5
A:
Depends on the OS and at what level your code is running.
For a normal user-land program on a modern OS, you will get the virtual address.
Tyler McHenry
2009-08-24 18:28:02
but if i run the following prog :int main(){ int (*p)(); p=main;printf("%p",p);}it is giving address like 0x80483c4 ,it think it is physical address ,why will the virtual address of main start at such a large address??i am running this on linux.plz clarify if i am wrong??
mawia
2009-08-24 18:35:21
You are wrong. That is a virtual address. Nobody said that your main() function gets loaded at virtual address 0. In fact, by default in ELF binaries (used in linux) the code segment starts at virtual address 0x80482c0, which looks just about right (there is some hidden set-up code before main()). Keep in mind that the virtual address space is often non-contiguous, and virtual addresses are only allocated as you use them. Just because your program is loaded at 0x80482c0 doesn't mean that everything between there and 0x0 has already been allocated.
Tyler McHenry
2009-08-24 18:41:41
If you're really interested in learning about how your program is organized in memory under Linux, give this a read: http://www.linuxforums.org/articles/understanding-elf-using-readelf-and-objdump_125.html
Tyler McHenry
2009-08-24 18:42:17
thanks for reply,i get ur point but plz give me some link so that i can get to know in detail what exactly is happening here.thanks
mawia
2009-08-24 18:44:39
The previous comment had a link. What you need to understand is: All non-kernel programs deal only with virtual addresses, and (if you want to get into more detail), how the ELF format organizes your virtual address space (the previous link). If you're unclear on virtual memory in general, Wikipedia has a decent treatment of it: http://en.wikipedia.org/wiki/Virtual_memory . Pay particular attention to the fact that a virtual address does not necessarily correspond to any physical address until such time as it is actually used.
Tyler McHenry
2009-08-24 18:50:38
And even if your program were loaded at address 0, that wouldn't necessarily mean main() would end up at (or particularly close to) address 0. The linker is permitted to arrange functions and other objects linked into the executable at whatever addresses it sees fit (in general).
Michael Burr
2009-08-24 20:48:12