?
Solved

pointers, addresses and values

Posted on 2003-02-26
5
Medium Priority
?
191 Views
Last Modified: 2010-04-01
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'..."
0
Comment
Question by:mokopa
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
5 Comments
 

Accepted Solution

by:
Baquatelle earned 222 total points
ID: 8029351
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.

Baquatelle
0
 

Expert Comment

by:entropy12
ID: 8029369
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
0
 
LVL 6

Expert Comment

by:gj62
ID: 8029690
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...)
0
 
LVL 8

Expert Comment

by:Exceter
ID: 8030005
>> (I am only partly kidding here...)

Yeah, it might not be important. :-)

Exceter
0
 

Author Comment

by:mokopa
ID: 8058884
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.
0

Featured Post

[Webinar] Lessons on Recovering from Petya

Skyport is working hard to help customers recover from recent attacks, like the Petya worm. This work has brought to light some important lessons. New malware attacks like this can take down your entire environment. Learn from others mistakes on how to prevent Petya like worms.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

764 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question