Program only works in debug mode..

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Shisou
    New Member
    • Jul 2007
    • 52

    Program only works in debug mode..

    I think the title says it all... i'm building a program that will read in a dictionary and then ask the user for a string and will check all permutations of the string against the dictionary to see if it's a word.

    but for some reason when i run the program normally it crashes out... but if i run it in debug mode it runs fine... This is the code it gets stuck at

    Code:
        wordList = (char**)malloc(num * sizeof(char*));
        for(i=0;i<num;i++){
           fgets(testWord, MAXLENGTH, input); 
           wordLength = strlen(testWord);              
           wordList[i] = malloc(wordLength);
           strcpy(wordList[i], testWord);
           //printf("wordList[%d] = %s", i, wordList[i]);
        }
    it seems to go through the loop about 8 times then crashes, any help would be appreciated!
  • oler1s
    Recognized Expert Contributor
    • Aug 2007
    • 671

    #2
    I can’t say with certainty what your problem is right now, until I get a chance to take a closer look. But here is what I would look at:

    A C string is null terminated. So if I have “hi”, that’s ‘h’,’i’,’\0’. I would look at those strlen and malloc calls. Do you have enough space for that \0? “hi” has two alphabets, but you need space for three characters. What number does strlen give you? What should you be sending to malloc instead?

    Comment

    Working...