无法打印缓冲区地址
Cannot print buffer address
我想测试以下代码,但我遇到了编译错误。让我困惑的是,创建和打印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;
您只在试图打印缓冲区内容的流中放入一个<
。
cout << pd3[i] < " at " << &pd3[i] << "; "; // there is only one <
cout << pd4[i] < " at " << &pd4[i] << endl; // ^
确保在流插入运算符中有两个<'s
。
cout << pd3[i] << " at " << &pd3[i] << "; ";
cout << pd4[i] << " at " << &pd4[i] << endl;
相关文章:
- 将数组的地址分配给变量并删除
- C++字符*缓冲区的大小
- 空基优化子对象的地址
- C++ 指针的内存地址和指向数组的内存地址如何相同?
- 为什么msgrcv()将垃圾字符馈送到缓冲区
- 使用动态分配的数组会导致代码分析发出虚假的C6386缓冲区溢出警告
- 为什么我在leetcode上收到AddressSanitizer:地址0x602000000058上的堆缓冲区溢出错误
- LeetCode 1:两和 - 地址清理器:堆缓冲区溢出地址
- 为什么 LeetCode 给出错误:地址清理器:堆缓冲区溢出
- 为什么存储对象地址在缓冲区中会导致内存泄漏并删除它们
- 将新放置到缓冲区后,缓冲区和实例是否具有相同的 void* 地址?
- 无法打印缓冲区地址
- 如何抑制来自 gcc 中地址清理器的堆栈缓冲区溢出
- 从缓冲区指针计算堆栈中的返回地址
- 从C++(Linux 套接字)中的缓冲区中提取 IP 地址
- 将缓冲区的地址存储为无符号整数;
- std::string::operator[]的结果地址是否指向一个可写的、以nul结尾的缓冲区
- 协议缓冲区的地址簿示例未编译
- 分配OpenCL缓冲区时内存地址错误
- 从缓冲区中提取大小字节并将其复制到所提供的数据地址