C STD ::列表分割故障
C++ std::list segmentation fault
我在运行程序中有一个分割故障(核心倾倒)。第一个版本运行完美,但我需要列表作为指针,但是该代码不再起作用,请参阅第二个代码。我在做什么错?
运行版本:
int main(int argc, char *argv[]) {
std::list<int> TestList;
for (int i = 0; i < 10; ++i) {
TestList.push_back(i);
}
for (std::list<int>::const_iterator iterator = TestList.begin(), end = TestList.end(); iterator != end; ++iterator) {
std::cout << *iterator << std::endl;
}
return 0;
}
不运行版本:
int main(int argc, char *argv[]) {
std::list<int> *TestList;
for (int i = 0; i < 10; ++i) {
TestList->push_back(i);
}
for (std::list<int>::const_iterator iterator = TestList->begin(), end = TestList->end(); iterator != end; ++iterator) {
std::cout << *iterator << std::endl;
}
return 0;
}
在第二版testList中只是一个指针,它并不指向任何有效的列表对象。尝试
std::list<int>* TestList = new std::list<int>();
请记住,完成后,您也需要正确清理堆分配的内存。
第二个无法运行,因为没有list<int>
对象,而只是指向arbitary内存空间的指针。
如果要使用指针,请先分配内存区域。
相关文章:
- 分割故障C++矩阵
- C ,二进制树的高度,而不是检查我的子树是否为空,而是在检查我的子树是否是叶子节点.抛出分割故障
- 我正在为此代码分割故障
- 在执行strcpy时获取分割故障错误
- 空隙指针分割故障
- Xcode会产生分割故障,而不是例外
- OPENCV C 中的分割故障误差
- 分割故障:核心倾倒C 矢量对字符串:
- C 2D数组导致分割故障(核心倾倒)
- 分割故障/错误读数变量-C 列表
- DLSYM导致分割故障
- 使用C 中的操作员删除的分割故障
- 使用Linux X64下的Libmozjs-52(SpidermonKey)进行分割故障
- 使用长变量时分割故障(11)错误
- C - 返回字符串时的分割故障
- 创建VertexArray时的分割故障
- 向量分割故障的向量
- 从std :: fileSystem ::路径对象的类中退出时的分割故障
- C 矢量分割故障
- 基于向量的数据格式的分割故障误差