Reputation: 10284
Is this legal?
int& foo = 0x00000000;
I don't know if 'int' is the right data type. If you wanted to store an address what data type would you use? Or do you have to use a pointer?
in addition to this, is this legal?
int foo = 5;
int bar = &foo;
Essentially what I'm asking is, is there a way of storing addresses without the use of pointers?
Upvotes: 1
Views: 98
Reputation: 153909
The first snippet,
int& foo = 0x00000000;
is illegal; you can't initialize a reference with a temporary unless it is a reference to const. The following would be legal:
int const& foo = 0x00000000;
, but I can't think of a reason anyone might want to do it. It is effectively the same thing as:
extern int const foo = 0x00000000;
The second snippet,
int foo = 5;
int bar = &foo;
is also illegal. The type of &foo
is int*
, and there is no implicit
conversion of int*
to int
. You can use an explicit conversion
(reinterpret_cast
), but only if int
is large enough to hold all
possible pointer values without loss. In such cases, it's better to use
intptr_t
(from <stdint.h>
), which is a typedef to an integral type
large enough to hold all pointer values. (Formally: <stdint.h>
was
introduced in C90/C++11, and intptr_t
isn't guaranteed to be present
if there isn't a large enough integral type. C90/C++11 require long
long
, however, and require it to be 64 bits, so this will only be a
problem if you have a machine with an address space of over
264. Which is not something I'd worry about.)
Upvotes: 0
Reputation: 81349
Is this legal? int& foo = 0x00000000;
No.
is this legal? int foo = 5; int bar = &foo;
No. In order to convert a pointer to an integer a cast is needed: reinterpret_cast< int >( &foo )
. You probably want int* bar = &foo;
instead.
is there a way of storing addresses without the use of pointers?
No, pointers are the elements used to store addresses. You could cast them to an integer long enough, and then you would be able to 'store' addresses disguised as numbers.
References are not addresses, in fact references aren't anything. They are just a new name for an existing object.
Upvotes: 1
Reputation: 4557
The second snippet is perfectly fine might compile, but probably doesn't do what you want. The first is nonsensical.
If you want to declare a pointer to an int, it goes int *foo
.
You can then do:
int foo = 5;
int *bar = &foo;
With them declared thusly, this is what you can and cannot do:
*bar = 6; // foo is now 6.
bar = 6; // bar now points to the blackness of space
*bar = 6; // Seg fault!
Upvotes: 0
Reputation: 308130
int& foo = 0x00000000;
is trying to make a reference to a literal, which won't work because literals are constant. This works though:
const int& foo = 0x00000000;
This creates a reference to a constant integer 0. Whenever you try to use foo
in your code, it will be 0.
References are often implemented as pointers by the compiler, but this is an implementation detail that should be invisible to you.
Pointers were invented for storing addresses, the only reason not to use it is to use a smart pointer class instead.
Upvotes: 0
Reputation: 47762
If you wanted to store an address what data type would you use? Or do you have to use a pointer?
For storing addresses, you normally use pointers. If for who-knows-what reasons you to store pointers in an integral type, you can, provided the integral type is big enough to hold the pointer (64bit integer for 64bit pointer etc.).
C++03 AFAIK still doesn't have (or guarantee) integral types large enough to hold pointers, but C99 introduced intptr_t
typedef which servers just for this purpose. C++11 has it, too (std::intptr_t
in header <cstdint>
).
You use it like this:
int x;
intptr_t i=(intptr_t)&x;
or better
intptr_t i=reinterpret_cast<intptr_t>(&x);
note that the value in i
is implementation defined.
Upvotes: 2