JumpingRock
JumpingRock

Reputation: 59

Issue with Segmentation Faults using Double Pointers in C

So my goal is to create an array of specific words. Here is the current code that I am using:

char **specificWords; //Global
specificWords = malloc(100 * sizeof( char *));

int addToArray(char * word, int i){
     specificWords[i] = word;
     printf("%s\n", specificWords[i]);
return 0;
}

Now just pretend in the main the function is repeatedly calling the function with incrementing "i" values and new "words". The print statement inside of the function works perfectly fine, but if I attempt to use the same print statement outside of the function, the program closes with a seg fault error.

Im still new to C programming but I have tried many different things, from allocating space for the array every time it is called. To different methods of incrementing such as "(**specificWords)++", and just using a loop in the main, but I really cannot figure it out.

Let me know if you need anything clarified, thanks.

EDIT: Here is the Main... I was trying to post a program to explain my issue, so here is the real one:

char **specificWords; //Global
char *globalString;

int main(int argc, char* argv[]) {
    specificWords = malloc(100 * sizeof( char *));
    int newLineCount = countLines(globalString);
    addToArray(newLineCount);
    printf("%s\n", specificWords[0]); //segFaults
    return 0;
}


int addToArray(int newLineCount){
   int ch;
   int loc = 0;
   char *tempKeyword;
   char temp[5026];
   int j = 0;
   int k = 1;
   j = 0;
   for(int i = 0; i < newLineCount; i++){
       while(1){
           //I read in a file and made the whole thing one big string which is global string
           ch = globalString[loc];
           if(ch == '\n')
               break;
           temp[j] = ch;
           loc++;
           j++;
       }
       loc++;
       temp[j] = '\0';
       tempKeyword = temp;
       specificWords[k] = tempKeyword;
       printf("%s\n", specificWords[k]);
       //k++; // if used seg faults...
    }
}

Upvotes: 0

Views: 415

Answers (2)

nio
nio

Reputation: 5289

  1. You have used a local string variable char temp[5026]; defined inside of a function that will cease to exist after you exit the function, hence your printf will perfectly work in the function but will crash if you try to print anything outside of the addToArray.

    Explanation:

    {   // this is a block of code, for example function body
    
        char word[128]; // this string will exist at this level of code
    
        // it will work perfectly at this level
    
    }
    // now, there's no word variable at all, any pointer to it's address (data)
    // is invalid and could crash or you would read garbage from it
    
  2. At this line:

    tempKeyword = temp;
    specificWords[k] = tempKeyword;
    

    You are always assigning whole temp buffer for each word, so you would get always the first word read in all specificWords indexes for example in specificWords[2]. To follow unwind's advice, rewrite the code to read every word to temp, from the beginning and then using strdup to copy the word into dynamically allocated memory that will survive returning from your function. Inside of your for loop:

     j = 0; // always reset the position to buffer from beginning
     while(1){
           //I read in a file and made the whole thing one big string which is global string
           ch = globalString[loc];
           if(ch == '\n')
               break;
           temp[j] = ch;
           loc++;
           j++;
       }
       loc++;
       temp[j] = '\0';
       tempKeyword = strdup(temp);
       specificWords[k] = tempKeyword;
       printf("%s\n", specificWords[k]);
    
  3. You are counting your word indexes from 1 at this line:

    int k = 1;
    

    In C language, indexes in arrays start from zero, so you are losing one word from capacity of specificWords so you can store maximum of 99 words in the array. To solve this you could start with index k=0.

Upvotes: 2

unwind
unwind

Reputation: 399871

The pointer word that you are storing is probably going out of scope or becoming invalid in some other way.

It's often wrong to directly store pointers like you do, perhaps the addToArray() function should store the return value of strdup(word) instead. Then you also need to test it for failure, of course, since it allocates memory.

Upvotes: 2

Related Questions