如何修复分段错误运行时错误

时间:2019-09-29 20:18:22

标签: c++

我在处理Pila类(它是使用记录的链接列表的ADT)时遇到了麻烦,更确切地说,问题出在Pila类的push方法中。当T类型(record的elem属性类型)为int时,没有问题,返回值应为0,但是当T类型为PaccoPostale时,返回值为3221225477,但无论如何它似乎都能完成工作。 谁能帮助我弄清楚正在发生的事情和/或理解改变这种奇怪的行为吗?!

#include <iostream>
#include <string.h>
#define N 20
using namespace std;


class PaccoPostale{
        friend ostream& operator<<(ostream& ,const PaccoPostale& );
        friend istream& operator>>(istream& ,PaccoPostale& );
        public:
            explicit PaccoPostale(const int& c=0,const float& p=0, const char* i=""):codice(c),peso(p),indirizzo(new char[strlen(i)+1]){
                strcpy(indirizzo,i);
            }



PaccoPostale(const PaccoPostale& p)
            :codice(p.codice),peso(p.peso),indirizzo(new char[strlen(p.indirizzo)+1]){
                strcpy(indirizzo,p.indirizzo);
            }

            ~PaccoPostale(){delete [] indirizzo;}
            int get_codice()const{return codice;}
            void set_indirizzo(const char* i){delete [] indirizzo; indirizzo=new char[strlen(i)+1]; strcpy(indirizzo,i);}
            bool operator<(const PaccoPostale& );
            const PaccoPostale& operator=(const PaccoPostale& );
        private:
            int codice;
            float peso;
            char* indirizzo;
    };


        typedef PaccoPostale T;
    //  typedef int T;

    struct Rec{
        T elem;
        Rec* next;
    };

    class Pila{
        public:
            Pila(){start();}
            ~Pila(){if(l!=0) delete [] l;}
            void start(){l=0; n=0;}
            void push(const T& );
            void pop(T& );
            void top()const;
            bool empty()const{return (n==0);}
            bool full()const{return (n==N);}
        private:
            Rec* l;
            unsigned int n;
    };

//the method that generates the problem when T is PaccoPostale is the `//following`

void Pila::push(const T& e){
    if(l==0){
        l=new Rec;
        l->elem=e;
        l->next=0;
    }else{
        Rec* t=0;
        if(l->elem<e){
            t=new Rec;
            t->elem=e;
            t->next=l;
            l=t;
        }else{
            t=l->next;
            Rec* p;
            while(t!=0 && (t->elem)<e){
                p=t;
                t=t->next;
            }
            if(t==0){
                t=new Rec;
                p->next=t;
                t->elem=e;
                t->next=0;
            }else{
            //  if((t->elem)==e)
            //      throw Errore();
                Rec* q;
                q=new Rec;
                q->elem=e;
                q->next=t->next;
                t->next=q;
            }
        }
    }
}

const PaccoPostale& PaccoPostale::operator=(const PaccoPostale& p){
        codice=p.codice;
        peso=p.peso;
        set_indirizzo(p.indirizzo);
    }

 /*   The function should add to the linked list a new element in order but without generating segmentation fault at run time but as i said the return value when i use that method in the main is not 0 but 3221225477.*/


//this is the main that returns the value i'm talking about

int main(int argc, char** argv) {
    PaccoPostale p(1,2,"ok");
    Pila pil;
    pil.push(p);
    return 0;
}

1 个答案:

答案 0 :(得分:0)

当您“ new”时,需要“ delete”。在您称为“ delete[]”(而不是new)的某事上调用“ new[]”是未定义的行为。

在您的情况下,您在l=new Rec中叫Pila::push,但后来在delete [] l;中叫Pila::~Pila

最小的变化是

Pila::~Pila() {
    while(l!=0) {
        auto next = l->next;
        delete l;
        l = next;
    }
}

或者您可以Rec进行一组适当的复制和销毁操作,具体取决于您自己。