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

时间:2019-01-09 09:41:22

标签: c++ linked-list

我正在尝试使用类在c ++中创建链接列表。我仅使用了两种方法pushFront()和traverse()将新元素推入链表的开头,并在任何给定时间显示列表中的元素。每当我使用遍历方法时,都会出现错误“细分错误”

我的代码如下所示。我在此站点和其他地方查询了此错误。据说当我们尝试访问不是我们需要的变量时会发生分段错误。也就是说,这是我们无法进入的空间。但是正如我的代码中看到的那样,我正在尝试使用该类的私有成员。但我在类本身的方法中使用它们。所以我应该被允许这样做吗?我不知道我要怎么做。

class ATest{
    String name = "name";
    @Before
    public void setup{
        class A = new class A();
        class B = Mockito.mock(classB.class);
    }

    public void testmethod1()
    {
        A.method1(name);

        ArgumentCaptor<String> captor = ArgumentCaptor.forClass(String.class);

        Mockito.verify(B, Mockito.times(1)).method2(captor.capture());

        String actual = captor.getValue();
        assertEquals("some data", actual);        
    }
}

我希望输出应为#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; } ,如遍历打印一样。但我得到细分错误错误。谁能指出我做错了什么?

1 个答案:

答案 0 :(得分:5)

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

必须(最小变化):

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

否则,您没有初始化正确的属性,而是初始化了局部变量


如果我使用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'

这正是问题所在


以及关于 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

所以您还会看到 head tail 未初始化等