c++链表segfault调试
c++ linked list segfault debug
我在C++中实现自己的linked_list时遇到了一个segfault。我花了几个小时在上面,但仍然无法找出错误。任何帮助都将不胜感激。提前谢谢。
我认为错误在find_kth1()函数中,但我不知道我做错了什么。我可以按预期打印链接列表。
/*
*
* find kth to last element in a singly linked list
*/
#include <iostream>
// #include "../linked_list.h"
using namespace std;
struct Node {
int data ;
struct Node *next;
};
Node* push(Node *head, int d){
// push to top
Node *n = new Node;
n->data = d;
n->next = head;
return n;
}
void print_LL( Node* head){
Node* p = head;
while(p) {
cout << p->data << ' ';
p = p->next;
}
}
int find_length( Node* head) {
int len = 0;
Node* p = head;
while(p){
len ++;
p = p->next;
}
return len;
}
Node* find_kth1( Node* head, int k){
// find length, scan twice
int len = find_length(head);
Node *p = head;
int i = 0;
while( i < len-k){
i ++ ;
p = p->next;
}
return p;
}
int main( void){
Node *head;
head = push( head, 2) ;
head = push( head, 3) ;
head = push( head, 4) ;
head = push( head, 5) ;
head = push( head, 2) ;
print_LL( head);
int k = 3;
Node *p = find_kth1( head, k);
// cout<<p->data<<endl;
}
头指针需要初始化
它与未初始化一起使用
通常编译器会发出警告,所以总是要注意的警告
Node *head=NULL;
相关文章:
- 当回溯以零开始时,如何调试崩溃
- C++中带有List类的迭代器Segfault
- 在linux上调试巨大的C++项目
- 为什么使用__LINE_的代码在发布模式下在MSVC下编译,而不是在调试模式下
- 如何针对特定情况调试和修复此双自由内存损坏问题
- 正在VS调试器中监视映射条目
- 使用调试/崩溃报告将应用程序部署到客户端
- 使用Vulkan hpp vk::enumerateInstanceVersion()会导致segfault
- VC++本机单元测试,找不到调试符号
- 如何在C++生成器中禁用"使用调试.dcus"
- 使用vscode调试时,GDB意外退出
- 即使使用调试编译标志,表达式也是"optimized out"
- 调试 CUDA MMU 故障
- 小字符串优化(调试与发布模式)
- 调试和自由执行中的信号处理
- 调试编译的服务器在数据库打开时崩溃
- Visual Studio 调试优化如何工作?
- 调试版本SEGFAULT而发布版本有效-是RVO吗
- 调试Boost序列化segfault问题
- c++链表segfault调试