0

I'm having some problems with spell-checker(pset 6). I'm trying to debugg with gdb, but when I try to run ./spell with gdb the following happens:

Reading symbols from /home/jharvard/Dropbox/C/Harvard/pset6/speller...done. "/home/cs50/pset6/dictionaries/large" is not a core dump: File format not recognized

I'm fairly confident that my pseudocode has no logic mistakes, but I'm not sure if I understood how to read words from the dictionary (I don't understand that const char* declaration), so I'll post that part of my code here:

/code snipped per honor code/

EDIT: My code is segfaulting when trying to load the words from the dictionary. I'm not sure I understood how fscanf works. What I think it does is: it takes one word at a time from a file* and put it where you tell it to put it. It also, returns 1 if one argument was read and NULL when the end of the file was reached. Taking that into account is that I wrote this, but it doesnt work:

// Initialize hash table
node* hashtable[26];

// Open dictionary file
FILE* file = fopen(dictionary, "r");

// For every word in file
while (true)
{
    node* new_node = malloc(sizeof(node));
    if (fscanf(file, "%s", new_node -> word) == 1)
    {
        // Hash word of new node

        // Go to the linked list in that bucket

        // Check if list is empty and insert new node there

        // Check if new node belongs to the head of the linked list

        // Try insert new node at the middle or tail
        else
        {
            {
                // Avoid duplicates

                // Check for insertion at tail

                // check for insertion in middle

                // Check for insertion in middle

                // Space hasn't been found yet. Update predptr
            }
    }   
}

}

This is what outputs GDB.

    Breakpoint 1, main (argc=2, argv=0xbffff084) at dictionary_test.c:34
warning: Source file is more recent than executable.
34      FILE* file = fopen(argv[1], "r");
(gdb) n
39          node* new_node = malloc(sizeof(node));
(gdb) n
40          if (fscanf(file, "%s", new_node -> word) == 1)
(gdb) n

Program received signal SIGSEGV, Segmentation fault.
__isoc99_fscanf (stream=0x0, format=0x80487e6 "%s") at isoc99_fscanf.c:30
30    _IO_acquire_lock_clear_flags2 (stream);
(gdb) n

Program terminated with signal SIGSEGV, Segmentation fault.
The program no longer exists.
(gdb) l
25  __isoc99_fscanf (FILE *stream, const char *format, ...)
26  {
27    va_list arg;
28    int done;
29  
30    _IO_acquire_lock_clear_flags2 (stream);
31    stream->_flags2 |= _IO_FLAGS2_SCANF_STD;
32  
33    va_start (arg, format);
34    done = _IO_vfscanf (stream, format, arg, NULL);
(gdb) 

Thanks

1 Answer 1

1

I replied over on your reddit post. The issue is how you are running gdb. Be sure to enter the command line arguments after gdb has launched.

Try this:

gdb ./speller
...
(gdb) run ~cs50/pset6/dictionaries/large ~cs50/pset6/texts/austin.txt

or whatever dictionary and text you would like to use.

edited to add

Okay, so based on your segfault information above, I'll repeat what I've answered over in reddit:

Program received signal SIGSEGV, Segmentation fault. __isoc99_fscanf (stream=0x0, format=0x80487e6 "%s")

stream=0x0 ... which means that the file was null. Are you checking that you've opened the dictionary?

gdb says that

FILE* file = fopen(argv[1], "r")

is what you have. Is that what you actually want? It's not what you have shown as you are using in your code in your original post above. Remember that speller.c already defines dictionary so you need to open dictionary, not argv[1].

Whenever you use fopen(), it's good practice to make sure it actually returns a pointer and didn't fail. In this case, your segfault is because file is null. If fopen() returns NULL, you should fail gracefully with an error message.

3
  • Thanks, I added more information about the segmentation fault that my code provokes when trying to load the dictionary.
    – FranGoitia
    Commented Aug 12, 2014 at 23:40
  • updated my answer.
    – curiouskiwi
    Commented Aug 13, 2014 at 2:21
  • Sorry, I'm just testing the loading process in another program where a text is passed as an argument and it only goal is to create a hash table with every word in the text file.
    – FranGoitia
    Commented Aug 13, 2014 at 2:32

You must log in to answer this question.

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