How to read a value from an absolute address through C code
Just assign the address to a pointer:
char *p = (char *)0xff73000;
And access the value as you wish:
char first_byte = p[0];
char second_byte = p[1];
But note that the behavior is platform dependent. I assume that this is for some kind of low level embedded programming, where platform dependency is not an issue.
C - Read value from a specific memory address
Each process has its own virtual address space, so, address 0x12345678
in one program will not be the same as address 0x12345678
in another.
You can access what is at the address simply by doing:
#include <stdio.h>
int main(int argc, char *argv[]){
char *ptr = (char *)0x12345678; //the addr you wish to access the contents of
printf("%c\n", *ptr); //this will give you the first byte, you can add any more bytes you need to the ptr itself, like so: *(ptr + nbyte).
return 0;
}
You'll likely get a segmentation fault though, unless you really know what you're doing. My guess is that you think this is the way to solve some other problem, whereas this isn't the actual solution. This does answer your question in the OP, though.
Here is Another Virtual Memory learning resource.
How to get a value from a memory adress in C?
The standard way of accessing a particular address 0x12345678 (such as a hardware register) is:
(volatile uint32_t*)0x12345678
If you want to use the contents of that address as if it was a variable, you could either have a pointer point there, or make a macro:
#define REGISTER ( *(volatile uint32_t*)0x12345678 )
C++ - Get value of a particular memory address
You can and should write it like this:
#include <cstdint>
uintptr_t p = 0x0001FBDC;
int value = *reinterpret_cast<int *>(p);
Note that unless there is some guarantee that p
points to an integer, this is undefined behaviour. A standard operating system will kill your process if you try to access an address that it didn't expect you to address. However, this may be a common pattern in free-standing programs.
(Earlier versions of C++ should say #include <stdint.h>
and intptr_t
.)
How to read data in a specific memory address in c++
To elaborate on my comment above:
As I wrote each program will run in a different process.
Each process has a separate adddress space.
Therefore using the address of a variable in another process doesn't make any sense.
In order to communicate between processes, you need some kind of IPC (inter-process communication):
Inter-process communication
If you only need to share a variable, the first mechanism that comes to mind is to use shared memory:
Shared memory
Shared-memory is very much OS dependent.
You didn't mention which OS you are using.
On Windows you can use Win API for managing shared memory:
Windows shared memory
There's an equivalent on Linux, but I am not familiar with the details.
Alternatively you can use the boost solution which is cross platform:
boost shared memory
If you'll delve into that it will be clear that these kind of solutions comes with some compilcations.
So the question is why do you need to do it ? Maybe there's a better solution to your problem
(you haven't described what it actually is, so there's not much more I can say).
C - get value stored at an address in the virtual memory of a process
if (low_hex < ret_adr < high_hex) {
This line is incorrect, it should read:
if (low_hex <= ret_adr && ret_addr < high_hex) {
Related Topics
Why Do You Need to Append an L or F After a Value Assigned to a C++ Constant
What's the Real Use of Using N[C-'0']
Linux Ipc - Multiple Writers, Single Reader
Cmake Error: "Add_Subdirectory Not Given a Binary Directory"
How to Find How Much Memory Is Shared Between Forked Process with Copy-On-Write in Linux
Lambda Capture by Value Mutable Doesn't Work with Const &
Segmentation Fault Before Main() When Using Glut, and Std::String
How to Find The Full Path of The C++ Linux Program from Within
Set System Date and Time Using C++ in Linux
Does Class/Function Order Matter in C++
How to Compile a C++ Code in Gcc (G++) to See The Name Mangling on Overloaded Functions
Why Have Unary_Function, Binary_Function Been Removed from C++11
How to Reproduce Tcp Protocol 3-Way Handshake with Raw Sockets Correctly