Change address of a pointer

R

Ramon

Hi,

How can I change the address of a pointer? Please look at the code
below. What I'm trying to do is to make c = &cell (without using this
notation of course). How can I assign the address of /cell/ (which is
stored in the variable /addr/) to the pointer named /c/??

// creating an object
Cell *cell = new Cell();

// getting the address of the mem location of the object
unsigned int addr = (unsigned int) &cell;

// changing the address of the new pointer to point to the
// 'cell' object
Cell *c = ??? <-- make c point to the address addr

Thanks.
 
M

Maxim Yegorushkin

How can I change the address of a pointer? Please look at the code
below. What I'm trying to do is to make c = &cell (without using this
notation of course). How can I assign the address of /cell/ (which is
stored in the variable /addr/) to the pointer named /c/??

// creating an object
Cell *cell = new Cell();

// getting the address of the mem location of the object
unsigned int addr = (unsigned int) &cell;

Note that the above line assumes that Cell** pointer can be stored in an
unsigned int. This may work on popular 32-bit architectures, but not on
64-bit ones.

There is an integer type defined in <stdint.h> for this purpose that can
be safely used for storing pointers:

uintptr_t int_addr = reinterpret_cast said:
// changing the address of the new pointer to point to the
// 'cell' object
Cell *c = ??? <-- make c point to the address addr

Convert the integer back to Cell** address:

Cell* same_sell = *reinterpret_cast<Cell**>(int_addr); // load
*reinterpret_cast<Cell**>(int_addr) = new Cell; // store

Please note that such code is rather fragile because the compiler won't
check the types for you.

Any reason why you are converting pointers to integers and back?
 
D

Donkey Hottie

25.10.2009 17:28, Ramon kirjoitti:
Hi,

How can I change the address of a pointer? Please look at the code
below. What I'm trying to do is to make c = &cell (without using this
notation of course). How can I assign the address of /cell/ (which is
stored in the variable /addr/) to the pointer named /c/??

// creating an object
Cell *cell = new Cell();

// getting the address of the mem location of the object
unsigned int addr = (unsigned int) &cell;

// changing the address of the new pointer to point to the
// 'cell' object
Cell *c = ??? <-- make c point to the address addr

The address in 'addr' is of type (Cell **), not Cell *.

Cell **c = (Cell**) addr ;

Cell* myCell = *c;
 
J

James Kanze

How can I change the address of a pointer?

Pointers are objects. You can't change the address of an
object. Did you mean that you want to change the address
contained in the pointer, or something else.
Please look at the code below. What I'm trying to do is to
make c = &cell (without using this notation of course).

The only way to get the address of cell is &cell, period. If
you want the address of cell, there has to be a &cell somewhere.

And in your code, c is declared to point to a Cell, not to a
pointer to a cell, so all of the ways of getting the address of
cell into c are going to be very implementation dependent, only
for use by experts in very low level code.
How can I assign the address of /cell/ (which is stored in the
variable /addr/) to the pointer named /c/??
// creating an object
Cell *cell = new Cell();
// getting the address of the mem location of the object
unsigned int addr = (unsigned int) &cell;

Maybe. There's absolutely no guarantee that an unsigned int can
hold an address. And normally, you don't want to do this
anyway; it's only usable for very low level, machine specific
code. (You might need to do it at times if you were writing an
OS kernel, or perhaps an implementation of malloc or a garbage
collector, but those are about the only cases I can think of
where it would be appropriate. And in such cases, you'd want to
use reinterpret_cast, rather than the C style cast, to call
attention to the fact that the code isn't portable, and is doing
some low level trickery.)
// changing the address of the new pointer to point to the
// 'cell' object
Cell *c = ??? <-- make c point to the address addr

The most obvious way to set c to the address of cell is:
Cell* c = reinterpret_cast< Cell* >( &cell );
But this is only for experts. And if you are actually writing
very low level code, and have the address in an unsigned int,
and know exactly what you're doing, just put addr in the place
of &cell, above. But quite frankly, if you had the level of
expertise to authorize such things, you wouldn't be asking the
question here.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,236
Members
46,822
Latest member
israfaceZa

Latest Threads

Top