I am trying to create a function that deletes an entire list, but I keep getting an error. Everything works excepting cleaner() function.
#include <stdio.h>
#include <stdlib.h>
struct node{
    int n;
    struct node *next;
};
typedef struct node NOD;
NOD *create_first_node(int i)
{
    NOD *q;
    q=(NOD*)malloc(sizeof(NOD*));
    q->n=i;
    q->next=NULL;
    return q;
}
NOD * add_to(NOD *x)
{
    NOD *q;
    q=(NOD*)malloc(sizeof(NOD*));
    q->n=rand();
    x->next=q;
    q->next=NULL;
    return q;
}
void show_list(NOD *p)
{
    printf("root");
    while(p->next){
        printf(" -> %d",p->n);
        p=p->next;
    }
    printf("\n");
}
void cleaner(NOD *p)
{
    NOD *r;
    while(p)
    {
        r=p;
        p=r->next;
        free(r);
        r=NULL;
    }
}
int main()
{
    int i;
    NOD *root,*c,*r;
    root=create_first_node(1);
    c=r=root;
    c=add_to(root);
    for(i=0;i<10;i++)
    {
        r=c;
        c=add_to(r);
    }
    show_list(root);
    //cleaner(root);
    system("pause");
    return 0;
}
NetBeans:
Signal received: SIGTRAP (?) with sigcode ? (?) From process: ? For program list, pid -1
You may discard the signal or forward it and you may continue or pause the process To control which signals are caught or ignored use Debug->Dbx Configure
Visual Studio:
Debug Error!
HEAP CORRUPTION DETECTED: after Normal block (#57) at 0x00393230 CRC detected that the application wrote to memory after end of heap buffer.
(I get this error with #57,#58,...,#68 each time cleaner() is trying to free a list item)
 
     
     
    