0

Possible Duplicate:
pointer to a specific fixed address

int a, hex = 0x573285;
int * pA; // Pointer?
*pa = &a; // &a = the hexadecimal-memory location and *pa is now going to point to that place.

So I figured:

let's say it possible to write something like this for example:

(*hex) = 5; or whatever the hexadecimal number is

to change the value of the memory?

and if I had a program (I don't know one), that showed me all the memory locations my game use. if i changed the value in the program and then got back to my game or what ever, would it be changed there aswell? (if the game's running).

and is (*this) the same as (this*) ?

EDIT: This works

int a = 5, b=0x28ff1c;
int* c = &a;
cout << *(c);

But not:

int a = 5;
int * b=0x28ff1c;
int * c = &a;
cout << *(b);

nor:

int a = 5, b=0x28ff1c;
int * c = &a;
cout << *(b);

Do you see what I'm trying to do?

8
  • 3
    *this dereferences this. this* needs something after it in order to call operator*, but it's not complete by itself. Commented Dec 30, 2012 at 4:09
  • 3
    Luckily modern operative systems prevent you from reading/writing memory you don´t own... Commented Dec 30, 2012 at 4:10
  • 1
    @K-ballo, Until you call something like WriteProcessMemory (in Windows) ;) Commented Dec 30, 2012 at 4:11
  • 1
    @MaggiQall I am afraid this question doesn't make much sense. hex is an int, so there is no such thing as *hex. For C++ textbooks see stackoverflow.com/questions/388242/…. Commented Dec 30, 2012 at 4:15
  • unless of course, this wasn't on a 32/64 bit modern architecture, with memory protection. I remember, Nintendo allowed reading from a memory location and writing to it(with such integer2pointer conversions). Commented Dec 30, 2012 at 4:16

1 Answer 1

2

let's say it possible to write something like this for example:

(*hex) = 5; or whatever the hexadecimal number is

to change the value of the memory?

The standard does not necessarily guarantee that ability, but on many systems you can write:

*((int *) hex) = 5;

to "cast" hex as a pointer-to-int, then dereference that pointer and set the int to 5 — provided, of course, that the value in hex really refers to some memory location that you can write to. But needless to say, you shouldn't do this in any program that you actually intend to use for anything.

if I had a program (I don't know one), that showed me all the memory locations my game use. if i changed the value in the program and then got back to my game or what ever, would it be changed there aswell? (if the game's running).

This is often the case on embedded systems, but is not generally the case on modern machines with normal operating systems, due to the use of virtual memory and virtual address spaces. What happens is, a process usually won't know the real physical memory location of the memory it's using (if there even is one); instead, the values stored in pointers are "virtual", and process-dependent. Two processes' 0x12345678 can have absolutely nothing to do with each other, being mapped to completely separate memory locations.

Sign up to request clarification or add additional context in comments.

5 Comments

The standard does not necessarily guarantee that ability? The unary operator * only applies to pointer types. That is what the Standard says.
Well *((int *) hex) worked like a charm : Thank you very much. So I wasn't completely out of my mind (I mean in my "logical thinking") ^^, I think this is really interesting and fun. Thank you again.
@jogojapan: To clarify: the standard certainly does not guarantee that syntax; but it doesn't necessarily guarantee that ability. The correct syntax is what I gave in my answer. (But even with the correct syntax, it's still not guaranteed to work, because the standard doesn't guarantee that it's safe to convert back and forth between int and pointer-to-object.) Do you see what I mean?
Alright, I really just wanted the idea if that worked just typing a hex to see what value something is holding or if you're bound to do it like this: *(&a) or something. thought maybe it just converted the thing to a hex when you tried to print it out. like a magic function or something. I'm not sure if you get what I mean though :) But I though your answer was spot on to my question though. and I upvoted chris answer about the other question so. I can't see why I wouldn't accept your answer.
(Well, strike my previous comment. The question's having been closed does completely prevent other people from offering answers. :-P )

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.