Segmentation fault (core dump) C ++: dynamic memory

 template<class T>
   void SimpleList<T>::in_node_l(void){
       Node* fin;
       fin=new Node;
       (*last).next=fin;
       last=fin;        
       size++;
}

      

I am getting a segmentation fault (core dumped) when the member_node_l function is called by the push function

 template<class T>
   void stack<T>::push(T var){
       SimpleList<T>::in_node_l();
       (*(this->last)).data=var;
       position++;
       return;
  } 

      

stack is a derived class:

    class stack : public SimpleList<T>

      

Node is declared as

template<class T>
struct SimpleList<T>::Node{
  T data;
  Node* next;
 };

      

if i call in_node_l in main but i dont get any errors.

constructor for the class stack:

template<class T>

stack<T>::stack(int s,string n)
:SimpleList<T>(s,n)
{
  position=1;
}

      

Constructor

for SimpleList is set

template<class T>
SimpleList<T>::SimpleList(int s,string n){
int i;
//name= input name
Node *prev,*current;
size=s;
name=n;
if(s==1){
    current=new Node;
    first=current;
    last=current;
    cout<<"SIMPLELIST CONSTRUCTUR"<<endl;
}
else{
    for (i=1;i<s;i++){
        current=new Node;
        if(i==1){
            first=current;
        }
        else{
            (*prev).next=current;
            if(i==s-1){
                last=current;
                if(last=(*prev).next){
                    cout<<"SIMPLELIST CONSTRUCTUR2"<<endl;
                }       
            }
        }
        prev=current;
    }
}

      

}

+3


source to share





All Articles