用< iostream>打印指针 [英] Printing a pointer with <iostream>

查看:138
本文介绍了用< iostream>打印指针的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么

#include <iostream>
using namespace std;

int main() {
  cout << (char*)0x10 << endl; 
}

segfault,但是

segfault, but

#include <iostream>
using namespace std;

int main() {
  cout << (void*)0x10 << endl; 
}

看起来工作正常吗?

推荐答案

因为

cout::operator <<(void*) 

打印一个内存地址,然后

prints a memory address, and

cout::operator <<(char*)

打印一个以空字符结尾的字符数组,当您尝试从0x10读取char数组时遇到不确定的行为.

prints a null-terminated character array, and you run into undefined behaviour when you attempt to read the char array from 0x10.

这篇关于用&lt; iostream&gt;打印指针的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆