#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2009
    Posts
    3
    Rep Power
    0

    Help with a bug, it is in one line and idk why


    I highlighted the line of code that causes the program to crash, but idk why it is crashing it,

    Code Code:
    /*
     *
     *
     *
     *
     */
    #include <stdio.h>
    #include <math.h>
    #include <string.h>
     
    #define MAX 31
    #define DIC "dictionary.txt"
    #define QUERY "query.txt"
    #define TABLE_SIZE 9999
    struct list{
        char word[MAX];   
        struct list *next;
    };
    struct diction{
        struct list *words[TABLE_SIZE];
        int size;
    };
     
    void print_dic(struct diction *dic);
    void initiate_table(struct diction dic);
    void add_word(struct diction *dic, char word[MAX]);
    int hash_it(char word[MAX]);
     
    int main(){
        printf("Start:\n");
        struct diction dic;
        int x, sized;
        FILE *fin=fopen(DIC,"r");
        fscanf(fin,"%d ",&sized); 
        dic.size=11;
     
        //Table SetUp
        initiate_table(dic);
            print_dic(&dic);
        for(x=0;x<dic.size;x++){
            char temp[MAX];
            fscanf(fin,"%s ",temp);
            add_word(&dic,temp);
            printf("ADD Done\n");
        }
     
        print_dic(&dic);
     
        system("PAUSE");
        return 0;
    };
     
    void initiate_table(struct diction dic){
        int x;
        for(x=0;x<dic.size;x++)
            dic.words[x]=NULL;
        return;
    };
     
    void add_word(struct diction *dic, char wor[MAX]){
        printf("ENETER ADD\n");
        int hash=hash_it(wor)%dic->size;
        if(dic->words[hash]==NULL){
            printf("EEEE %d and %s and Dic %s\n", hash, wor,dic->words[hash]->word);
     
             strcpy( dic->words[hash]->word  , wor );//Whats is Wrong with this line??? 
     
            printf("www\n");
        }
     
        else{
            struct list *new=(struct list*)malloc(sizeof(struct list));
            strcpy(new->word,wor);
     
            if(hash==0){
                char tempw[MAX];
                strcpy(tempw,dic->words[0]->word);
                struct list *temp=dic->words[0]->next;
                dic->words[0]->next=temp;
                strcpy(dic->words[0]->word,tempw);
            }
            else{
                new->next=dic->words[hash];
                dic->words[hash-1]->next=new;        
            }
        }
     
        return;    
    };
    int hash_it(char word[MAX]){
        int hash, x;
        int length=strlen(word);
        for(x=length-1;x>=0;x--)
            hash=pow((int)word[x],x-1);
     
        return hash;
    };
    void print_dic(struct diction *dic){
        int x;
        for(x=0;x<dic->size;x++){
            printf("X: %d Word: %s\n",x,dic->words[x]->word);
        }
        return;
    }

    here is the txt file
    11
    10000
    apple
    bag
    cane
    dog
    elephant
    fox
    game
    hand
    ivory
    jackolantern
  2. #2
  3. /usr/bin/drinking
    Devshed Novice (500 - 999 posts)

    Join Date
    Jul 2004
    Posts
    719
    Rep Power
    1886
    Welcome to the forum. Please edit your original post so that all the code is surrounded by code tags. It makes things so much easier to look at.
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2009
    Posts
    3
    Rep Power
    0
    Originally Posted by Muzza
    Welcome to the forum. Please edit your original post so that all the code is surrounded by code tags. It makes things so much easier to look at.
    Fixed, it so it is in the code forum, next to where the bug is their is a comment next to it

    Also this is written in C and complied on netbeans
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Feb 2004
    Location
    San Francisco Bay
    Posts
    1,939
    Rep Power
    1313
    Code:
        if(dic->words[hash]==NULL){
            printf("EEEE %d and %s and Dic %s\n", hash, wor,dic->words[hash]->word);
    Well, you check that the pointer is NULL, and then you proceed to dereference it.
  8. #5
  9. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,162
    Rep Power
    2222
    Well, if that pointer has not been initialized, I'm not sure that it would be NULL, but rather could be any garbage invalid address.

    OP, where do you allocate space for dic->words[hash]->word to point to? I.e., where do you initialize that pointer?


    Comment: Please standardize and clean up your indenting. Be consistent. It's still a bit confusing as it is, albeit much better than with indenting stripped out.

    Recommendation: start each function in column 1. I.e. (AKA "id est", AKA "that is"), start the function header and the open and close brace in column 1. Then start the function body at the next level of indentation. That will help immensely in visually setting each function apart from the others.

    Also, the closing brace of a function does not need to be followed by a semicolon. I'm not sure whether it would generate warnings, but it makes things a little more confusing.


    PS
    I have cited to yet another newbie denizen of this forum this dictum:
    Use not those variables which you have deigned to initialize!

    That goes quadrupally for pointers! (and only quadrupally, because I do not know the adjective for the quadrillionth degree -- ie, to 10 to the fifteenth power ).


    Do you know what a "golden bullet" is? In military slang, it is that one piece of ordnance (ie, bullet, shrapnel, bomb, what-have-you) that ruins your entire day [From the NAVTERMFAQ: "Golden BB - That one bullet or shell that ruins your (or your opponent’s) day in a dogfight or when conducting ground attacks."].

    Well, dig this: An uninitialized pointer is guaranteed to be your own very personal golden BB! EVERY SINGLE FRAKKKKING TIME!!!!

    How many more levels of emphasis do I need to add to that?
    Last edited by dwise1_aol; April 15th, 2009 at 01:51 AM.

IMN logo majestic logo threadwatch logo seochat tools logo