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?
FalcubarAsked:
Who is Participating?
 
Infinity08Commented:
You cannot just assign an integer value (0x28ff3c is an integer value) to a pointer. This might make sense on some platforms, but there's no guarantee of that. Furthermore, it isn't very safe, so make sure that this is really what you want/need to do.

If you really do need to do this, you'll need to add an explicit cast, to re-assure the compiler that this is really what you want to do :
int* pInt = reinterpret_cast<int*>(0x28ff3c);

Open in new window

0
 
shajithchandranCommented:
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
0
 
käµfm³d 👽Commented:
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
0
Cloud Class® Course: Microsoft Exchange Server

The MCTS: Microsoft Exchange Server 2010 certification validates your skills in supporting the maintenance and administration of the Exchange servers in an enterprise environment. Learn everything you need to know with this course.

 
mccarlIT Business Systems Analyst / Software DeveloperCommented:
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.
0
 
FalcubarAuthor Commented:
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

0
 
FalcubarAuthor Commented:
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
0
 
Infinity08Commented:
>> 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 ?
0
 
FalcubarAuthor Commented:
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?
0
 
Infinity08Commented:
>> 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 ?
0
 
FalcubarAuthor Commented:
No, that was the only way i could think of explaining what i was trying to get at :)

Thanks all for help.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.