When I use gdb to debug a program written in C, the command disassemble shows the codes and their addresses in the code memory segmentation. Is it possible to know those memory addresses at runtime? I am using Ubuntu OS. Thank you.
This is a discussion on Find program code's address at runtime within the C Programming forums, part of the General Programming Boards category; When I use gdb to debug a program written in C, the command disassemble shows the codes and their addresses ...
When I use gdb to debug a program written in C, the command disassemble shows the codes and their addresses in the code memory segmentation. Is it possible to know those memory addresses at runtime? I am using Ubuntu OS. Thank you.
Not portably. You can take the address of a function, but it's not guaranteed that the address you get is actually the address of the code which implements the function (although in practice it usually is).
The question is, WHAT code? Are you interested in the address of a particular function, or the base address of the entire code segment? On most operating systems/binary formats, the code segment load address is fixed at link time and cannot change. So there is no need to determine it at runtime.
Code://try //{ if (a) do { f( b); } while(1); else do { f(!b); } while(1); //}