使用列表迭代器时出现分段错误
Segmentation error with a list iterator
class Line{
list<char> line;
char getChar(const int position) const {
std::list<char>::const_iterator itr;
itr = line.begin();
for (int i = 0; i< position; i++){
itr++;
}
return *itr;//according to the debugger this is where the error happens.
}
所以这是一个在我的程序中被称为LOT的函数,然而在一个特定的实例中,它被调用并导致分割错误。是什么原因导致的呢?
这就是它的名字:
std::list<Line>::const_iterator itr;
itr = file.begin();// file is a list of Lines, line 1 characters 0-4 are all spaces.
while (itr != file.end()) {
int i = 0;//this works with 0 but anything else it gets a segmentation error.
while ((*itr).getChar(i) != 'n') {//This is where it errors.
这是一些已经出现了几次,但对于我的生活,我不记得我是如何解决它。我只是不明白当这个函数在极其相似的情况下多次调用时,它是如何得到这样的错误的。
如果文件中有一行只有换行符,这将导致分段错误。如果你传入1,它会尝试访问行中位置1的东西。但是n
字符在位置0
如果您的第一行是一个空格,那么一旦您在实现中获得索引(i
)超过0,您就会得到分段错误。将迭代器推进到容器的长度之外(超过列表的末尾)。
你可以使用一些标准的STL,它会抛出一个异常,只要你超越了容器的结束(这将给你更多的信息,一个seg错误):
class Line{
list<char> line;
char getChar(const int position) const {
auto itr = line.begin();
advance(itr,position);
return (*itr);
}
};
如果您不使用c++ 11,将auto
更改为std::list<char>::iterator
。如果你想无声地阻止客户端获得超出结束的位置(我不建议):advance(itr,(position > line.size()-1) ? line.size()-1 : position);
(也可能想检查该位置是正的或使其为unsigned int)。
我也不知道你为什么要使用list
,除非你做了很多跨线拼接。如果你的行包含很多字符,我建议使用vector<char>
(如果你知道长度,使用reserve
),或者简单地使用std::string
。
string abcd = "abcd";
cout << abcd.at(0) << endl;
cout << abcd[0] << endl;
cout << abcd.at(4) << endl;
异常将更有用:
libc++abi.dylib: terminating with uncaught exception of type std::out_of_range: basic_string
相关文章:
- 在某些循环内使用vector.push_back时出现分段错误
- 为什么在运行时没有向我们提供有关分段错误的更多信息?
- 如何解决gcc编译器优化导致的centos双编译器设置中的分段错误
- 当我的阵列太大时出现分段错误
- 分段错误当我试图运行程序时出错
- 在c++中初始化矩阵时出现分段错误(核心转储)
- 尝试使用集合函数时出现分段错误
- 我无法缩小此分段错误的原因
- g++的分段错误(在NaN上使用to_string两次时)
- 我是如何在这段代码中出现分段错误的
- 创建结构的数组时遇到分段错误
- 在c++中键入向量中的所有值后,得到分段错误(核心转储)
- 在 c++ 中实现 Trie 时出现分段错误
- 为什么 fstream 在打开带有格式的文件时会导致分段错误?
- 为什么我遇到分段错误?
- 动态类的分段错误(家庭作业问题)
- 分段错误 - 读取初始化指针的数组
- 如何摆脱C ++中的分段错误错误?
- 使用 CTYPE 时出现分段错误
- 为什么代码给出分段错误?