为什么会导致分段错误? [英] Why is this causing a segmentation fault?

查看:186
本文介绍了为什么会导致分段错误?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在C ++中的项目中编码了,通常我不会有太多的分段错误的麻烦,但我是C ++的新手。基本上,我做一个指向IntList的指针,并调用prepend()从指针做一个IntList。问题是当prepend被调用它被卡住在头文件中的某个地方justd退出。我不知道是什么导致这个和gdb告诉我,它只是卡在标题。帮助将真正感谢,像一个提示或线索,我做错了。谢谢。

I've been coding this in project in C++, normally I wouldn't have too much trouble with a segmentation fault, but I'm new to C++. Basically I'm making a pointer to an IntList and calling prepend() to make a IntList from the pointer. The problem is when prepend is called it is getting stuck somewhere in the header file an justd exits out. I can't tell what is causing this and gdb tells me it just gets stuck at the header. Help would be really appreciated, like a hint or clue to what I'm doing wrong. Thank you.

IntList.h:

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:

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:

testIntList.cpp:

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

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

}

gdb一步一步:

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  {


推荐答案

任何未初始化的指针都有未定义的值,例如它可能指向堆的未分配区域中的随机垃圾。对未分配的堆区域的任何引用都是分段错误。

Any pointer that has not been initialized has an undefined value, e.g. it probably points to random garbage in the unallocated regions of the heap. Any dereference into an area of the heap which is not allocated is a segmentation fault.

您需要使用new运算符来分配一些堆内存,并获取该地址记忆。
在main:

You need to use the new operator to allocate some heap memory and get the address of that memory. In main:

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

在预设中:

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

看起来你想使用运算符的地址将x指向y,即 x =& y 但是这将返回一个指向垃圾的指针。任何局部变量都分配在堆栈上,并在函数返回时立即释放。在指向离开作用域的值之后,不要保留指向堆栈内存的指针,因为该内存很可能会被快速重新分配给其他内容,从而防止崩溃。导致神秘的行为。

It looks like you wanted to use the address-of operator to point x at y, i.e. x=&y but this will return a pointer to garbage. Any local variable is allocated on the stack and deallocated as soon as the function returns. Never retain a pointer to stack memory after the value pointed to leaves scope, as that memory is likely to be quickly re-allocated to something else, preventing a crash & causing mysterious behavior.

查找delete操作符。

Look up the delete operator as well.

这篇关于为什么会导致分段错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆