Giovanni Saltini
Giovanni Saltini

Reputation: 1

Segmentation Fault (core dumped) linked list

Hey guys I'm trying to run this code but I get segmentation fault when the compiler gets to the class functions.

This is the main function:

int main (int argc, char* argv[]){

    cout<<"\t1.Add Program\n";
    cout<<"\t2.Kill Program\n";
    cout<<"\t3.Fragmentation\n";
    cout<<"\t4.Print Memory\n";
    cout<<"\t5.Exit"<<endl;

    LinkedList Memory;
    Memory.createMemory();   (I get the segmentation error on this line)


    int choice;
    cin>>choice;
    cout<<"choice - "<<choice<<endl;

    if  (choice==1){
        string programName;
        cin>>programName;
        cout<<"Program name - "<<programName<<endl;

        int size;
        cin>>size;
        cout<<"Program size (KB) - "<<size<<endl;

        int numpages;
        if (size%4==0) numpages=size/4;
        if (size%4!=0) numpages=size/4+1;

        Memory.addProgram(numpages, programName);
        return 0;
    }

This is the class

 class LinkedList{
 private:
     struct node{
         string name;
         node *next;
      };
 public:
      void createMemory();
      void addProgram(int val, string s);
      void killProgram(string s1);
      void print();
      void fragmentation();
      LinkedList(){head=NULL;};
  };

And these are two of the class functions

 void LinkedList::createMemory(){
    int i=0;
    node* temp;
    temp = new node;
    while(i<32){
        temp->name="Free";
        temp=temp->next;
        i++;
    }
    };



    void LinkedList::addProgram(int val, string s){
    int i=0;
    node* temp;
    temp=new node;
    while(temp->name!="Free")
        temp=temp->next;

    while(temp->name=="Free"){
        while (i<val){
            temp->name=s;
            temp=temp->next;
            i++;
        }
    }
    cout<<"Program "<<s<<" added successfully: "<<val<<" page(s) used."<<endl;
    };

The other functions in the class are similar to these two so they're all gonna have the same error. The main function runs correctly, but when I call the class functions in the main i get the segmentation fault.

Upvotes: 0

Views: 169

Answers (3)

Jacek Cz
Jacek Cz

Reputation: 1906

while(i<32){
        temp->name="Free";
        temp=temp->next;
        i++;
    }

In this snippet, you use null or uninitialized temp->next

Maybe there are more subtle errors in your code. Use a debugger.

Tip always to keep in keep in mind: in constructor initialize all members, not only selected. In my code, I use constructors for struct too (some people advice otherwise)

Upvotes: 1

venki
venki

Reputation: 45

'createMemory()' Method that initializes the List got problem with memory allocation. Only the first node got memory allocated. You are re-assigning 'temp' with 'temp->next' which does not have memory allocated to it and accessing 'temp->name' which would cause the 'Segmentation fault'. If you are creating multiple nodes in an iterative fashion, you must allocate memory for each of the nodes in the loop. Use this stanford link as a reference to learn how to initialize Linked List: http://cslibrary.stanford.edu/103/LinkedListBasics.pdf

Upvotes: 0

Edd
Edd

Reputation: 1370

In LinkedList::createMemory and LinkedList::addProgram, you are creating a new node in the scope of the function but you are not assigning such new node to a class variable. Therefore, when you exit your function, the pointer to the resource you created is lost and:

1) you leaked memory because you haven't called delete on the pointer 2) your class doesn't have any nodes

add a

node *_root; 

variable to your LinkedList class and assign to it.

Now with this being said here's a couple of tips:

  • Don't use new like that, it is very easy to leak memory. Use std::unique_ptr so that resources are automatically cleared.

  • Use std::list if you want to have a LinkedList.

Upvotes: 0

Related Questions