1
votes
void *insert_rear_node(void *arg)
{   
  int *argument=(int *)arg;
  int value=*argument;
  //Assume that allocation is happening fine (Just like malloc , it is a custom heap allocator)

  struct Node *head=(struct Node *) RTallocate(RTpointers, sizeof(struct Node));
  struct Node *temp;
  setf_init(temp,head);

  while(value>0)
    {
      if(temp==NULL)
        {
          RTwrite_barrier(&(temp), new_node(value,NULL));
          RTwrite_barrier(&(head),temp);
          printf("Inserted %d into the list\n",head->data);
        }
      else
        {
          RTwrite_barrier(&(temp),head);
          while(temp->next!=NULL)
            {
              RTwrite_barrier(&(temp),temp->next);
            }
          RTwrite_barrier(&(temp->next),new_node(value,NULL));
          printf("Inserted %d into the list\n",temp->next->data);
        }
      value=value-1;

    }
  free(head);

}

int main(int argc, char *argv[])
{                                                                                                                                                                                                                              
  long heap_size = (1L << 28);
  long static_size = (1L << 26);
  printf("heap_size is %ld\n", heap_size);
  RTinit_heap(heap_size, static_size, 0);                                                                                                                                                                                                                 
  pthread_t thread1;                                                                                                                                                                                                                                   
  int limit=1000;
  RTpthread_create(&thread1,NULL,insert_rear_node,(void *)&limit);
}

Assume that RTallocate and RTwrite_barrier are 2 custom functions which work perfectly fine. RTallocate - allocates memory on the heap RTwrite_barrier is equivalent to an assignemnt statement.

This program simply insert nodes into a linkedlist. Then attempts to delete the head.

I get this error:
Error in `/home/test/RT-Test/InMul': double free or corruption (out): 0x00007fffe3465010
warning: Corrupted shared library list: 0x7ffea4000920 != 0x7ffff7ffd9d8
======= Backtrace: =========
/lib/x86_64-linux-gnu/libc.so.6(+0x777e5)[0x7ffff77f97e5]
/lib/x86_64-linux-gnu/libc.so.6(+0x7fe0a)[0x7ffff7801e0a]
/lib/x86_64-linux-gnu/libc.so.6(cfree+0x4c)[0x7ffff780598c]
/home/test/RT-Test/InMul[0x400b98]
/lib/x86_64-linux-gnu/librtgc.so(rtalloc_start_thread+0x1ef)[0x7ffff7b4fa2c] /lib/x86_64-linux-gnu/libpthread.so.0(+0x76ba)[0x7ffff756c6ba] /lib/x86_64-linux-gnu/libc.so.6(clone+0x6d)[0x7ffff788882d]


I am freeing the head only once. Why am I facing this issue?
0x00007fffe3465010 is the address of the head.

2
Why, by the great god of C, beginner always write linked list ?Stargateur
Don't use free when you didn't use malloc, use the equivalent to RTallocate.Tony J
"Assume that RTallocate and RTwrite_barrier are 2 custom functions which work perfectly fine" That's a big assumption.dbush
@Tony J - I will be shocked if that is not the answer.hoodaticus
@Stargateur: Yes and using Pytghon would remove all UB and pointer related questions, too. Oh, there is one problem: One does not learn how to use pointers with that approach ;-). I have no idea about other's courses, but of course arrays and pointer basics should be treated before lists (I think I covered that in my former comment with "if the basics are taught and understood well" - that seems to be the actual crux.too honest for this site

2 Answers

1
votes

You can't use free() when you didn't use malloc() for head. Use the free equivalent to RTallocate.

1
votes

Some piece of code you didn't show us, quite probably nowhere near the code you think is the problem, has corrupted malloc's internal bookkeeping data.

Run your program under valgrind and fix the very first invalid operation that it reports. Repeat until valgrind has no more complaints. Now your program should work. If you do not understand valgrind's output, edit the first 20 or so lines of it into your question and we may be able to explain.