-2

I'm really stumped. I keep getting a segmentation fault error when I run my program. I've been toying with it for a while now and I suspect the error may lie in my memory allocation or freeing. Please help :( I have no idea what I'm doing wrong. Thank you!

CODE:

163 //Arguments: s1, delimter                                                                          
164 //Returns a pointer to a char array containing entries for each token in s1.  Think of this        
165 //as a 2-D array.  Each row contains a char* (token).  This function creates a NEW char**          
166 //variable and NEW char* variables for each token.  Your method should be memory efficient.        
167 //In other words, the space created for each token should be an exact fit for the space            
168 //that token needs.  If a token is five characters, allocate 5 bytes (plus null terminator).       
169 //The function also writes the number of tokens into the numTokens variable.      

170 char** tokenize(const char *s, char delimiter, int *numTokens){
171   //set variables                                                                                  
172   int i,j,a;
173     //alloacte space                                                                               
174   char **arr = (char **)malloc((*numTokens) * sizeof(char *));
175   for (i=0; i<(*numTokens); i++){
176     arr[i] = (char *)malloc(50 * sizeof(char));
177   }
178 
179   //while loop to search commence second search                                                    
180   //fill new 2d array                                                                              
181   while(s[a] != '\0'){
182     if(s[a] == delimiter){
183       j++;
184       i = 0;
185     }else{
186       arr[i][j] = s[a];
187       i++;
188     }
189     a++;
190   }// end while                                                                                    
191 
192   //to end arr                                                                                     
193   arr[i][j] = '\0';
194 
195   return arr;
196 }

TEST:

137   char **test;
138   char *testFour;
139   int numTokens, *token;
140   testFour = (char *)calloc(50, sizeof(char));
141   sprintf(testFour, "check it with a space");
142   token = &numTokens;
143 
144   test = tokenize(testFour, ' ', token);
145 
146   if (compare(test[0], "check"))
147     printf("Test 1: Pass\n");
148   else
149     printf("Test 1: Fail\n");
150   if (compare(test[1], "it"))
151     printf("Test 2: Pass\n");
152   else
153     printf("Test 2: Fail\n");
154   if (compare(test[4], "space"))
155     printf("Test 3: Pass\n");
156   else
157     printf("Test 3: Fail\n");

free(testFour);
D_Toy
  • 19
  • 9

1 Answers1

2

You are using passing the address of numTokens without initializing it, and then using the value at that address in

char **arr = (char **)malloc((*numTokens) * sizeof(char *));

since it's not initialized that value could be anything, so unexpected things should happen, like a segmentation fault for example.

Also

Community
  • 1
  • 1
Iharob Al Asimi
  • 52,653
  • 6
  • 59
  • 97
  • It seems like `numTokens` is something `tokenize` is supposed to calculate based on the input string. – Barmar Mar 03 '15 at 00:43
  • Okay, so I adjusted a few things and it turned out that my numTokens wasn't being calculated. Once I inserted the calculation into the method before malloc my seg fault went away. thanks guys – D_Toy Mar 03 '15 at 00:52