-
erors from Valgrind ,,
Hi every one,
i just started c programming... my program(some parts i got it from some site) compiles fine ,, but when i check the program with valgrind ,,, iget the errors..
my code
trie.h
-
Code:
typedef int Value;
typedef struct trieCDT *Trie;
trie.c
Code:
#define pool_size 1024*1024
typedef struct trieNodeTag{
char key;
Value value;
struct trieNodeTag *next, *children;
}trieNode;
typedef struct posPoolspace{
char *posList[pool_size];
int listSize;
}posPool;
typedef struct trieCDT {
trieNode *root;
posPool *pool;
}trieCDT;
Trie createTrie(){
Trie trie;
trie = (Trie)malloc(sizeof(Trie));
trie->root= NULL;
trie->pool= (posPool*)malloc(sizeof(posPool));
trie->pool->listSize=2;
return trie;
}
void destroyTrie(Trie trie){
trieNode *level = trie->root;
destroyNodes(level);
posPool *pool =trie->pool;
int i=0;
for(i=0;i<pool->listSize;i++)
free(pool->posList[i]);
free(trie->pool);
free(trie);
}
createTrie is called from some other function i have : loadDict .
with the valgrind i get many errors . errors:
==14965== 1 errors in context 3 of 20:
==14965== Invalid read of size 4
==14965== at 0x8049FF9: createTrie (in /home/prakash/Desktop/Work In Progress/LDC_Modifier/Modifier)
==14965== by 0x8049F8C: loadDict (in /home/prakash/Desktop/Work In Progress/LDC_Modifier/Modifier)
==14965== by 0x80494F4: main (in /home/prakash/Desktop/Work In Progress/LDC_Modifier/Modifier)
==14965== Address 0x419b074 is 0 bytes after a block of size 4 alloc'd
==14965== at 0x4025D2E: malloc (vg_replace_malloc.c:207)
==14965== by 0x8049FD5: createTrie (in /home/prakash/Desktop/Work In Progress/LDC_Modifier/Modifier)
==14965== by 0x8049F8C: loadDict (in /home/prakash/Desktop/Work In Progress/LDC_Modifier/Modifier)
==14965== by 0x80494F4: main (in /home/prakash/Desktop/Work In Progress/LDC_Modifier/Modifier)
==14965==
==14965== 1 errors in context 4 of 20:
==14965== Invalid write of size 4
==14965== at 0x8049FF3: createTrie (in /home/prakash/Desktop/Work In Progress/LDC_Modifier/Modifier)
==14965== by 0x8049F8C: loadDict (in /home/prakash/Desktop/Work In Progress/LDC_Modifier/Modifier)
==14965== by 0x80494F4: main (in /home/prakash/Desktop/Work In Progress/LDC_Modifier/Modifier)
==14965== Address 0x419b074 is 0 bytes after a block of size 4 alloc'd
==14965== at 0x4025D2E: malloc (vg_replace_malloc.c:207)
==14965== by 0x8049FD5: createTrie (in /home/prakash/Desktop/Work In Progress/LDC_Modifier/Modifier)
==14965== by 0x8049F8C: loadDict (in /home/prakash/Desktop/Work In Progress/LDC_Modifier/Modifier)
==14965== by 0x80494F4: main (in /home/prakash/Desktop/Work In Progress/LDC_Modifier/Modifier)
..
...
...
==14965== 1421209 errors in context 20 of 20:
==14965== Invalid read of size 4
==14965== at 0x804A2F4: destroyNodes (in /home/prakash/Desktop/Work In Progress/LDC_Modifier/Modifier)
==14965== by 0x804A2E5: destroyNodes (in /home/prakash/Desktop/Work In Progress/LDC_Modifier/Modifier)
==14965== by 0x804A2E5: destroyNodes (in /home/prakash/Desktop/Work In Progress/LDC_Modifier/Modifier)
==14965== by 0x804A2E5: destroyNodes (in /home/prakash/Desktop/Work In Progress/LDC_Modifier/Modifier)
==14965== by 0x804A2E5: destroyNodes (in /home/prakash/Desktop/Work In Progress/LDC_Modifier/Modifier)
==14965== by 0x804A31A: destroyTrie (in /home/prakash/Desktop/Work In Progress/LDC_Modifier/Modifier)
==14965== by 0x8049656: main (in /home/prakash/Desktop/Work In Progress/LDC_Modifier/Modifier)
==14965== Address 0x5259670 is 8 bytes inside a block of size 16 free'd
==14965== at 0x4024B4A: free (vg_replace_malloc.c:323)
==14965== by 0x804A2F0: destroyNodes (in /home/prakash/Desktop/Work In Progress/LDC_Modifier/Modifier)
==14965== by 0x804A2E5: destroyNodes (in /home/prakash/Desktop/Work In Progress/LDC_Modifier/Modifier)
==14965== by 0x804A2E5: destroyNodes (in /home/prakash/Desktop/Work In Progress/LDC_Modifier/Modifier)
==14965== by 0x804A2E5: destroyNodes (in /home/prakash/Desktop/Work In Progress/LDC_Modifier/Modifier)
==14965== by 0x804A2E5: destroyNodes (in /home/prakash/Desktop/Work In Progress/LDC_Modifier/Modifier)
==14965== by 0x804A31A: destroyTrie (in /home/prakash/Desktop/Work In Progress/LDC_Modifier/Modifier)
==14965== by 0x8049656: main (in /home/prakash/Desktop/Work In Progress/LDC_Modifier/Modifier)
why ami getting these errors ??/ please help me find the solution..
thanks in advance~~
prakash
-
sizeof (Trie) == sizeof( void*) != sizeof (struct trieCDT)
you are mallocing the wrong size
-