0

So I've got all the functionality down for dictionary.c and there are no leaks. I'm still getting 1 error in valgrind.

==8654== Conditional jump or move depends on uninitialised value(s) ==8654== at 0x4C2E1D1: strcpy (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so) ==8654== by 0x40143B: load (dictionary.c:106) ==8654== by 0x400A4D: main (speller.c:45)

Here's the code.

         while(!feof(dptr))
         {
        // allocates enough memory for a new node
        node* new_node = malloc(sizeof(node));

        //checks if there is enough memory
        if(new_node == NULL)
        {
            printf("Not enough free memory\n");
            return false;
        }
        fscanf(dptr, "%s", new_node -> word);
        strcpy(new_word, new_node -> word);

        //updates word count until the end of the file is reached
        if(!feof(dptr))
            wordcounter++;

        unsigned int i = hash_it(new_word);
        new_node -> next = hashtable[i];
        hashtable[i] = new_node;

    }

From searching on here I understand that this is because I haven't initialized new_node -> word in strcpy and my hash function. I thought it was initialized in this line.

     node* new_node = malloc(sizeof(node));

Do I have to do something like:

     new_node -> word = NULL;

Thanks for your time.

0

While the pointer new_node will be initialized with the address of the allocated memory, malloc does NOT initialize the allocated memory. It only allocates memory. That means that anything in the structure contains whatever random data which was in the memory that was allocated. See calloc() instead.

It's also a good practice to always explicitly allocate anything that you malloc.

| improve this answer | |
  • Thanks. Everything's working now. – user11010 Apr 13 '16 at 5:58

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .