Hi guys,

I am implementing a singly linked list. The thing is whenever my str_temp goes above 12-15 elements, i get this error

malloc: *** error for object 0x1099008b8: incorrect checksum for freed object - object was probably modified after being freed.
*** set a breakpoint in malloc_error_break to debug
Run Command: line 1: 7322 Abort trap: 6 ./"$2"

The error is happening in AppendNode method. Can you please tell me where I might be going wrong.
Code snippet is given below. Please let me know if you need more info.

class node{
    public:    
    int data;
    class node* next;
    
};

class IO{
    public:
    int numReq;
    class node *head;
    class node *tail;
    
    IO(int);
    void makeList(int*);
    void makeSorted(int*);    
    void printList();
    void SortedInsert(class node*&,int); 
    void InsertSort();
    void AppendNode(int);
       
};

void IO::makeList(int str_temp[]){
    int i;
    cout<<"NUMREQ "<<numReq<<endl;
    for (i=0;i<numReq;i++) {
    cout<<str_temp[i]<<endl;
        AppendNode(str_temp[i]);
    }
}


void IO::AppendNode(int num) {
   class node* current = head;
   class node* newNode;
   newNode = new node();
   newNode->data = num;
   newNode->next = NULL;

   // special case for length 0
   if (current == NULL) {
      head = newNode;
   }
   else {
      // Locate the last node
      while (current->next != NULL) {
         current = current->next;
      }
      current->next = newNode;
   }

}

i didnot get that error so please
check that head is set to NULL before call the build
if not solved please upload the ehole project cause there might be other problem

I am initializing it to null.

IO::IO(int num):numReq(num),head(NULL),tail(NULL){
    }
This article has been dead for over six months. Start a new discussion instead.