簡體   English   中英

C++ 中奇怪的段錯誤

[英]Weird segfault in C++

我在構造函數中檢查頭節點的數據,它沒問題,但是當我在函數中檢查它時,它就消失了。 為什么這個指針這么鬼?

隊列頭

#ifndef QUEUE_H
#define QUEUE_H
#include "Node.h"
#include "LinkedList.h"

class Queue
{
    public:
        Queue();
        ~Queue();
        void addTail(Node* newNode);
        Node* deleteHead();
        int isEmpty();
        void PrintQueue(char* outName);

    protected:
    private:
        Node* head;
        Node* tail;
        LinkedList* myList;
};

#endif // QUEUE_H

隊列類

#include "Queue.h"

Queue::Queue()
{
    //constructor
    myList =  new LinkedList();
    head = myList->getHead();
    std::cout<<" head here is " << head->getData()<<std::endl; //
    std::cout<<" next after head is " << head->getNext()->getData()<<std::endl; //
    tail = myList->getTail();
}

void Queue::addTail(Node* newNode)
{
    std::cout<<"inserting before tail (queue)"<<std::endl;
    std::cout<<"new node is " << newNode->getData() <<std::endl;
    std::cout<<" head here is " << myList->getHead()->getData() <<std::endl;
    myList->insertLast(newNode);
}

Node* Queue::deleteHead()
{
    if(isEmpty()){
        std::cout<<"Queue is empty. Cannot remove anything anymore. Add more data to the queue!"<<std::endl;
        return head;
    }
    return myList->removeFirst();
}

int Queue::isEmpty()
{
    return myList->isEmpty();
}

void Queue::PrintQueue(char* outName)
{
    Node* cur = head->getNext(); //store current node to keep track. Set it to one after the head
    std::ofstream outfile;
    outfile.open(outName, std::ios::app);
    outfile << "Printing the values of the queue: ";
    std::cout<<"Printing the values of the queue: "<<std::endl;
    while(cur != tail)
    {
       std::cout<< cur->getData()<<", "<<std::endl; //print to the console
       outfile << cur->getData() <<", "; //print to file
    }
    outfile.close();
}

Queue::~Queue()
{
    //destructor
    delete myList;
}

主功能

#include <iostream>
#include <fstream>
#include <string>
#include "Stack.h"
//#include "Stack.cpp"
#include "LinkedList.h"
//#include "LinkedList.cpp"
#include "Node.h"
//#include "Node.cpp"
#include "HashTable.h"
//#include "HashTable.cpp"


using namespace std;

int main( int argc, char* argv[] )
{
   //specifying the in and out files
    char* inFileName = argv[1];
    char* outFileName = argv[2];


    std::fstream infile (inFileName) ; // input file
    //open the input file and find largest integer
    int num;
    int largest = 0;
    Stack * myStack = new Stack();
    char buffer[33]; //create a buffer for using with itoa
    std::ofstream outfile;
    outfile.open(outFileName);

    if ( !infile.is_open() ) //check if input file is open
      cout<<"Could not open file\n";
    else {
        while (infile >> num) { // read file int by int and check if current is not the largest
            if(num > largest) largest = num;
            Node *newNode= new Node(itoa(num,buffer,10));
            myStack->push(newNode);
        }
    }
    std::cout<< std::endl;
    infile.close(); //close files that you read to avoid memory leaks

    myStack->PrintStack(outFileName);

    HashTable* hashBrown = new HashTable();

    int currentDigit = 0;
    int currentTable = 0;
    int numOfDig =0; //stores number of digits of the largest number
    string maxNum = itoa(largest,buffer,10);
    numOfDig = maxNum.length();
    std::cout<< "Num of digits " << numOfDig << std::endl;
    Node* current;
    while(!myStack->isEmpty())
    {
        current = myStack->pop();
        std::cout<< "pop from stack element " << current->getData() << std::endl;
        string str = current->getData();
        int index = atoi(&str.back());
        std::cout<< "insert at index " << index << std::endl;
        std::cout<< "inserting data: "<< current->getData()<< " at index:" << index << std::endl;
        hashBrown->myQueues[index].addTail(current);

    }
    hashBrown->printHashTable(outFileName);


    delete myStack;
    delete hashBrown;

    outfile.close();
    std::cout<< "finishing program " << std::endl;
    return 0;

}

鏈表

#include "LinkedList.h"
#include "Node.h"




LinkedList::LinkedList()
{
    //constructor
    head = new Node("head"); //dummy variable
    tail = new Node("tail"); //dummy variable
    head->setNext(tail);

}

void LinkedList::insertNode(Node* newNode, Node *position)
{

    newNode->setNext(position->getNext());         // set its pointer to position
    position->setNext(newNode);
}

void LinkedList::insertFirst(Node* newNode)
{
    std::cout<<"head here is  "<< head->getData() <<std::endl; //
    insertNode(newNode, head);
}

void LinkedList::insertLast(Node* newNode)
{
    std::cout<<"inserting before tail (LinkedList)"<<std::endl; //
    Node* cur = head;
    std::cout<<"outside the loop "<< cur->getData() <<std::endl; //
    std::cout<<"current node is "<< cur->getData() <<std::endl; //
    while(cur->getNext() != tail) //iterate until you reach one before tail
    {
        std::cout<<"current node is "<< cur->getData() <<std::endl; //
        cur = cur->getNext();

    }
    std::cout<<"inserting before tail"<<std::endl; //
    insertNode(newNode, cur); //insert at the end before the dummy tail

}

Node* LinkedList::removeFirst()
{
    if(isEmpty())
    {
        return head;
    }
    Node* result = head->getNext(); //store pointer to Node that you need to return
    head->setNext(result->getNext());
    return result;
}


Node* LinkedList::getTail()
{
    return tail;
}

Node* LinkedList::getHead()
{
    return head;
}

int LinkedList::isEmpty()
{
    return head->getNext() == tail;
}

std::string LinkedList::printList(){
    Node *current = head;
    std::string str;
    //append pieces of string to create new line
    while(current != tail){
            str.append (" --> (");
            str.append ( current->getData());
            str.append (",");
            str.append (current->getNext()->getData());
            str.append (")") ;
            current = current->getNext();
    }
    std::cout<<str<<std::endl; //display new
    return str; //return string containing next line that will be written to a file in main

}

LinkedList::~LinkedList()
{
    //destructor
    Node *current = head;
    while( current->getNext() != tail ) { //go through whole linked list and delete each node to free memory
        Node* next = current->getNext();
        delete current;
        current = next;
    }
    delete current;
    delete tail;
}

所以一切正常,直到它嘗試訪問隊列的 addTail() 函數中的頭部或其數據。

您可以使用gdb來調試您的程序,查找錯誤並修復它們。 如果程序內核,您可以使用backtrace查看導致內核的原因。

  1. 在您認為程序行為連接的行上設置斷點,您也可以讓程序一步一步地運行。
  2. 運行程序並打印變量值以查看變量值是否符合預期。 如果變量是指針,還可以打印它引用的內容。

一個簡短的gdb 教程

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM