• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 192
  • Last Modified:

pointers, addresses and values

Q: How can I convert an address pointed to by a pointer to an integer that I could assign to a variable, and conversely, how do I use a variable's value as the address a pointer should point to. Furthermore, is it possible to assign my own value to any address I choose? Finally...but I think I will know how to do the following if I know how to do the previous...how do I convert the address into a string (and vice versa)?

e.g. "...place the integer 15 at 0x0000ff00 and call it 'joe'..."
1 Solution
Hi mokopa!

1. For example, if you have a pointer, defined like this:

int* piNum;

then you can assign the value that stored on the location where this piNum pointer is pointing to, to an int variable this way:

int   iSomeThing = *piNum;

Of course, this isn't a life insurance if you didn't assign some safe value to piNum - values on a random memory place can be wierd sometimes.

2. Now, if you want to take that address and handle it as a number (for example to assign it to ant int variable), you can do it the most simple way:

int   iAddress = piNum;

because the address is the _value_ of piNum variable.

3. If you want to use an integer number as an adress and take the value that is stored there, you can do it this way:

piNum = iSomeThing;
int   iOneMore = *piNum;

Or if you don't want to use an explicit variable, then you can write this:

int   iOneMore = *(int*)iSomeThing;

4. Of course, you can assign any number to any memory place, using the above methods. Example:

int   *piMemoryPlace = 0x00150000;
*piMemoryPlace = 14;

This writes the int value 14 to memoery address 0x00150000.

You should be very careful doing this, because on most operating systems your program will generate errors if you want to modify data on memory addresses your program doesn't "have" or "own". On Windows this will cause the feared General Protection Fault.

I don't understand your last sentence:

>"...place the integer 15 at 0x0000ff00 and call it 'joe'..."

How can you "call it" on any name? Or do you mean you would put its address in a variable named "joe"?

I hope this will help you.

1) you can set value of an int to the address of a pointer like so:

int* testPtr = new int;
int testInt = int(testPtr);

of course, if you try to cout testInt, you won't get the hex value.

2) i dont' think you can choose a memory address to assign stuff to. c++ deals with memory management automatically
For your last question, this will create a variable named joe at address 0x0000ff00 and place the value 15 there...

int *joe = (int *)0x0000ff00;
*joe = 15;

Of course, shortly thereafter you will die a painful death because you've overwritten something important in memory (I am only partly kidding here...)
>> (I am only partly kidding here...)

Yeah, it might not be important. :-)

mokopaAuthor Commented:
Thanks for all the help from everyone. I managed to do most of what I originally intended to do, which was to create a small graphical "memory mapper" app which displays a square map of memory (from user supplied address to n bytes' offset), with each byte's value represented by a grey-shaded (256 shades) pixel. The map is updated by a timer. I can't get it to update realtime yet because, well, I don't know enough about vc++ yet.

I keep on running into problems looking at memory space that does not belong to my app, but I'm sure there's a way around that. I'll ask more questions later.

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now