Click here to Skip to main content
15,881,715 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi. I'm trying to use a pointer by accessing the memory address which I only know the hexadecimal value. Is it possible to do that?

For example, I know the address of the pointer is 0x7ffee96f6d3c and I want to manually access it outside from an another .cpp. Then, I can use the pointer and know that the value of a is 123.

Thanks.

What I have tried:

#include <iostream>

using namespace std;

int main1(){

    int a;
    a = 123;
    int* ptr_a;
    ptr_a = &a;

    cout<<"Address of a : "<<ptr_a<<endl; //Address=0x7ffee96f6d3c

    return (0);
}


int main2(){

    int* ptr_a;
    ptr_a = 0x7ffee96f6d3c; //assuming can get the address

    cout<<"Value of a   : "<<*ptr_a<<endl; //able to know *ptr_a=123

    return (0);
}
Posted
Updated 22-Aug-22 8:41am
v2
Comments
Richard MacCutchan 22-Aug-22 9:12am    
What happens when you try it?
Josh_0101 22-Aug-22 9:19am    
Hi. For main1() it can be run but for main2() it's just to illustrate the concept and the expected outcome.
Richard MacCutchan 22-Aug-22 9:29am    
Yes I understand that. So go ahead and test it and see what results you get.
Josh_0101 22-Aug-22 10:14am    
I got error: invalid conversion from ‘long int’ to ‘int*’ :(
Richard MacCutchan 22-Aug-22 10:23am    
Try this:
ptr_a = (int*)0x7ffee96f6d3c; 

You can do this within the same program (process), but most platforms prevent one process from reading the data of another process. To do that, you'd have to use a system call to make the memory accessible to another process.
 
Share this answer
 
Comments
Josh_0101 22-Aug-22 10:08am    
Hi, the main1() compiled in a .dll while the main2() compiled in another .dll and in the end they are running in the same application. It's considered as within the same program(process), right? If yes, how can I achieve this part? ptr_a = 0x7ffee96f6d3c; Thanks.
Greg Utas 22-Aug-22 12:11pm    
I only use static libraries, not DLLs, so I'm not really the one to answer this question. I believe that the DLL will be mapped into your process's address space, but where? You wouldn't be able to hard-code the address, but you might be able to get it from an interface that the DLL provides. Here is a link that I found:

https://docs.microsoft.com/en-us/windows/win32/dlls/using-shared-memory-in-a-dynamic-link-library
Dave Kreskowiak 22-Aug-22 14:03pm    
Why would you do this? There is no legit reason some code in one .DLL would need to look at memory used by code in another .DLL. To do so would be breaking encapsulation and separation of concerns. Each .DLL project should be concerns only with it's own data, not the data of another .DLL.
Quote:
Can a pointer access based on a known memory address?
Yes, provided they are in the same process.
If you need to access it from another process then you are out of luck.
 
Share this answer
 
Just to add to what Greg and Carlos have said ...

A hexadecimal address in a pointer is not a "real" memory address - the OS maps the real memory allocated to the process to a virtual address which is used by the pointers within the app.
So although two separate processes (think of that as "two apps running at the same time" but that's lies-to-children: an oversimplification) can use the same hexadecimal value in a pointer, they do not point to the same "real memory" - they point to virtual memory which is translated to different blocks of real memory by the OS.
If this wasn't the case, then pretty much all apps would immediately and continuously corrupt each other's memory and would crash in short order!

If you want to share memory between processes, then you have to specifically request that at run time and arrange for the two processes to talk to each other. That gets complicated, and there are normally better ways to share information: Sockets for example are a lot more flexible and robust solution in most cases.
 
Share this answer
 
How is it that you "know" that address? Normally, addresses of data items will change every time a program runs. If it is a physical address then you need to have acquired it through a driver and even it can change every time your program runs.

Note - this is with an OS that utilizes virtual addresses like Linux, Windows, and the Mac all do.

If this is a matter of acquiring a pointer in your process and making the DLLs know about it then you can pass an address to them through a function call. You can have a unique function for each DLL if you want but they will both need to be called.
 
Share this answer
 
Comments
Josh_0101 22-Aug-22 21:22pm    
Hi. The address will be write into a .txt file by main1() and read by main2(). The part that I can't make it is using the string get from .txt file, convert the string to a class pointer.

May I know how to pass the address through a function call? Is it just a unique function that we purposely to pass the address only?
Rick York 22-Aug-22 21:37pm    
Yes, that's all it is.
Josh_0101 15-Sep-22 3:43am    
What if the condition not allow to create an unique function? Means that back to the question itself. Is it possible to deal with the known address in string and convert to a class pointer?
Quote:
I got error: invalid conversion from ‘long int’ to ‘int*’ :(

Modern OS are using 64 bits pointers, but integers are commonly 32 bits.
So conversion of a 64 bits pointer to a 32 bits integer lead to a loss of data.
You may need to use a 64 bits integer like "unsigned long long" or another pointer.
 
Share this answer
 
Comments
Greg Utas 22-Aug-22 16:57pm    
The types uintptr_t, intptr_t, and size_t should be the same size as a pointer on the underlying platform.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900