Getting absolute address of variable in memory
-
Hello, I´ve tried running at the same time two instances of the program below. I pass the running time in seconds as a command-line parameter. I pass different values to the two instances, 7 for one and 9 for the other. int iTest=0; int _tmain(int argc, _TCHAR* argv[]) { printf("iTest: %d \n", iTest); // prints 0 printf("&iTest: %p \n", &iTest); // This prints "00417178" for both instances !! iTest = atoi((char *) argv[1]); printf("iTest: %d \n", iTest); Sleep(iTest * 1000); printf("iTest: %d \n", iTest); // Though the pointer &iTest is the same, each instance // prints its own value !! Sleep(5000); return 0; } Two questions: 1) Why the pointer is the same but the values are different ? I imagine that this value refers to a relative location, is this the point ? 2) In this case, I need to know: is it possible to get the absolute address of the variable in RAM ? My need is to assure we are not having problems using the same spaces in memory in some variables we are creating. Thanks, Emerson - from S.Paulo, Brazil
-
Hello, I´ve tried running at the same time two instances of the program below. I pass the running time in seconds as a command-line parameter. I pass different values to the two instances, 7 for one and 9 for the other. int iTest=0; int _tmain(int argc, _TCHAR* argv[]) { printf("iTest: %d \n", iTest); // prints 0 printf("&iTest: %p \n", &iTest); // This prints "00417178" for both instances !! iTest = atoi((char *) argv[1]); printf("iTest: %d \n", iTest); Sleep(iTest * 1000); printf("iTest: %d \n", iTest); // Though the pointer &iTest is the same, each instance // prints its own value !! Sleep(5000); return 0; } Two questions: 1) Why the pointer is the same but the values are different ? I imagine that this value refers to a relative location, is this the point ? 2) In this case, I need to know: is it possible to get the absolute address of the variable in RAM ? My need is to assure we are not having problems using the same spaces in memory in some variables we are creating. Thanks, Emerson - from S.Paulo, Brazil
It would seem that like most modern operating systems, the one you are using makes use of paging; therefore all addresses are fake. Therefore it can load them to diffrent real addresses, and tell the program it is at the address it was meant to run at (all programs are compiled to run at a specific base address).
-
Hello, I´ve tried running at the same time two instances of the program below. I pass the running time in seconds as a command-line parameter. I pass different values to the two instances, 7 for one and 9 for the other. int iTest=0; int _tmain(int argc, _TCHAR* argv[]) { printf("iTest: %d \n", iTest); // prints 0 printf("&iTest: %p \n", &iTest); // This prints "00417178" for both instances !! iTest = atoi((char *) argv[1]); printf("iTest: %d \n", iTest); Sleep(iTest * 1000); printf("iTest: %d \n", iTest); // Though the pointer &iTest is the same, each instance // prints its own value !! Sleep(5000); return 0; } Two questions: 1) Why the pointer is the same but the values are different ? I imagine that this value refers to a relative location, is this the point ? 2) In this case, I need to know: is it possible to get the absolute address of the variable in RAM ? My need is to assure we are not having problems using the same spaces in memory in some variables we are creating. Thanks, Emerson - from S.Paulo, Brazil
First some problems with your code.
chapultec wrote:
iTest = atoi((char *) argv[1]);
This is wrong. The type of the arguments is
_TCHAR* argv[]
so to assume the strings contained in the array arechar*
s is wrong. It should be something like this:iTest = _ttoi(argv[1])
In general casting something into something it isn't (when the program is compiled as Unicode) doesn't transform it, it just tells the compiler to plough on and assume it is.
chapultec wrote:
// Though the pointer &iTest is the same, each instance // prints its own value !!
Each process has its own memory space, even two instances of the same process. To get the behaviour you seem to be expecting you'd have to use shared memory. Address spaces are visualised by the OS (with hardware support) and an address in a process is not the physical address of the memory. The only place you'd use physical addresses is in device drivers, and even then only in certain circumstances (like telling some piece of hardware to perform a DMA operation).
Steve
-
It would seem that like most modern operating systems, the one you are using makes use of paging; therefore all addresses are fake. Therefore it can load them to diffrent real addresses, and tell the program it is at the address it was meant to run at (all programs are compiled to run at a specific base address).
Gwenio, You answered my question 1, thank you. But I still need to know - is there some routine from WinAPI that I can use to get the real / physical / absolute address ? The fact is I´m using a module from another person and I suspect he´s sharing some variable among different instances. I must be sure if it´s happening or not.
-
Hello, I´ve tried running at the same time two instances of the program below. I pass the running time in seconds as a command-line parameter. I pass different values to the two instances, 7 for one and 9 for the other. int iTest=0; int _tmain(int argc, _TCHAR* argv[]) { printf("iTest: %d \n", iTest); // prints 0 printf("&iTest: %p \n", &iTest); // This prints "00417178" for both instances !! iTest = atoi((char *) argv[1]); printf("iTest: %d \n", iTest); Sleep(iTest * 1000); printf("iTest: %d \n", iTest); // Though the pointer &iTest is the same, each instance // prints its own value !! Sleep(5000); return 0; } Two questions: 1) Why the pointer is the same but the values are different ? I imagine that this value refers to a relative location, is this the point ? 2) In this case, I need to know: is it possible to get the absolute address of the variable in RAM ? My need is to assure we are not having problems using the same spaces in memory in some variables we are creating. Thanks, Emerson - from S.Paulo, Brazil
Hi, operating systems such as Windows use "Virtual Memory" which means each process is presented memory independent of all other processes; so two processes may point at 0x2000 and that would touch different locations in physical memory. The MMU (Memory Management Unit, part of a processor) translates the virtual addresses you see into physical addresses. Windows is rather reluctant to even tell you the physical memory, but you should not really care, processes have non-overlapping memory by default. If you need to pass data to another process, there are several ways, such as shared memory; or the Win32 functions OpenProcess, ReadProcessMemory, WriteProcessMemory (but even those don't reveal physical addresses). :)
Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]
Prolific encyclopedia fixture proof-reader browser patron addict?
We all depend on the beast below.
-
First some problems with your code.
chapultec wrote:
iTest = atoi((char *) argv[1]);
This is wrong. The type of the arguments is
_TCHAR* argv[]
so to assume the strings contained in the array arechar*
s is wrong. It should be something like this:iTest = _ttoi(argv[1])
In general casting something into something it isn't (when the program is compiled as Unicode) doesn't transform it, it just tells the compiler to plough on and assume it is.
chapultec wrote:
// Though the pointer &iTest is the same, each instance // prints its own value !!
Each process has its own memory space, even two instances of the same process. To get the behaviour you seem to be expecting you'd have to use shared memory. Address spaces are visualised by the OS (with hardware support) and an address in a process is not the physical address of the memory. The only place you'd use physical addresses is in device drivers, and even then only in certain circumstances (like telling some piece of hardware to perform a DMA operation).
Steve
Steve, You´re right on your comment about this casting, but this statement may not be wrong (when one knows what he´s doing ...) In my project properties - General - Character set - I changed character set to "Use Multi-Byte Character Set". The option "Not Set" would get the same result. Right ? In fact I don´t need to use shared memory, but I need to know if the guys who developed the module I´m using used some kind of shared memory or not. That´s why I think I need some kind of routine to catch the physical addresses of some variables. I don´t know if there´s some way to get it. Thanks.
-
Gwenio, You answered my question 1, thank you. But I still need to know - is there some routine from WinAPI that I can use to get the real / physical / absolute address ? The fact is I´m using a module from another person and I suspect he´s sharing some variable among different instances. I must be sure if it´s happening or not.
chapultec wrote:
But I still need to know - is there some routine from WinAPI that I can use to get the real / physical / absolute address ? The fact is I´m using a module from another person and I suspect he´s sharing some variable among different instances.
WinAPI
allow developers to share memory between applications without giving them knowledge of the physical address of the shared buffer, see [^]. :)If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
[My articles] -
Hello, I´ve tried running at the same time two instances of the program below. I pass the running time in seconds as a command-line parameter. I pass different values to the two instances, 7 for one and 9 for the other. int iTest=0; int _tmain(int argc, _TCHAR* argv[]) { printf("iTest: %d \n", iTest); // prints 0 printf("&iTest: %p \n", &iTest); // This prints "00417178" for both instances !! iTest = atoi((char *) argv[1]); printf("iTest: %d \n", iTest); Sleep(iTest * 1000); printf("iTest: %d \n", iTest); // Though the pointer &iTest is the same, each instance // prints its own value !! Sleep(5000); return 0; } Two questions: 1) Why the pointer is the same but the values are different ? I imagine that this value refers to a relative location, is this the point ? 2) In this case, I need to know: is it possible to get the absolute address of the variable in RAM ? My need is to assure we are not having problems using the same spaces in memory in some variables we are creating. Thanks, Emerson - from S.Paulo, Brazil
MmGetPhysicalAddress[^] Doco says it should run at any IRQL, so user mode should be okay