0

I would like to ask what could I do to get rid of this one memory leak from valgrind I have tried to clear crawler->next as this is where it points me. I didn't think this would be the case but tried. Not sure what else it could be.

typedef struct node
{
  int value;
  char word[LENGTH + 1];
  struct node* next;
}
node;

void demolish(node* num);
void insert(node* crap, int val);

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

//setting up a core pointer
node* head = (node*) malloc(sizeof(node));
memset(head,0,sizeof(node));
head->value = 0;
head->next = NULL;
//setting up a crawler 
node* crawler = head;
crawler->next = NULL;
int d = 0;
//go to the last element in the linked list
for(int i = 0; i < 10; i++){

    if(crawler != NULL)
    {

            //creating a new element
            crawler->next = malloc(sizeof(node));
            memset(crawler->next,0,sizeof(node));
            crawler->value = i;
            crawler = crawler ->next;
            crawler->next = NULL;
            //keep count of created nodes - displays nums from 1 - 10 correctly
            d++;
            //printf("\n%d", d);

    }
    //create some space in memory for a new element
    //creating new element
    if(crawler == NULL)
    {
        printf("out of memeory");
        return 1;
    }

}
  crawler -> next = crawler;
  crawler-> next  = NULL;

  crawler = head;
while(crawler->next != NULL)
{
    crawler = crawler->next;

}
crawler = head;        
for(int i = 0; i < 8;i++)
{
    if(crawler->next == NULL)
   {
       printf("there is no element 5");
   }
   else
   {
       crawler = crawler->next;

   }
}
//inserting a new node
insert(crawler, 90);


crawler = head;
for(int i = 0; i < 1;i++)
{
   if(crawler->next == NULL)
   {
       printf("there is no element 5");
   }
   else
   {
       crawler = crawler->next;
       // printf("\n%d",crawler->value);
   }
}
 demolish(crawler);


crawler = head;
for(int i = 0; i < 10;i++)
{
   if(crawler->next == NULL)
   {
       printf("there is no element 5");
   }
   else
   {
       crawler = crawler->next;
       printf("\n%d",crawler->value);
   }
}
 while(head!=NULL)
{
   node* temp = head;
   head = head->next;
   free(temp);


}

return 0;
}

 void demolish(node* num )
{
    node* temp1 = num->next;
    num->next = temp1->next;
}
void insert(node* crap, int val)
{
    node* insert = malloc(sizeof(node));
    memset(insert,0,sizeof(node));
    insert->value = val;
    insert->next = NULL;
    insert->next = crap ->next;
    crap->next = insert;

}

This is the error I am getting.

        HEAP SUMMARY:
       ==6470==     in use at exit: 64 bytes in 1 blocks
       ==6470==   total heap usage: 12 allocs, 11 frees, 768 bytes allocated
      ==6470== 
      ==6470== 64 bytes in 1 blocks are definitely lost in loss record 1 of 1
      ==6470==    at 0x4C2AB80: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
      ==6470==    by 0x40081A: main (main1.c:39)
      ==6470== 
      ==6470== LEAK SUMMARY:
      ==6470==    definitely lost: 64 bytes in 1 blocks
      ==6470==    indirectly lost: 0 bytes in 0 blocks
      ==6470==      possibly lost: 0 bytes in 0 blocks
      ==6470==    still reachable: 0 bytes in 0 blocks
      ==6470==         suppressed: 0 bytes in 0 blocks
      ==6470== 
      ==6470== For counts of detected and suppressed errors, rerun with: -v
      ==6470== ERROR SUMMARY: 1 errors from 1 contexts (suppressed: 0 from 0)
0

I never freed the temp1 node in demolish , this is why I was getting this memory leak. Hope this helps someone.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .