actuall number contained within the pointer

in tutorial of pointers i found out that a pointer actually contains the "id of memory cell"

how can i get that number? and store it in double variable?

for example i have:
1
2
double* pointer=new double;
*pointer=5;


now that pointer points to some place where double 5 is stored,
next i am tring to do is get that number where 5 is out of my pointer and store it into another double variable, i tried like this:

double address = reinterpret_cast<double>(pointer);

but i get the error when compiling it, but it has to be posible to achive this because when i write

cout<<pointer;

i get that number i am searching for, but i want to have it in double variable,


can somebody please help me achive this?
i would be really gratefull


Last edited on
closed account (zb0S216C)
mcf3lmnfs wrote:
double address = reinterpret_cast<double>(poniter);
Do not do this. If you want to obtain the address of poniter, use the address-of operator on poniter. If you want the address of the double pointed-to by poniter, do not dereference it, and do not use the address-of operator. For instance:

1
2
3
4
5
// Get the address of the pointer:
&some_pointer_;

// Get the address of the pointed-to object:
double *another_pointer_(some_pointer_);

Wazzak
Last edited on
Storing it in a double seems a bit silly, since it is an integer, but sure. This involves some horrific casting, but if you've already decided to represent a memory address as a double, that bridge has been burnt :)

1
2
3
4
5
6
7
8
9
10
11
12
13
#include <iostream>

using namespace std;

int main()
{

double* poniter=new double;
int number = (int)poniter;
double numberAsDouble = number;

cout << number << " " << numberAsDouble;
}
Framework, I'm interested as to why you should not do this? Aren't pointers in C++ just integers, the addresses? I'm not so hot on classical memory pointers...
thanks a lot guys, particularly to Moschops, that is exactly what i needed.

but am still curious why this works:
int number = (int)poniter;

and why not this:
double number = (double)pointer;


and i have just one more question,
i needed this pointer as int, beacause i am trying to export it to another program, (i am building a dll), and i am not shore how long will that memory stay there? the one that is pointed by this pointer? all untill a dll is freed from program using it?
or is this completly wrong way to do something like that?

thanks a lot for your time =)
Maybe it's because by your logic, THIS should work:

 
double number = (int) pointer;


The other casting was implied, but how should the compiler know to make it an int first?

EDIT: And yes, that works. You were right, but still expecting too much out of the compiler. A double is a complex number type, an address is a type of int. Connect the dots for the compiler.
Last edited on
are all pointers 4 bytes long? aren't some of them 8? on 64bit system? or something like this? that was my reason to store the pointer as double.

sorry if i was aking something so obvious.
closed account (zb0S216C)
Veltas wrote:
"Aren't pointers in C++ just integers, the addresses?"

I guess you could say that since addresses are represented in hexadecimal. However, a double shouldn't represent an address, and trying to do so is absurd - there's no 1.2 address in memory. Also, since floating-point types struggle with rounding, you'd have a hard time getting the correct address.

And why he/she's trying to store an address in a double in the first place is doesn't make any sense. Addresses are whole numbers, so store them in integral types.

Wazzak
Last edited on
still aren't some of them 8bytes long? on 64bit system? that fits to double not to int
closed account (zb0S216C)
Use a long long; that's 8-bytes.

Wazzak
thank you a lot Framework =), that is perfect
Want to know the only good way of storing an address? In a pointer! Or if that's too specific for you just stick to a void *. EDIT: I wonder if cout will output your address after casting to a void *?

I have no idea why anyone would want to store the address any other way, it's still an address; all you can accomplish is making your code platform-specific through bad data sizes.

Thanks for answering my question Framework.
Last edited on
i wasn't asking anything like that!!, noone needed to know why store it like this, we figure it out without that ok? where is the problem? if you really need to know so bad it is becaouse the progam in wich i want to export that pointer, does not accpts nothing else than strings and numbers in int double long char, ok? satisfied? in diffrent circumstances you need difrent measures ok? i never asked what is the best way to store pointer!! so answer on your first question if you haven't figure it out yet, NO I DONT WANNA KNOW.
Last edited on
Well, honestly I said it in a kind of firing a gun at a mountain kind of way; I was a little put-off by the discussion in general rather than you.

Also it's good practise to store addresses inside pointers.
Topic archived. No new replies allowed.