1

So I'm building a side code to print out results before implementing onto pset5 dictionary.c

I'm getting a seg fault stating i did not initialise the values, can someone help me out? Do i actually need to add '\0' at the end of the word? Do i need to deal with Const Char vs Char...? I used Strcpy but seem unnecessary?..

    /**
 * Create a data structure of node, put data in it, and print data out
 */

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <cs50.h>
#include <ctype.h>  //to lower()
#include "hashtableTestNode.h"
#define HASHTABLESIZE 10

bool check(const char *word);   //prototype

int main(int argc, char* argv[])
{
    node* hashtable[HASHTABLESIZE];
    char DicWord[LENGTH];
    // TODO
    //  * 1. create linked list (Allocate memory to store node) [Done in .h file]
    //  * 2. create hashtable
    //  * 3. populate hashtable with words found in dictionary
    if(argc != 2)
    {
        printf("./hashtableTest.1 small\n");
    }
        //open dictionary small/large [cont here.]
        FILE* fp = fopen(argv[1], "r");

        while(fscanf(fp, "%s", DicWord) != EOF)    //this will scan 'fp' for a 'string', and put that string into variable 'word'
        {

    //  * 4. for every word scanned, malloc a node for the word     (new_node will be forgotten and inserted into linklist at end of while() function)
            node *new_node = malloc(sizeof(node));
            if(new_node == NULL)
                {
                    free(new_node);
                    return 1;
                }
    //  * 5. copy word into node
            strcpy(new_node->word, DicWord);

    //  * 6. at this point we have a new node with values scanned in from dictionary[no action]
    //  * 7. insert that node into a linked list (don't let go of linked list until, we are sure it's safe. See 9:24)
    //  * 8. use hash function to know which word will be put into which link list (which array number or in this case, hashtable[hashfunction(word)]
            new_node->next = hashtable[hashFunction(new_node->word)];
            hashtable[hashFunction(new_node->word)] = new_node;

            //how to print a link list from a hashtable?
            printf("%s\n", new_node->word);
            //printf("%s\n", node2->word);

            if(check(new_node->word))
            {
                printf("true! and spelling correct\n");
            }
            else
            {
                printf("false and spelling wrong\n");
            }

        }

           fclose(fp);

}

bool check(const char *word)    //remember to debug on 17 Feb 2017!
{   
    // TODO
    // Assume, case insensitive
    // Assume, sting passed in are only alphabets or apostrophes

    // ZYMALA's Solution:

    //char* word = argv[1];   //assign varible to char word <-- could this be a shadow variable, if so try deleting the statement

    //  convert all case to small cap first<-- this is not from Zymala (it is from: http://cs50.stackexchange.com/questions/19991/speller-c-appears-to-be-working-but-is-case-sensitive-cant-recognize-capitol-l)
    node* hashtable[HASHTABLESIZE];

    int len = strlen(word + 1);
    char* word2 = malloc(sizeof(len));
    //char word2[strlen(word)];
    strcpy(word2, word);


    int i = 0;
    while(i < strlen(word2))    //alternatively, while(i < strlen(word)).. or.. word[i] != NULL; could work too
    {
        if(word2[i] > 'A' && word2[i] < 'Z')  //if characters are between 'A' and 'Z'
        {
            tolower(word2[i]);
        }

        i++;
    }
    word2[len] = '\0';
    //  if bucket exisit
    //  which bucket will the word be in? 

    //  //if the word exist, it can be found in the hashtable
    //  search that link list (strcasecmp)

    //  Traverse linked list:
    //  question: node* new_node = head?..     //hashtable[bucket]node* new_node = head...?



    int index = hashFunction(word2);
    node* cursor = NULL;

    cursor = hashtable[index];
                if(cursor == NULL)
                {
                    printf("cursor is NULL after line 112!\n");
                }
                    printf("hash function = %i\n", hashFunction(word2));

    //  Assuming true: node* cursor = head;
    //node* cursor = head;


     while(cursor != NULL)  //while not at the end of link list
     {
         //do something (compare strings)
         if((strcmp(word2, cursor->word)) != 0)    //if the words are not the same
         {
             //if not found, move to next <-- added by erik
             cursor = cursor-> next;

         }

         else
         {
            return true;    //if string compare results in similar words, return true
         }
     }

     return false;
                                      printf("did we even get to this point?\n");

     //free(word2);
     //free(word);
}

Valgrid error:

    valgrind ./hashtableTest.1 small
==4997== Memcheck, a memory error detector
==4997== Copyright (C) 2002-2013, and GNU GPL'd, by Julian Seward et al.
==4997== Using Valgrind-3.10.0.SVN and LibVEX; rerun with -h for copyright info
==4997== Command: ./hashtableTest.1 small
==4997== 
cat
hash function = 2
==4997== Conditional jump or move depends on uninitialised value(s)
==4997==    at 0x42E2F2: check (hashtableTest.1.c:117)
==4997==    by 0x42DE32: main (hashtableTest.1.c:53)
==4997==  Uninitialised value was created by a stack allocation
==4997==    at 0x42DEA0: check (hashtableTest.1.c:69)
==4997== 
==4997== Conditional jump or move depends on uninitialised value(s)
==4997==    at 0x42E363: check (hashtableTest.1.c:120)
==4997==    by 0x42DE32: main (hashtableTest.1.c:53)
==4997==  Uninitialised value was created by a stack allocation
==4997==    at 0x42DEA0: check (hashtableTest.1.c:69)
==4997== 
==4997== Conditional jump or move depends on uninitialised value(s)
==4997==    at 0x42E38F: check (hashtableTest.1.c:120)
==4997==    by 0x42DE32: main (hashtableTest.1.c:53)
==4997==  Uninitialised value was created by a stack allocation
==4997==    at 0x42DEA0: check (hashtableTest.1.c:69)
==4997== 
==4997== Use of uninitialised value of size 8
==4997==    at 0x4C2F1B4: strcmp (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==4997==    by 0x42E3B9: check (hashtableTest.1.c:120)
==4997==    by 0x42DE32: main (hashtableTest.1.c:53)
==4997==  Uninitialised value was created by a stack allocation
==4997==    at 0x42DEA0: check (hashtableTest.1.c:69)
==4997== 
==4997== Conditional jump or move depends on uninitialised value(s)
==4997==    at 0x42E407: check (hashtableTest.1.c:123)
==4997==    by 0x42DE32: main (hashtableTest.1.c:53)
==4997==  Uninitialised value was created by a stack allocation
==4997==    at 0x42DEA0: check (hashtableTest.1.c:69)
==4997== 
==4997== Conditional jump or move depends on uninitialised value(s)
==4997==    at 0x42E472: check (hashtableTest.1.c:123)
==4997==    by 0x42DE32: main (hashtableTest.1.c:53)
==4997==  Uninitialised value was created by a stack allocation
==4997==    at 0x42DEA0: check (hashtableTest.1.c:69)
==4997== 
==4997== Use of uninitialised value of size 8
==4997==    at 0x42E498: check (hashtableTest.1.c:123)
==4997==    by 0x42DE32: main (hashtableTest.1.c:53)
==4997==  Uninitialised value was created by a stack allocation
==4997==    at 0x42DEA0: check (hashtableTest.1.c:69)
==4997== 
==4997== Invalid read of size 1
==4997==    at 0x4C2F1B4: strcmp (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==4997==    by 0x42E3B9: check (hashtableTest.1.c:120)
==4997==    by 0x42DE32: main (hashtableTest.1.c:53)
==4997==  Address 0x2d48550063c06fb8 is not stack'd, malloc'd or (recently) free'd
==4997== 
==4997== 
==4997== Process terminating with default action of signal 11 (SIGSEGV)
==4997==  General Protection Fault
==4997==    at 0x4C2F1B4: strcmp (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==4997==    by 0x42E3B9: check (hashtableTest.1.c:120)
==4997==    by 0x42DE32: main (hashtableTest.1.c:53)
==4997== 
==4997== HEAP SUMMARY:
==4997==     in use at exit: 628 bytes in 3 blocks
==4997==   total heap usage: 3 allocs, 0 frees, 628 bytes allocated
==4997== 
==4997== LEAK SUMMARY:
==4997==    definitely lost: 0 bytes in 0 blocks
==4997==    indirectly lost: 0 bytes in 0 blocks
==4997==      possibly lost: 0 bytes in 0 blocks
==4997==    still reachable: 628 bytes in 3 blocks
==4997==         suppressed: 0 bytes in 0 blocks
==4997== Reachable blocks (those to which a pointer was found) are not shown.
==4997== To see them, rerun with: --leak-check=full --show-leak-kinds=all
==4997== 
==4997== For counts of detected and suppressed errors, rerun with: -v
==4997== ERROR SUMMARY: 8 errors from 8 contexts (suppressed: 0 from 0)
Segmentation fault
1

Looking at your code, strcmp and strcpy (at line 106 and 94/94 in your code) it could quite well be char word2[strlen(word)]; that is causing a chain reaction of errors. Remember that when you are declaring a char array (or mallocing some memory for a "string"), you need strlen(origionalstring + 1);. It seems that without +1 part, it will try to write out of memory (when strcpy tries to put the word into the new string, it will try and write the \0) and later, when you are comparing, strcmp will have no clue where to stop comparing, which leaves it trying to read something which is out of the memory your program got allocated by the system. I hope this helps!

9
  • Definitely help! Though, actually, char word2[strlen(word)]; is actually already commented out. Also, even after i removed the +1 i still get a seg fault.. Could it be my earlier code that is cause it? would you mind taking a look at them too? – nvs0000 Feb 20 '17 at 17:59
  • No problem, I also just realized I made a stupid mistake in my post, you need the +1 part, but my post accidentally made it sound like that was the bad part :P. Anyway, would you be able to give me an output log of valgrind (if required, edit your origional post or use hastebin). This generally gives an insight into where the error is caused :) I do have to say, I have barely completed this weeks lecture, but I'll try my best to decipher it :) – HowToCompute Feb 20 '17 at 18:07
  • Absolutely! will add the +1 back and show the valgrind error. I heard most people drop out at this Pset. Trying my best to carry on and looking forward to Java script! This course is hard! But so glad to have such a strong community! – nvs0000 Feb 20 '17 at 18:09
  • 1) Valgrind says invalid read during my 'strcmp'. That's why i kept wondering if the error has to do with the length of my word. 2) There's also a bunch of 'Conditional jump or move depends on uninitialised value(s)', which I'm not sure how to deal with/solve the error... – nvs0000 Feb 20 '17 at 18:20
  • I won't be the first to agree that this problem set is hard, tho in my opinion pset4 is somehow harder :P Anyway, it seems that somehow cursor is uni-initialized, which seems really odd as you check to see if it is eqaul to NULL. Furthermore, I have no clue weather or not the space in cursor = cursor-> next; is legal, it probably is, but long time scince I used ->'s. I really wish I could help more, but I am still not at this problem set :P – HowToCompute Feb 20 '17 at 18:20

You must log in to answer this question.

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