这段代码在执行时显示了预期的输出,但在最后打印了 segmentation fault (core dumped)
:
string str[4] = {
"Home",
"Office",
"Table",
"Bar"
};
for (int i = 0; i<5; i++)
{
cout << str[i] << "\n";
}
输出:
Home
Office
Table
Bar
Segmentation fault (core dumped)
段错误(核心转储)的意义是什么。我搜索了一下,当您尝试访问未分配的内存时,似乎会发生类似的错误,那么,上面的代码有什么问题吗?
最佳答案
你应该写:
for (int i = 0; i<4; i++) //0,1,2,3 = total 4 values
{
cout << str[i] << "\n";
}
关于c++ - c++中的段错误(核心转储),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11176820/