Link to home
Start Free TrialLog in
Avatar of Falcubar
FalcubarFlag for United Kingdom of Great Britain and Northern Ireland

asked on

C++ assign memory address?

Hello,

I'm still learning c++ so bear with me.

If i know the address lets say 0x28ff38 and i want to read that address how can i?

Obviously i can do this...

int *pInt;
int number = 0;

pInt = &number; // Now contains 0x28ff38.
cout << *pInt <<endl; // outputs 0
system("pause");

but what i want is to do something like...

int number = 0;
int *pInt = 0x28ff38; // address of number

cout << pInt << endl;

I hope you understand what i mean?
Avatar of shajithchandran
shajithchandran
Flag of India image

you want to read the content of the memory 0x28ff38?

if yes.. then  normal star operator.

int *pInt = 0x28ff38; // address of number

cout << *pInt << endl;        <==== * operator on pint

If you want to read more data from that pointer location, then use memcpy
char buff[100];
memcpy(buff,pint,sizeof(buff));
Buff will contain 100 bytes of data located in the memory starting from the address where pint is pointing to
Avatar of kaufmed
If I recall correctly, the following lines

    int *pInt = 0x28ff38; // address of number
    cout << pInt << endl;

as you have it will print the address of pInt, not the value it holds. Because you declared pInt as a pointer, you still need to dereference it to use  the value it points to. This is what you did in the top example
If this is really what you want to do, the only difference from the above is that you use the same cout line as the first example you posted, ie.

cout << *pInt << endl;

The more interesting question is are you sure that you want to be reading arbitrary memory locations? You don't mention what platform you are coding for, and I know that in some cases (ie. embedded development) that this may be a fairly normal thing to do, but if this is windows/linux/other advanced OS then doing this can be fairly dangerous and will often lead to runtime problems, etc.
Avatar of Falcubar

ASKER

Thats what i had tried, but im getting this...

    int number = 1234;
    int *pInt = 0x28ff3c; // address of number

    cout << *pInt << endl;

Error:

 C:\C++\Learning\main.cpp In function `int main()':
13 C:\C++\Learning\main.cpp invalid conversion from `int' to `int*'
 C:\C++\Learning\Makefile.win [Build Error]  [main.o] Error 1

it doesnt like int *pInt = 0x28ff3c; // address of number

ASKER CERTIFIED SOLUTION
Avatar of Infinity08
Infinity08
Flag of Belgium image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Maybe there is anther way then?

If i know the address of something, how would i read it? Thats all im trying to do.

I just want to say Read This address: 0x28ff3c or what ever address i choose.
I dont want to do things that are bad practice and if there is no way of doing it then np. Im still learning.

Thanks
>> I just want to say Read This address: 0x28ff3c or what ever address i choose.

Why that specific address ? How do you know which address to use ?
to be honest its just a learning question, i know addresses are not always going to be the same, im simply wondering if its possible. One of my friends, or a guy i know does some memory reading in things like WoW, the address values used by WoW seem to stay the same for things like HP, MANA, Playername, playerbase etc...

So im just wondering if it works like that or am i confusing myself?
>> im simply wondering if its possible

In that case, it is, as has been shown above.


>> One of my friends, or a guy i know does some memory reading in things like WoW, the address values used by WoW seem to stay the same for things like HP, MANA, Playername, playerbase etc...

Right. You're talking about cheating with a game then ?
No, that was the only way i could think of explaining what i was trying to get at :)

Thanks all for help.