LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   problem: *** glibc detected *** free(): invalid pointer: (https://www.linuxquestions.org/questions/programming-9/problem-%2A%2A%2A-glibc-detected-%2A%2A%2A-free-invalid-pointer-719192/)

shashank929 04-15-2009 12:37 AM

problem: *** glibc detected *** free(): invalid pointer:
 
I'm using a structure which is

struct bstnode{
unsigned long int byte_size;
struct node *list;
struct bstnode *lchild;
struct bstnode *rchild;
}*filetree;

struct node{
char * filepath;
char * checksum;
struct node *next;
};

bstnode is the structure for binary search tree which has a long int field and a linked list i.e. node

and I'm using the following function to allocate memory for the tree

struct bstnode * maketree(unsigned long int byte_size,char *path){
struct bstnode *p;
struct node *q,*x;
p=(struct bstnode *)malloc(sizeof(struct bstnode *));
p->list=NULL;
p->byte_size=byte_size;
q=(struct node *)malloc(sizeof(struct node *));
q->filepath=(char *)malloc(sizeof(char[strlen(path)+1]));
strcpy(q->filepath,path);
q->checksum=(char *)malloc(sizeof(char[strlen(strSHA2(path))+1]));
strcpy(q->checksum,strSHA2(path));
q->next=NULL;
p->list=q;
p->lchild=NULL;
p->rchild=NULL;
return (p);
}


since the filepath and checksum fields are for strings so I'm allocating memory for them also using malloc


now when it comes to free the complete tree, I'm using the following function to free the entire tree

void startfree(struct bstnode *temp){
struct node *listentry,*saved_next;
while(temp!=NULL){
startfree(temp->lchild);
//printf("going right\n");
startfree(temp->rchild);
listentry=temp->list;
do{
if(listentry->next!=NULL){
saved_next=listentry->next;
}
else
saved_next=NULL;
free(listentry->filepath);
listentry->filepath=NULL;
free(listentry->checksum);
listentry->checksum=NULL;
free(listentry);
listentry=NULL;
listentry=saved_next;
}while(listentry!=NULL);
printf("cleared the list\n");
temp->list=NULL;
free(temp);
temp=NULL;
printf("free bst node\n");
}
}

so It is freeing successfully the filepath and checksum field but when it comes to

free(listentry);

program gets aborted saying:

*** glibc detected *** ./a.out: free(): invalid pointer: 0x096233f0 ***
======= Backtrace: =========
/lib/libc.so.6[0x45937f7d]
/lib/libc.so.6(cfree+0x90)[0x4593b5d0]
./a.out[0x8048fd1]
./a.out[0x8048f6c]
./a.out[0x8048f6c]
./a.out[0x8048f7a]
./a.out[0x8048f6c]
./a.out[0x8048f7a]
./a.out[0x8048f6c]
./a.out[0x8048f6c]
./a.out[0x8048a02]
/lib/libc.so.6(__libc_start_main+0xdc)[0x458e7dec]
./a.out[0x8048641]
======= Memory map: ========
00a12000-00a13000 r-xp 00a12000 00:00 0 [vdso]
08048000-0804d000 r-xp 00000000 08:03 137738 /home/iiit/a.out
0804d000-0804e000 rwxp 00004000 08:03 137738 /home/iiit/a.out
09622000-09644000 rwxp 09622000 00:00 0
458b5000-458ce000 r-xp 00000000 08:03 3126510 /lib/ld-2.5.so
458ce000-458cf000 r-xp 00018000 08:03 3126510 /lib/ld-2.5.so
458cf000-458d0000 rwxp 00019000 08:03 3126510 /lib/ld-2.5.so
458d2000-45a09000 r-xp 00000000 08:03 3126512 /lib/libc-2.5.so
45a09000-45a0b000 r-xp 00137000 08:03 3126512 /lib/libc-2.5.so
45a0b000-45a0c000 rwxp 00139000 08:03 3126512 /lib/libc-2.5.so
45a0c000-45a0f000 rwxp 45a0c000 00:00 0
4695c000-46967000 r-xp 00000000 08:03 3126516 /lib/libgcc_s-4.1.1-20070105.so.1
46967000-46968000 rwxp 0000a000 08:03 3126516 /lib/libgcc_s-4.1.1-20070105.so.1
b7e00000-b7e21000 rw-p b7e00000 00:00 0
b7e21000-b7f00000 ---p b7e21000 00:00 0
b7f22000-b7f23000 rw-p b7f22000 00:00 0
b7f2b000-b7f2d000 rw-p b7f2b000 00:00 0
bff2e000-bff44000 rw-p bff2e000 00:00 0 [stack]
Aborted



someone please tell what the problem is with my code....TIA

Fidori 04-15-2009 02:11 PM

In line
Code:

p=(struct bstnode *)malloc(sizeof(struct bstnode *));
you are allocating memory enough to hold a pointer to struct bstnode, but you probably wanted to allocate memory for the structure itself. Like this:
Code:

p=(struct bstnode *)malloc(sizeof(struct bstnode));

shashank929 04-16-2009 02:09 AM

Thanks
 
thanks a lot for that.... i guess i did that silly mistake...


All times are GMT -5. The time now is 01:55 PM.