LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 04-15-2009, 12:37 AM   #1
shashank929
LQ Newbie
 
Registered: Apr 2009
Posts: 10

Rep: Reputation: 0
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
 
Old 04-15-2009, 02:11 PM   #2
Fidori
LQ Newbie
 
Registered: Oct 2007
Location: Finland
Distribution: Slackware
Posts: 27

Rep: Reputation: 17
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));
 
Old 04-16-2009, 02:09 AM   #3
shashank929
LQ Newbie
 
Registered: Apr 2009
Posts: 10

Original Poster
Rep: Reputation: 0
Thanks

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


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Error during cimserver start : *** glibc detected *** realloc(): invalid pointer MaverickLikesMustang Linux - Software 0 02-14-2008 06:42 AM
glibc detected : Invalid Pointer Error DragonM15 Linux - Software 3 06-01-2007 12:43 AM
*** glibc detected *** free(): invalid pointer: 0x0804b094 *** lmvent Programming 4 02-03-2007 09:51 PM
Glibc free() invalid pointer pierre-luc Programming 3 11-13-2005 08:04 PM
OpenOffice.org *** glibc detected *** free(): invalid pointer: 0x41481c94 *** Artanicus Linux - Software 2 02-19-2005 07:04 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

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

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration