cout << (void *) test 什么意思?

mr_moran 2006-05-18 07:01:25
#include <new>
using namespace std;
int main()
{
char total[512];
char* pd1 = new (total) char[2];
char* pd2 = new (total) char[2];
cout << "The address of total is " << &total << endl
<< "The address of pd1 is " << &pd1 << endl
<< "The address of pd2 is " << &pd2 << endl;

cout << "The other address of total is " << (void *) total << endl
<< "The other address of pd1 is " << (void *) pd1 << endl
<< "The other address of pd2 is " << (void *) pd2 << endl;
cin.get();
}

两个cout 显示的结果不相同,不明白void * 究竟是什么意思.
...全文
239 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
mr_moran 2006-05-18
  • 打赏
  • 举报
回复
哦,那我明白了,感谢大家.
结帖,送分~!
wf520pb 2006-05-18
  • 打赏
  • 举报
回复
mr_moran 2006-05-18
  • 打赏
  • 举报
回复
错了,是以total为首地址,开辟一块2bit的空间,
然后,再以total+2为首地址,开辟另一块2bit的空间?
mr_moran 2006-05-18
  • 打赏
  • 举报
回复
char* pd2 = new (total+2) char[2]; //这句的意思是不是说,以total为首地址,再开辟一个2bit的空间?
tatbaby 2006-05-18
  • 打赏
  • 举报
回复
char* pd1 = new (total) char[2];
char* pd2 = new (total+2) char[2]; //是否应该加2,否则同一个地址上new两次??
cout << "The address of total is " <<(void *) total << endl
<< "The address of pd1 is " <<(void*) pd1 << endl //这才是输出pd1分配到的空间的地址写法
<< "The address of pd2 is " <<(void*) pd2 << endl;
tatbaby 2006-05-18
  • 打赏
  • 举报
回复
#include <iostream>
#include <new>
using namespace std;
那是因为你用错了!!!!!!!!!!
int main()
{
char total[512];
char* pd1 = new (total) char[2];
char* pd2 = new (total) char[2];
cout << "The address of total is " << &total << endl
<< "The address of pd1 is " << &pd1 << endl
<< "The address of pd2 is " << &pd2 << endl;

cout << "The other address of total is " << (void *) &total << endl ///&
<< "The other address of pd1 is " << (void *) &pd1 << endl ///&
<< "The other address of pd2 is " << (void *) &pd2 << endl;///&
cin.get();
}

注意,多了3个取地址&
fangrk 2006-05-18
  • 打赏
  • 举报
回复
char* pd1 = new (total) char[2];
char* pd2 = new (total) char[2];

这属于"定位new",pd1/pd2是在total的位置上的,都等于total
mr_moran 2006-05-18
  • 打赏
  • 举报
回复
cout << "The other address of total is " << (void *) total << endl
<< "The other address of pd1 is " << (void *) pd1 << endl
<< "The other address of pd2 is " << (void *) pd2 << endl;

那么为什么这条cout 语句输出的结果都是一样的呢?都是相同的内存地址.
这不是我的意图啊.

我在total的512里面各划分了两个字节分配给pd1 和 pd2 .
pd1 和 pd2的内存地址应该相差2啊. 而且pd1的内存地址,不也应该等于total的首地址么?
可是,编译的结果却不是这样的啊。.
yeahspyme 2006-05-18
  • 打赏
  • 举报
回复
cout << "The address of total is " << &total << endl
<< "The address of pd1 is " << &pd1 << endl
<< "The address of pd2 is " << &pd2 << endl;
// 这象是在输出指针的指针

cout << "The other address of total is " << (void *) total << endl
<< "The other address of pd1 is " << (void *) pd1 << endl
<< "The other address of pd2 is " << (void *) pd2 << endl;
// 因为<<对于char*(char[])是作为C字符串看待的,为了输出total指针的内容,用了类型转换而已

64,642

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧