17

I want to know if there is anyway that I can store the address location of a variable as an integer value. For example, let's say that I have a number stored in some location in memory

int i= 20;

and we know that for example, the location of the variable i is 0x77C79AB2. e.g.

int * ip = &i;

so we know that ip = 0x77C79AB2.

But at this point the variable ip is just a pointer. But let's say that I now want to store the address location 0x77C79AB2 into a variable of type int (NOT of type Pointer).

so, somehow I want to be able to make another variable of type (int) to actually store the number 0x77C79AB2 as a value not as a address location.

int a = 0x77C79AB2;

So, I could do whatever I want with the variable a. For example, I want to treat a as an integer and add a hex number 0x20 to it.

e.g.

int b = a + 0x20 =  0x77C79AB2 + 0x20 = 0x77C79AD2

Is this possible? How could I make this assignment ?

Rudy01
  • 1,027
  • 5
  • 18
  • 32

4 Answers4

29

Pointers are not integers. If you want to store a pointer value, you should almost always store it in a pointer object (variable). That's what pointer types are for.

You can convert a pointer value to an integer using a cast, either a C-style cast:

int foo;
int addr = (int)&foo;

or using a C++-style cast:

int foo;
int addr = reinterpret_cast<int>(&foo);

But this is rarely a useful thing to do, and it can lose information on systems where int happens to be smaller than a pointer.

C provides two typedefs intptr_t and uintptr_t that are guaranteed to be able to hold a converted pointer value without loss of information. (If no integer types are wide enough for this, intptr_t and uintptr_t will not be defined). These are declared in the <stdint.h> header, or <cstdint> in C++:

#include <stdint.h>
// ...
int foo;
uintptr_t addr = reinterpret_cast<uintptr_t>(&foo);

You can then perform integer arithmetic on the value -- but there's no guarantee that the result of any arithmetic is meaningful.

I suspect that you're trying to do something that doesn't make much sense. There are rare cases where it makes sense to convert a pointer value to an integer and even rarer cases where it makes sense to perform arithmetic on the result. Reading your question, I don't see any real indication that you actually need to do this. What exactly are you trying to accomplish? It's likely that whatever you're trying to do, there's a cleaner way to do it.

Keith Thompson
  • 254,901
  • 44
  • 429
  • 631
  • Why would you use `intptr_t` if you have `uintptr_t`? For relative addressing or something? Must `intptr_t` be a larger data size than the unsigned version since it cannot utilize its signed bit, thus its range is smaller? –  Aug 10 '14 at 03:50
  • @snowandotherjoys: Are you assuming that pointers are unsigned? Pointers had no signedness, and a signed integer might be the most natural representation on some systems. There's no reason to assume the sign bit can't be used. – Keith Thompson Aug 10 '14 at 04:33
5

Q: Is there any way I can store the address location of a variable as an integral value.

A: Sure. All you have to do is cast.

CAVEATS:

1) Just remember that sizeof(int) != sizeof (char *) on all platforms. As mentioned above, use "size_t" whenever possible.

2) For C++, consider using reinterpret_cast<>:

FoggyDay
  • 11,962
  • 4
  • 34
  • 48
3

The header cstdint defines a type uintptr_t which is an integer type large enough to hold a pointer. Cast your pointer type to it with reinterpret_cast. e.g:

#include <cstdint>

...

int i = 20;
uintptr_t ip = reinterpret_cast<uintptr_t>(&i);
Fsmv
  • 1,106
  • 2
  • 12
  • 28
1

You can already do things like adding an offset by just going back to array syntax: ip[0x20]. You can typecast between different types of pointers to change the offset "step size".

Gheesh
  • 536
  • 3
  • 4