C++ 运行时出现不一致的分段错误

C++ 运行时出现不一致的分段错误,c++,C++,我目前正在尝试,遇到了分割错误。这些故障仅在我运行程序的第3-4次出现一次。有人能解释一下为什么会出现这种情况,更重要的是,为什么它不会每次都出错(或工作) 我已将SEGFULT精确定位到第二次while循环的开始,但仍然无法确定根本原因 #include <iostream> #include "LinkedList.h" using namespace std; int main(){ int square, sum, answer = -1; int sta

我目前正在尝试,遇到了分割错误。这些故障仅在我运行程序的第3-4次出现一次。有人能解释一下为什么会出现这种情况,更重要的是,为什么它不会每次都出错(或工作)

我已将SEGFULT精确定位到第二次
while
循环的开始,但仍然无法确定根本原因

#include <iostream>
#include "LinkedList.h"

using namespace std;

int main(){
    int square, sum, answer = -1;
    int start = 1;

    LinkedList tripletChoices;    

    while (square<=1000){
        //create new node
        node * factor = new node;
        factor->root = start;
        square = start*start;
        factor->square = square;

        //insert into list
        if (square<=1000) tripletChoices.insertNode(factor);

        start++;
    }

    node * a_factor = tripletChoices.head;

    /** segfaults just after this ***********************/
    cout<<"before segfault" << endl;

    while(a_factor->next!=NULL){

        cout<<"after segfault" << endl;

        node * b_factor = a_factor->next;

        while(b_factor->next!=NULL){
            sum = a_factor->square + b_factor->square;

            cout<<"A: " << a_factor->square << " B: " << b_factor->square<< " sum:" << sum <<endl;

            node * c_factor = tripletChoices.head;

            while(c_factor->next!=NULL){

                if (sum == c_factor->square){
                    if ((a_factor->root + b_factor->root + c_factor->root)==1000){
                        answer = a_factor->root * b_factor->root * c_factor->root;
                        break;
                    }
                }

                c_factor = c_factor->next;
            }

            b_factor = b_factor->next;
        }

        a_factor = a_factor->next;
    }

    cout<<"Answer: " << answer << endl;
}
LinkedList.cpp
#包括“LinkedList.h”
#包括
//默认构造函数-使用头节点初始化列表
LinkedList::LinkedList(){
head=NULL;
listLength=0;
}
//用于插入新节点的setter方法
//在列表的开头插入新节点
bool LinkedList::insertNode(node*newNode){
新建节点->下一步=头部;
头=新节点;
listLength++;
返回true;
}
//析构函数取消分配列表使用的内存
LinkedList::~LinkedList(){
节点*p=头部;
节点*q=头部;
while(q){
p=q;
q=p->next;
如果(q)删除p;
}
}
由于访问未初始化的局部变量而导致未定义的行为

在进入
while
循环之前,您正在访问未初始化的变量
square
,因此它可能会也可能不会进入
while
循环。所以
TripleToices.head
可能是也可能不是非空的,因为您无法确定是否会发生任何插入


因此,当(a_factor->next!=null)时,在
中取消引用
null
a_因子
,将导致SegFault。

未定义的行为是未定义的行为分段错误是代码暴露的结果。因为它没有定义,所以它不遵循任何规则。我是个白痴。感谢
if(q)删除p
这似乎是错误的。与其创建自己的链表类,为什么不直接使用
std::list
,其中
node
只是
struct node{int root,square;}?作业的目的是解决问题,而不是陷入创建自制链表的泥潭,对吗?
#ifndef LinkedList_h
#define LinkedList_h

struct node{
    int root;
    int square;
    node *next;
};

class LinkedList{
    public: 
        node * head;
        int listLength;

        //default constructor creates head node
        LinkedList();

        //setter method
        bool insertNode(node * newNode);

        //destructor de-allocates memory used by the list
        ~LinkedList();
};

#endif
#include "LinkedList.h"
#include <iostream>

//Default Constructor - initilizes list with head node
LinkedList::LinkedList(){
    head = NULL;
    listLength = 0;
}

// setter method for inserting a new node
// inserts new node at the head of the list
bool LinkedList::insertNode(node * newNode){
    newNode->next = head;
    head = newNode;
    listLength++;
    return true;
}

//Destructor de-allocates memory used by list
LinkedList::~LinkedList(){
    node * p = head;
    node * q = head;

    while(q){
        p = q;
        q = p->next;
        if (q) delete p;
    }
}