简体   繁体   English

如何在C ++中修复“分段错误(核心转储)”?

[英]How to fix “Segmentation fault(core dump)” in C++?

I am trying to create Linked list in c++ using class. 我正在尝试使用类在c ++中创建链接列表。 I used only two methods for it that is pushFront() and traverse() to push the new element in the head of the linked list and display the elements in the list at any given time. 我仅使用了两种方法pushFront()和traverse()将新元素推入链表的开头,并在任何给定时间显示列表中的元素。 Whenever i use traverse method i am getting the error "Segmentation fault" 每当我使用遍历方法时,都会出现错误“细分错误”

My code is shown below. 我的代码如下所示。 I looked up about this error in this site and elsewhere. 我在此站点和其他地方查询了此错误。 Segmentation fault is said to occur when we attempt to access variable which is not ours to take. 据说当我们尝试访问不是我们需要的变量时会发生分段错误。 ie it is is space which is not accesible to us. 即是我们无法进入的空间。 But as one can see in my code I am attempting to use head and tail the private members of the class. 但是正如我的代码中看到的那样,我正在尝试使用该类的私有成员。 But i am using them in the methods of the class themselves. 但我在类本身的方法中使用它们。 So i should be allowed to do that right? 所以我应该被允许这样做吗? I don't know where am i going wrong with this. 我不知道我要怎么做。

#include <iostream>
using namespace std;
struct Node
{
    int data;
    Node * next;
};
class LinkedList
{
    private:
        Node * head;
        Node * tail;
    public:
        LinkedList();
        void pushFront(int i);
        void traverse();
};
LinkedList::LinkedList()
{
    Node * head = NULL;
    Node * tail = NULL;
}
void LinkedList::pushFront(int i)
{
    Node * newNode =new Node;
    newNode->data=i;
    newNode->next=head;
    head=newNode;
    if(tail==NULL)
        tail = head;
}
void LinkedList::traverse()
{
    if (head==NULL){
        cout<<"empty list. add elements";
        return;
    }
    Node * ptr =  head;
    while(ptr!=NULL)
    {
        cout<<ptr->data;
        ptr=ptr->next;
    }
}
int main()
{
    LinkedList l;
    l.pushFront(10);
    l.pushFront(9);
    l.traverse();
    return 0;
}

I expect that output should be 910 as should be printed by traverse. 我希望输出应为910并应通过遍历打印。 But i get the segmentation fault error. 但我得到细分错误错误。 Can anyone point out where i am wrong? 谁能指出我做错了什么?

LinkedList::LinkedList()
{
  Node * head = NULL;
  Node * tail = NULL;
}

must be (minimal change) : 必须是(最小变化):

LinkedList::LinkedList()
{
  head = NULL;
  tail = NULL;
}

else you do not initialize the right attributes but local variables 否则您没有初始化正确的属性,而是局部变量


I encourage you to compile to produce all the warnings, if I compile your code with g++ -Wall -pedantic c.cc this gives 如果我使用g++ -Wall -pedantic c.cc编译您的代码,建议您编译以产生所有警告。

c.cc: In constructor 'LinkedList::LinkedList()':
c.cc:20: warning: unused variable 'head'
c.cc:21: warning: unused variable 'tail'

and this is exactly where the problem is 这正是问题所在


and about valgrind , if I execute under it that gives : 关于valgrind ,如果我在它下面执行,则给出:

valgrind ./a.out
==18508== Memcheck, a memory error detector
==18508== Copyright (C) 2002-2012, and GNU GPL'd, by Julian Seward et al.
==18508== Using Valgrind-3.8.1 and LibVEX; rerun with -h for copyright info
==18508== Command: ./a.out
==18508== 
==18508== Conditional jump or move depends on uninitialised value(s)
==18508==    at 0x400829: LinkedList::pushFront(int) (c.cc:29)
==18508==    by 0x4008C2: main (c.cc:48)
==18508== 
==18508== Conditional jump or move depends on uninitialised value(s)
==18508==    at 0x40089A: LinkedList::traverse() (c.cc:39)
==18508==    by 0x4008DF: main (c.cc:50)
==18508== 
==18508== Use of uninitialised value of size 8
==18508==    at 0x400876: LinkedList::traverse() (c.cc:41)
==18508==    by 0x4008DF: main (c.cc:50)
==18508== 
==18508== Use of uninitialised value of size 8
==18508==    at 0x400888: LinkedList::traverse() (c.cc:42)
==18508==    by 0x4008DF: main (c.cc:50)
==18508== 
==18508== Invalid read of size 4
==18508==    at 0x400876: LinkedList::traverse() (c.cc:41)
==18508==    by 0x4008DF: main (c.cc:50)
==18508==  Address 0x4b43415254475542 is not stack'd, malloc'd or (recently) free'd
==18508== 
==18508== 
==18508== Process terminating with default action of signal 11 (SIGSEGV)
==18508==  General Protection Fault
==18508==    at 0x400876: LinkedList::traverse() (c.cc:41)
==18508==    by 0x4008DF: main (c.cc:50)
9101778121006==18508== 
==18508== HEAP SUMMARY:
==18508==     in use at exit: 32 bytes in 2 blocks
==18508==   total heap usage: 2 allocs, 0 frees, 32 bytes allocated
==18508== 
==18508== LEAK SUMMARY:
==18508==    definitely lost: 0 bytes in 0 blocks
==18508==    indirectly lost: 0 bytes in 0 blocks
==18508==      possibly lost: 0 bytes in 0 blocks
==18508==    still reachable: 32 bytes in 2 blocks
==18508==         suppressed: 0 bytes in 0 blocks
==18508== Rerun with --leak-check=full to see details of leaked memory
==18508== 
==18508== For counts of detected and suppressed errors, rerun with: -v
==18508== Use --track-origins=yes to see where uninitialised values come from
==18508== ERROR SUMMARY: 5 errors from 5 contexts (suppressed: 6 from 6)
Segmentation fault

so you see also the head and tail are not initialized etc 所以你也看到头部尾部没有初始化等等

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM