简体   繁体   English

打印 64 位 C++ 完整内存地址

[英]print 64bit c++ full memory address

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:当我在 64 位机器上为 C++ 程序编写代码时,我注意到打印变量的地址(例如)只返回 12 个十六进制字符,而不是 16 个。这是一个示例代码:

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

The output is:输出是:

8 bytes 8 字节

0x7fff007bcce0 0x7fff007bcce0

Obviously, the address of a variable is 8 byte (64 bit system).显然,变量的地址是 8 字节(64 位系统)。 But when I print it, I get only 12 hexadecimal digits instead of 16.但是当我打印它时,我只得到 12 个十六进制数字而不是 16 个。

  • Why this?为什么这个? I think that is due to the fact that the 4 "lost" digits were leading zeroes, that were not printed.我认为这是因为 4 个“丢失”的数字是前导零,没有打印出来。 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?是否有内置解决方案,或者我应该手动使用“sizeof”以获得真实长度,然后在地址中添加正确数量的零?

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.) (在 stackoverflow 上,我没有找到关于我需要知道的内容的任何问题/答案,但也许我错了。)

Someone asks a pretty similar question here: c++ pointer on 64 bit machine有人在这里问了一个非常相似的问题: 64 位机器上的 c++ 指针

Hope this helps :)希望这可以帮助 :)

To print the full 64bit address with leading zeros you can use:要打印带有前导零的完整 64 位地址,您可以使用:

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?来自: 如何在使用 cout << 运算符时用前导零填充 int?

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 :(我目前正在为像你这样的人写一本关于 C++ 和 windows 32 位编程的书,但不幸的是我还没有完成:(

The following code demonstrates how you would display a 64-bit unsigned number using cout method:以下代码演示了如何使用 cout 方法显示 64 位无符号数:

// 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.您可能需要(键入)将地址转换为 64 位无符号值。 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.

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM