2016-09-06 2 views
-2

현재 Project Euler #9에 jab을 가져 가고 세그먼트 오류가 발생했습니다. 이러한 segfault는 프로그램을 실행할 때마다 3 ~ 4 번째마다 발생합니다. 누군가가 이것이 왜 더 중요한지 왜 설명 할 수 있습니까? 왜 때마다 segfault (또는 작동)하지 않습니까?런타임 중 세그먼트 오류가 일치하지 않습니다.

세 번째 오류를 두 번째 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.h

#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 

LinkedList.cpp

#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; 
    } 
} 
+6

정의되지 않은 동작은 정의되지 않은 동작입니다. – bolov

+0

세그먼트 오류는 코드에서 노출 된 [정의되지 않은 동작] (https://en.wikipedia.org/wiki/Undefined_behavior)의 결과입니다. 그리고 그것은 정의되지 않았기 때문에, 어떤 규칙도 따르지 않습니다. – axiac

+0

나는 바보 야. 감사합니다 – Geebs

답변

2

정의되지 않은 동작

초기화되지 않은 변수squarewhile 루프를 입력하기 전에 에 액세스 중이므로 while 루프를 입력 할 수도 있고 입력하지 않을 수도 있습니다. 따라서 tripletChoices.head은 삽입이 발생했는지 여부를 확신 할 수 없으므로 null이 아니거나 아닐 수도 있습니다.

따라서 null의 값이 a_factor 인 경우 while(a_factor->next!=NULL)으로 역 참조하면 SegFault가 발생합니다.

관련 문제