当我尝试运行以下内容时
// Example program
#include <iostream>
#include <string>
int* x;
int main()
{
x = (int[5]) { 16, 2, 77, 40, 12071 };
std::cout << x;
}
Run Code Online (Sandbox Code Playgroud)
我收到以下消息 error: taking address of temporary array
这是什么意思?
(int[5]) { 16, 2, 77, 40, 12071 }是一个匿名的临时.一旦分配完成,它就超出了范围.
这让你有一个悬垂的指针.它x在全局命名空间中没有任何区别.
使用std::vector代替; 利用初始化列表构建.
| 归档时间: |
|
| 查看次数: |
494 次 |
| 最近记录: |