链表操作的段错误
Segfault on linked list manipulation
我不知道为什么我在这里得到这个段错误。我试图取每一个其他节点,并把它放在一个新的列表。编辑:这是我结束了,但我仍然得到一个段故障
template <class T>
List<T> List<T>::mixSplit()
{
List<T> newList;
newList.length=0;
for (int count=0;count<2;count++)
newList.head=newList.head->next;
newList.tail=tail;
newList.head->prev=NULL;
newList.tail->next=NULL;
return newList;
}
的第一次迭代
for (int count=0;count<1;count++)
newList.head=newList.head->next;
…newList.head
是NULL
…所以使用newList.head->next
是一个坏主意。
我建议你相当正常地遍历当前列表(即:current = head; while(current) ...
),在循环中增加计数器以跟踪列表中的当前位置,并且每当循环计数器为偶数或0 (counter % 2 == 0
或(counter & 1) == 0
)时,使用标准的'list add'函数在新列表上添加新节点。
相关文章:
- 为什么PyImport_ImportModule python 3.7.2 中出现段错误?
- 为什么在访问 vtkRenderWindow 的"交互器"变量时会发生段错误?
- 全局向量导致 C++ 程序结束时出现段错误
- 为什么重载运算符<<打印特征类成员会导致段错误?
- 更改条件段错误
- 使用 TTF_RenderText() 加载字体时获取段错误 TTF_OpenFontRW()
- 注册对对象工厂的调用会导致段错误
- pthread_create在构造函数段错误中
- Nanoflann发现邻居提出段错误
- C++ 中的构造函数、继承、堆栈、堆、this-pointer 和段错误
- 具有unique_ptr的 CRTP 会导致段错误
- 增强纤维work_stealing屏障会导致段错误
- 当我返回指向结构的指针向量时出现段错误
- C++为什么我的代码没有爆炸/段错误?
- Pthread段错误,使用指向main中变量的指针
- C++ 模板中的段错误
- 相当于Windows/MSVC上的段错误?
- 为什么我的 LLVM JIT 实现出现段错误?
- 为什么访问我的引用捕获变量会导致我的 lambda 函数出现段错误?
- 为什么自删除的全局 Vulkan 实例仅在添加层时才导致段错误?