Why does this cause a segmentation error?

I encoded this in a C ++ project, usually I would not have too many problems with a segmentation error, but I am new to C ++. Basically, I am making a pointer to an IntList and calling prepend () to make an IntList from a pointer. The problem is that when the preend is called, it gets stuck somewhere in the header file, and justd shuts down. I can't say what causes this, and gdb tells me that it just gets stuck in the header. Help would really be appreciated as a hint or hint about what I am doing wrong. Thank.

IntList.h:

#ifndef _INTLIST_H
#define _INTLIST_H

#include <string>
#include <cstring>
using namespace std;

class EmptyIntList;

class IntList
{
public:
     static IntList *emptyList();
     //static IntList *fromString(string s);                                                                                                                                                         

     virtual bool     isEmpty();
     IntList *prepend(int n);
     virtual int      head();
     virtual IntList *tail();
     string   toString();

     //     virtual int      length();                                                                                                                                                               
     //     virtual IntList *append(IntList *lst);                                                                                                                                                   

     //     virtual int      operator[](int n);                                                                                                                                                      

     //     virtual ~IntList();                                                                                                                                                                      

protected:
     IntList();
     IntList(IntList &);
     //     const IntList &operator=(const IntList &);                                                                                                                                               
private:
     int      data;
     IntList *rest;
};


IntList *operator+(IntList &lst1, IntList &lst2);
ostream &operator<<(ostream &outStream, IntList *lst);
ostream &operator<<(ostream &outStream, IntList &lst);

#endif

IntList.cpp:

#include "IntList.h"
#include "EmptyIntList.h"
#include <sstream>

IntList::IntList(){}

IntList *IntList::emptyList(){

  return ( (IntList*)EmptyIntList::emptyList() );

}

bool IntList::isEmpty(){

  return false;

}

IntList *IntList::prepend(int n){

  IntList *x;

  IntList y;

  *x = y;

  y.data = n ;

  y.rest = x ;

  return x;

}

int IntList::head(){

  return data;

}

IntList *IntList::tail(){

  return rest;

}

testIntList.cpp:

int main()
{
  int n;
  IntList *x;
  n=6;

  x->prepend(n);
  //  cout << x->toString();                                                                                                                                                                         
  return 0;

}

gdb step by step:

8   int main()
(gdb) step
12    n=6;
(gdb) 
14    x->prepend(n);
(gdb) 
IntList::prepend (this=0x0, n=6) at IntList.cpp:30
30    IntList y;
(gdb) 
IntList (this=0x7fff93ecb3c0) at IntList.cpp:12
12  IntList::IntList(){}
(gdb) 
IntList::prepend (this=0x0, n=6) at IntList.cpp:32
32    *x = y;
(gdb) 
IntList::operator= (this=0x401650) at IntList.h:18
18  {
(gdb) 

Program received signal SIGSEGV, Segmentation fault.
0x0000000000401361 in IntList::operator= (this=0x401650) at IntList.h:18
18  {
0
source share
4 answers

, , undefined, . , , . , , .

. :

IntList *x; // IntList
x = new IntList();

preend:

IntList *x;
x = new IntList(); // We now have no need for the local variable y
x->data = n;

, x y, .. x=&y, . , . , , , , - , .

.

0
IntList *x;

, , .

+1

, malloc, :  IntList * x;

"y", .

0

.

1) IntList, IntList *, . x- > prepend, . , .

2) Your prepend method creates an IntList localization and returns it, which is no-no. You are returning an object that was on the stack, and now it is no longer valid (although sometimes this may work. It's funny that the behavior is undefined.)

0
source

All Articles