there are about 500,000 for the plist,
here is the whole code, at first i programmed with static array ,, then i wanted to change it to dynamic,,, this is why i got the error,,
i still get error
pleaseeeeeeee,, help me find it
the program basically creates trie and adds data to it.
In the main function i have careated 2 tries . First, create first trie , add elements then destroy it , then the second trie. The problem is the second time the trie is created , data added and destroyed i have problem... how did the two instances of trie get tangled?
Code:
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
typedef int Value;
typedef struct trieCDT *Trie;
typedef struct posPoolspace{
char **posList;
int listSize;
}posPool;
typedef struct trieCDT {
posPool *pool;
}trieCDT;
Trie createTrie(){
Trie trie;
trie = malloc(sizeof(struct trieCDT));
trie->pool= malloc(sizeof(struct posPoolspace));
trie->pool->listSize=2;
return trie;
}
void addTrie(Trie trie, char *keys,char *val){
Value value=0;
if(trie == NULL){
trie= createTrie();
}
char **plist=(trie->pool->posList);
plist =realloc(plist,(trie->pool->listSize+1)*sizeof(char*));
if(plist==NULL){
printf("Error realloc couldn't allocate heap space!!");
exit(0);
}
*(plist+trie->pool->listSize)=malloc(200);
strcpy(*(plist+trie->pool->listSize),val);
trie->pool->posList=plist;
value=trie->pool->listSize;
trie->pool->listSize++;
}
void destroyTrie(Trie trie){
posPool *pool =trie->pool;
int i=0;
char **plist=(trie->pool->posList);
for(i=0;i<pool->listSize;i++){
free(plist[i]);
}
free(trie->pool->posList);
free(trie->pool);
free(trie);
}
main(){
//Create First Trie
Trie trie;
trie = createTrie();
addTrie(trie, "abf", "56");
addTrie(trie, "abcde", "56");
addTrie(trie, "prameshh", "26");
addTrie(trie, "pramesh", "15");
destroyTrie(trie);
//Create Second Trie
Trie trie2;
trie2 = createTrie();
addTrie(trie2, "abf", "56");
addTrie(trie2, "abcde", "56");
addTrie(trie2, "prameshh", "26");
addTrie(trie2, "pramesh", "15");
destroyTrie(trie2);
}
error
*** glibc detected *** ./trie: free(): invalid pointer: 0xb7f593a0 ***
======= Backtrace: =========
/lib/tls/i686/cmov/libc.so.6[0xb7e6d454]
/lib/tls/i686/cmov/libc.so.6(cfree+0x96)[0xb7e6f4b6]
./trie[0x80488ab]
./trie[0x8048b91]
/lib/tls/i686/cmov/libc.so.6(__libc_start_main+0xe5)[0xb7e14685]
./trie[0x80484a1]
======= Memory map: ========
08048000-08049000 r-xp 00000000 08:07 7215635 /home/praka