gpt4 book ai didi

c++ - 无法打印缓冲区地址

转载 作者:行者123 更新时间:2023-11-28 00:17:18 26 4
gpt4 key购买 nike

我想测试下面的代码,但是我得到一个编译错误。令我困惑的是,创建和打印 pd1 和 pd2 的方式与 pd3 和 pd4 相同,但编译器在我打印时提示 pd3 和 pd4。

const int BUF = 512;
const int N = 5;
char buffer[BUF]; // chunk of memory
int main(){
using namespace std;

double *pd1, *pd2;
int i;
cout << "Calling new and placement new:\n";
pd1 = new double[N]; // use heap
pd2 = new (buffer) double[N]; // use buffer array
for (i = 0; i < N; i++)
pd2[i] = pd1[i] = 1000 + 20.0*i;
cout << "Buffer addresses:\n" << " heap: " << pd1 << " static: " << (void *)buffer << endl;
cout << "Buffer contents:\n";
for(i = 0; i < N; i++) {
cout << pd1[i] << " at " << &pd1[i] << "; ";
cout << pd2[i] << " at " << &pd2[i] << endl;
}

cout << "\nCalling new and placement new a second time:\n";
double *pd3, *pd4;
pd3 = new double[N];
pd4 = new (buffer) double[N];
for(i = 0; i < N; i++)
pd4[i] = pd3[i] = 1000 + 20.0 * i;
cout << "Buffer contents:\n";
for (i = 0; i < N; i++) {
cout << pd3[i] < " at " << &pd3[i] << "; ";
cout << pd4[i] < " at " << &pd4[i] << endl;
}

return 0;
}

编译错误:

newplace.cpp: In function ‘int main()’:
newplace.cpp:33:36: error: invalid operands of types ‘const char [5]’ and ‘double*’ to binary ‘operator<<’
cout << pd3[i] < " at " << &pd3[i] << "; ";
^
newplace.cpp:34:36: error: invalid operands of types ‘const char [5]’ and ‘double*’ to binary ‘operator<<’
cout << pd4[i] < " at " << &pd4[i] << endl;

最佳答案

你在这里漏掉了一个<符号

 cout << pd3[i] < " at " << &pd3[i] << "; ";
cout << pd4[i] < " at " << &pd4[i] << endl;

尝试

 cout << pd3[i] << " at " << &pd3[i] << "; ";
cout << pd4[i] << " at " << &pd4[i] << endl;

关于c++ - 无法打印缓冲区地址,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29451044/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com