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?

Community
  • 1
  • 1
Muqito
  • 1,369
  • 3
  • 13
  • 27
  • 3
    `*this` dereferences `this`. `this*` needs something after it in order to call `operator*`, but it's not complete by itself. – chris Dec 30 '12 at 04:09
  • 3
    Luckily modern operative systems prevent you from reading/writing memory you don´t own... – K-ballo Dec 30 '12 at 04:10
  • 1
    @K-ballo, Until you call something like `WriteProcessMemory` (in Windows) ;) – chris Dec 30 '12 at 04: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 http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list. – jogojapan Dec 30 '12 at 04: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). – Aniket Inge Dec 30 '12 at 04:16
  • @jogojapan depends on the compiler. I have fooled around with a broken TurboC++ compiler where I did this `*(0x500043) = 44;`, of course that's not standard. – Aniket Inge Dec 30 '12 at 04:17
  • @Aniket You can cast an integer to pointer. But you cannot apply operator `*` to an integer. – jogojapan Dec 30 '12 at 04:26
  • @jogojapan unless you cast an integer to integer pointer. – Aniket Inge Dec 30 '12 at 04:26

1 Answers1

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.

Community
  • 1
  • 1
ruakh
  • 175,680
  • 26
  • 273
  • 307
  • _The standard does not necessarily guarantee that ability_? The unary operator `*` only applies to pointer types. That is what the Standard says. – jogojapan Dec 30 '12 at 04:23
  • 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. – Muqito Dec 30 '12 at 04:24
  • @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? – ruakh Dec 30 '12 at 04:28
  • 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. – Muqito Dec 30 '12 at 04:36
  • (Well, strike my previous comment. The question's having been closed *does* completely prevent other people from offering answers. :-P ) – ruakh Dec 30 '12 at 18:10