0

I get this seg fault in the strcpy line when I debug with gdb.

int wordlen = strlen(word)+1;
char worda[wordlen];
strcpy(worda,word);

I tried with char* but even then it wasnt working.

Full code for check function:-

bool check(const char* word)
{

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

    //find the wanted bucket
    int buc = hashy(worda);

    //loop through word
    for(int i = 0,n = strlen(worda);i < n;i++){

    if(isupper(worda[i])){
     tolower(worda[i]);
     }

    }

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

    crawler = letters[buc];

    while(crawler != NULL){

      if(strcmp(crawler->word,worda) == 0){
      free(crawler);
      free(worda);
      return true;
      }

    crawler = crawler->next;

   }

    //free(crawler);
    free(worda);
    return false;
}
3
  • 1
    I created a test program with those three lines, plus a declaration, ` const char word = "test";` That worked fine. The only way I could recreate a seg fault was to not assign a value to word, ` const char word;` For that to happen in your program, check() would have had to be called with a NULL parameter. Have you altered speller.c? – Cliff B Oct 23 '15 at 18:41
  • I don't think so, but just to be sure I'll download and unzip it again, and add an if condition checking for NULL too, thanks! – Jedi18 Oct 24 '15 at 5:37
  • I checked, dindt alter speller and when I debug with gdb, word actually contains the word, but it still crashes in strcopy, are there any alternative ways to copy this? – Jedi18 Oct 24 '15 at 5:54

You must log in to answer this question.

Browse other questions tagged .