Questions about pointer address

Consider the code below:

1
2
3
4
5
int nbr = 3, *ptr = &nbr;
std::cout << ptr << std::endl;

int *ptr2 = nullptr;
std::cout << ptr2 << std::endl;


My output is:
0xb3c09ffabc
0


I have two questions:
1: I do not remember, but I think before, the address was only 6 hexadecimal digits, now It is 10, I think It has something to do with OS architecture(mine is 64-bit), but I am not sure. Do you have any idea about this?

2: the ptr gives an address, but what about ptr2, is this an address too?
Last edited on
1. One hexadecimal digit represents 4 bits. That means a 32-bit number/address can need up to 8 hexadecimal digits while a 64-bit number/address can need up to 16 hexadecimal digits, but as always you can can leave out leading zeroes so fewer digits might be necessary when you write out the number.

Compare these two outputs:
1
2
3
4
int x;
std::cout << &x << std::endl;
int* p = new int;
std::cout << p << std::endl;

For me these print different amounts of digits.


2. ptr2 is a so called "null pointer". Null is a special value that you can give to pointers when they don't point to anything.
but as always you can can leave out leading zeroes so fewer digits might be necessary when you write out the number.
Do you mean here the address should be 0x0000b3c09ffabc 0x000000b3c09ffabc, but the gave me 0xb3c09ffabc instead for the reason you mentioned?
Last edited on
Arguably 'should' is a wrong word choice here.
An address is just a number, so leading zeroes don't mean anything.
And your example address is only 14 hexadecimal digits.

What matters in the end is sizeof(my_ptr).
To get more technical, the C++ standard doesn't guarantee a pointer is 64-bit if compiling as a 64-bit program, but most implementations (like VC++) will make extra guarantees such as that.
Last edited on
Using à 64bits architecture you have a 16 digits address, but you don't have the fist null sequences here...
Last edited on
Arguably 'should' is a wrong word choice here.
, can you write the correct phrase so that I avoid using it next time.

And your example address is only 14 hexadecimal digits.
I just fixed it.

An address is just a number, so leading zeroes don't mean anything.
, I am trying to understand what @peter said when he wrotes:
but as always you can can leave out leading zeroes so fewer digits might be necessary when you write out the number.
It's just like when you write a normal number in math. 0000000024 means the same thing as 00024 or 24. The leading zeroes has no meaning.

Note that I'm talking about math. If you write an integer literal starting with a zero C++ will interpret that as an octal (base 8) number.

Hexadecimal is often used to display binary data, because each digit corresponds exactly to 4 bits (2 digits = 1 byte), and it is therefore quite common that you see leading zeroes so that the number of digits corresponds to how many bits there are in the underlying data.

Exactly how the addresses are displayed seems to differ between different implementations. Microsoft's implementation seem to display the addresses in hexadecimal with leading zeroes, using upper case letters and without the 0x prefix.
Last edited on
std::noshowbase and std::uppercase handle two of those, but the leading zeroes would take std::setw and std::setfill, should we do it manually?
In that case you would have to convert the pointer to an integer before printing and also use std::hex to get hexadecimal.
Last edited on
std::format can print out pointer addresses, it has a format presentation type for pointers, though it currently requires a bit of a 'casting hack' to work:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#include <iostream>
#include <format>

int main()
{
   unsigned int  u { 42 };
   const    int& s { static_cast<int>(u) };

   std::cout << std::format("u = {} s = {}\n", u, s);

   u = 6 * 9;

   std::cout << std::format("u = {} s = {}\n\n", u, s);

   // format presentation type for pointers {:p}
   // https://en.cppreference.com/w/cpp/utility/format/formatter#Standard_format_specification
   // requires a C cast to void*
   std::cout << std::format("&u = {:p} &s = {:p}\n", (void*) &u, (void*) &s);

   // or using C++ casting
   std::cout << std::format("&u = {:p} &s = {:p}\n\n",
                            reinterpret_cast<void*>(&u),
                            reinterpret_cast<void*>(const_cast<int*>(&s)));

   // displaying the nullptr contained address
   std::cout << std::format("nullptr = {:p}\n", (void*) nullptr);
}
u = 42 s = 42
u = 54 s = 42

&u = 0x211a0ffb74 &s = 0x211a0ffb70
&u = 0x211a0ffb74 &s = 0x211a0ffb70

nullptr = 0x0
Registered users can post here. Sign in or register to post.