Monday, May 24, 2010

How to get the adderss in c++?

i have declared a pointer and have the hexadecimal address and stored in a file. Now i close the program. As the memory is not deallocated the vale in RAM still resides. now can i access that memory using memory address in file from any other program. i will be picking the value of address in character as i have saved it in a txt file. now my problem is how to assign a pointer to that memory address(i hav it in string format)..

How to get the adderss in c++?
No, when the program terminated that memory was returned to the operating system. Just because you didn't free the memory, it will still be freed when the program terminates. C/C++ programs only leak memory while they're running (on modern OS's anyway), when they terminate the OS can and does clean up.





What you've got is probably a relative address anyway--relative to the program that was running, not an absolute address in memory. Regardless, even if you had an exact address you couldn't access it from another program unless that program just happened to be assigned that location by the operating system. It would result in what's called a protection fault, I think.





Think about it. If any program could just grab any piece of memory, the system would crash all the time because there would be no way to protect one program from another, or even to protect the operating system. Older version of Windows/DOS suffered from that very problem.
Reply:A pointer is pretty much, just a long int. If you have it as a string, you can parse it with something like


void *ptr = (void*) strtol (str, 0, 16);


Or if you are really writing c++ (not just compiling with a c++ compiler), read the long directly from the stream:


long n = 0;


stream %26gt;%26gt; hex %26gt;%26gt; n;


void *ptr = static_cast%26lt;void*%26gt; n;





The problem however is that as far as I understand your question, this pointer was created and saved to the file by another program, not the one that is trying to read it. That will not work. You cannot access other process memory from your program. Moreover, the values of pointers only have any meaning in the context of the program that created it - the old good times when computers had 64K of memory are all gone.





If you must do what you describe (and you, porbably, don't - there must be a simpler, more elegant way to do what you need), you'll have to use shared memory. In normal, POSIX-compliant systems - like OS/X, Unix or Linux, you can use shmget() to create a shared memory segment, than shmat() to attach to it (it returns a pointer, that you can use as a normal regular pointer), shmdt() to detach, and shmctl() to destroy the segment, when you don't need it anymore. Then the porgram that creates the pointer would do shmget() and shmat(), put whatever data it needs into that address, and then store the segment id - what's returned by shmget(), NOT the pointer into the file. The other program, can read the segment id from the file (it's an integer), do shmat() to obtain the pointer, and use it ... Shared memory even survives the process - the first program can exit after creating its segment, and the second one (or anyone else) can still use it until somebody uses shmctl() to destroy it.





If you are on windoze, they may not have exactly these functions... Bill Gates has been known to violate a few standards in the past :-)


But there should be something equivalent (though, probably, more painful) you can do... try searching your IDE help for shared memory. Or install cygwin or mingw - they both provide standard-compliant libraries.
Reply:Even though you do not explicitly deallocate the memory in your program, the operating system will still reclaim the memory when your program closes. All modern operating systems behave this way to prevent memory leaks when the program closes.





The memory address your program saved to a text file will is a virtual memory address. Programs typically only deal in virtual memory, with the operating system handling mapping the virtual memory your program sees with the real memory in your computer.





Technically, the value *may* still be out there in main memory after your program closes because the OS only frees the memory; it doesn't wipe it clean. However, locating the value is much more complicated than saving the virtual address to a text file.





For the general question of how to assign a pointer to a memory address:





unsigned char* ptr = (unsigned char*) 0xabcdabcd;





However, be advised that dereferencing this pointer will result in undefined behavior and for the reasons stated earlier, this will not work for recovering what the other program stored in memory.


No comments:

Post a Comment