使用指针进行分段错误

时间:2011-03-16 22:57:53

标签: c++ pointers segmentation-fault

我遇到了分段错误,因为LinearNode.cpp中的赋值操作是在setPrevious函数中读取previous = node的行。上一个声明为LinearNode* previous;我可以真正使用一些帮助,因为我在3年内没有使用指针而且记不得太多。谢谢!

LinearNode.cpp

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

using namespace std;

//Constructor for LinearNode, sets next and element to initialized states
LinearNode::LinearNode()
{
    next = NULL;
    previous = NULL;
    element = 0;
}//ends LinearNode default constructor

//Constructor for LinearNode takes an element as argument.
LinearNode::LinearNode(int el)
{
    next = NULL;
    previous = NULL;
    element = el;
}//ends LinearNode constructor

//returns the next element in the structure
LinearNode* LinearNode::getNext()
{
    return next;
}//ends getNext function

//returns previous element in structure
LinearNode* LinearNode::getPrevious()
{
    return previous;
}//ends getPrevious function

//sets the next variable for the node
void LinearNode::setNext(LinearNode* node)
{
    next = node;

}//ends the setNext function

//sets previous for the node
void LinearNode::setPrevious(LinearNode* node)
{
cout << "next" << node -> getElement() << endl;
    previous = node;
}//ends the setPrevious function

//returns element of the node
int LinearNode::getElement()
{
    return element;
}//ends the getelement function

//sets the element of the node
void LinearNode::setElement(int el)
{
    element = el;
}//ends the setElement function

LinkedList.cpp

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

using namespace std;

//linkedlist constructor for an empty linked list
LinkedList::LinkedList()
{
    count = 0;
    contents = NULL;
}//ends the constructor

//adds an element to the front of the linked list
void LinkedList::add(int element)
{

    int found = 0, current = 0;

    for (int index = 0; index < count; index++)
    {
        if (contents -> getElement() == element)
            found = 1;
        else    
        {

            contents = contents -> getNext();
        }//ends the else statement
    }//ends the while loop

    if ((found == 0) && (count == 0))
    {
        LinearNode *node = new LinearNode;
        node -> setElement(element);
        contents = node;
        count++;
print();
    }//ends the if statement
    else
    {

        LinearNode* node = new LinearNode ;
        node -> setElement(element);
        node -> setNext(contents);
        contents -> setPrevious(node);
        contents = node;
        count++;
print();
cout << endl;
    }//ends the found == 0 if statment
}//ends the add function

//this function removes one element from the linked list.
int LinkedList::remove(int element)
{
    int found = 0, result = 0; 
    LinearNode* previous;
    LinearNode* current;

    if (count == 0)
        cout << "The list is empty" << endl;
    else 
    {
        if (contents -> getElement() == element)
        {
            result = contents -> getElement();
            contents = contents -> getNext();
        }//ends the contents.getElement() == element
        else 
        {
            previous = contents;
            current = contents -> getNext();
            for (int index = 0; ( (index < count) && (found == 0) ); index++)
                if (current -> getElement() == element)
                    found = 1;
                else
                {
                    previous = current;
                    current = current -> getNext();
                }//ends the else statement 

            if (found == 0)
                cout << "The element is not in the list" << endl;
            else
            {
                result = current -> getElement();
                previous -> setNext(current -> getNext());
            }//ends else statement  

        }//ends the else stamtement

        count--;
    }//ends the else statement of count == 0
    return result;
}//ends the remove function


void LinkedList::print()
{
    LinearNode* current;
    current = contents; 

    for (int index = 0; index < count; index++)
    {
        cout << current -> getElement() << endl;
        current = current -> getNext();
    }//ends the for loop
}//ends Print function

2 个答案:

答案 0 :(得分:3)

您正在修改我认为应该指向列表头部的contents

contents = contents -> getNext();

当你迭代contents为空(在add()中),然后调用任何认为contents非空的东西,因为count>0你有崩溃。

首次拨打add():count为0,您创建LinearNode *并正确设置contents

第二次调用add():计数为1,您执行contents = contents->getNext()扫描 - 以contents==null结束。然后,您自(found == 0)(count != 0)创建新节点,然后致电contents->setPrevious()

答案 1 :(得分:0)

我不确定这是否能回答你的问题,但我确实注意到一个指针可能为null的地方,这可能会导致段错误:

//sets previous for the node
void LinearNode::setPrevious(LinearNode* node)
{
cout << "next" << node -> getElement() << endl;
    previous = node;
}//ends the setPrevious function

您不检查node实际上是否为null,或者无效。取消引用空指针或错误指针可能会导致段错误。