erors from Valgrind ,,

This is a discussion on erors from Valgrind ,, within the C Programming forums, part of the General Programming Boards category; Hi every one, i just started c programming... my program(some parts i got it from some site) compiles fine ,, ...

  1. #1
    Registered User
    Join Date
    Dec 2008
    Posts
    10

    Unhappy 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

  2. #2
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    sizeof (Trie) == sizeof( void*) != sizeof (struct trieCDT)

    you are mallocing the wrong size
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  3. #3
    Registered User
    Join Date
    Dec 2008
    Posts
    10

    Lightbulb

    thanks , its ok now~~

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Makefiles, GCC's -ggdb option, and Valgrind
    By Jesdisciple in forum C Programming
    Replies: 5
    Last Post: 03-14-2009, 04:25 PM
  2. Bug hunting with Valgrind
    By Smjert in forum C Programming
    Replies: 8
    Last Post: 11-06-2008, 03:21 AM
  3. Profiler Valgrind
    By afflictedd2 in forum C++ Programming
    Replies: 4
    Last Post: 07-18-2008, 09:38 AM
  4. Replies: 1
    Last Post: 12-01-2007, 01:06 AM
  5. Is valgrind always right?
    By g4j31a5 in forum Linux Programming
    Replies: 3
    Last Post: 07-16-2007, 10:39 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21