0

While I was writing code on a 64 bit machine for a c++ program,I noticed that printing the address of a variable (for example) returns just 12 hexadecimal characters, instead of 16. Here's an example code:

int a = 3 ;
cout sizeof(&a) << " bytes" << endl ;
cout << &a << endl ;

The output is:

8 bytes

0x7fff007bcce0

Obviously, the address of a variable is 8 byte (64 bit system). But when I print it, I get only 12 hexadecimal digits instead of 16.

  • Why this? I think that is due to the fact that the 4 "lost" digits were leading zeroes, that were not printed. But this is only my thought, and I wish to have a definitive and technically correct answer.

  • How could I print the entire address? Is there a built-in solution, or should I manually use "sizeof" in order to get the real lenght and then add to the address the right number of zeroes?

Forgive me, I googled for a day for an answer to my stupid question, but I wasn't able to find an answer. I'm just a newbie. (On stackoverflow I did not find any question/answer about what I needed to know, but maybe I'm wrong.)

Community
  • 1
  • 1

2 Answers2

3

Someone asks a pretty similar question here: c++ pointer on 64 bit machine

Hope this helps :)

To print the full 64bit address with leading zeros you can use:

std::cout
<< "0x"
<< std::hex
<< std::noshowbase
<< std::setw(16)
<< std::setfill('0')
<< n
<< std::endl ;

Got it from: How can I pad an int with leading zeros when using cout << operator?

lornova
  • 6,667
  • 9
  • 47
  • 74
nablahero
  • 143
  • 1
  • 13
  • Great! I didn't know that only 48 bytes were used on most systems... so 48/4 = 12 hex digits . This is a new thing that I've learned just now thanks to your fast answer :) – Patrick Roncagliolo May 19 '15 at 12:57
  • But the lenght of the address is 64 bit (c++ stores it in 8 bytes), and so, how can I print the entire address, with the leading zeroes? In the question/answer you linked, someone wrote about std::hex manipulator... How can I use it? And,does it allow me to print the entire address (so all its 8 bytes)? – Patrick Roncagliolo May 19 '15 at 13:01
  • 1
    I just copy and paste code from somewhere else: `std::cout << "0x" << std::hex << std::noshowbase << std::setw(16) << std::setfill('0') << &a << std::endl ;` – nablahero May 19 '15 at 13:08
  • So, this is a workaround to print what I asked for... And it is good. But is there no way to print the hex value with leading zeroes, without having to add zeroes artificially? I mean, a pointer stores a 8 byte address, for example 0x00001234567890ab, and this value, when it's printed out, is shorter, because the stream doesn't consider the leading zeroes. So the output would be only 0x1234567890ab, and the solution you posted is that we have to split this string and add four zeroes. Is there something that, if the value stored is 0x0000ab, prints directly 0x0000ab without workarounds? – Patrick Roncagliolo May 19 '15 at 13:28
  • Nothing i know of - You could use the old c function printf with `printf("%16X", &a)` . Not sure if that is better for you. – nablahero May 19 '15 at 14:42
0

I am currently writing a book on C++ and windows 32-bit programming for peeps such as you, but unfortunately I am not yet done with it :(

The following code demonstrates how you would display a 64-bit unsigned number using cout method:

// Define a 64-bit number. You may need to include <stdint.h> header file depending on your C++ compiler.

uint64_t UI64 = 281474976709632ULL; // Must include ULL suffix and this is C99 C++ compiler specific.

// unsigned __int64 UI64 = 281474976709632ULL; // Must include ULL suffix and this is Microsoft C++ compiler specific.

// Set decimal output.

cout << dec;

// Display message to user.

cout << "64-bit unsigned integer value in decimal is: " << UI64 << endl;

cout << "\n64-bit unsigned integer value in hexadecimal is: ";

// Set the uppercase flag to display hex value in capital letters.

cout << uppercase;

// Set hexadecimal output.

cout << hex;

// Set the width output to be 16 digits.

cout.width(16);

// Set the fill output to be zeros.

cout.fill('0');

// Set right justification for output.

right(cout);

// Display the 64-bit number.

cout << UI64 << endl;

You may need to (type) cast the address into a 64-bit unsigned value. In this case, you can do the following:

// (type) cast pointer adddress into an unsigned 64-bit integer.

uint64_t UADD64 = (uint64_t)&UI64; // C99 C++ compiler specific.
jwd
  • 10,837
  • 3
  • 43
  • 67
Richard
  • 11