【问题标题】:C Seg Fault with Token Method: Possible malloc error?C Seg Fault with Token Method: 可能的 malloc 错误?
【发布时间】:2015-03-03 00:40:15
【问题描述】:

我真的很难过。运行程序时,我不断收到分段错误错误。我已经玩了一段时间了,我怀疑错误可能在于我的内存分配或释放。请帮助:(我不知道我做错了什么。谢谢!

代码:

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 }

测试:

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);

【问题讨论】:

标签: c segmentation-fault malloc token free


【解决方案1】:

您正在使用传递numTokens 的地址而不对其进行初始化,然后使用该地址处的值

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

因为它没有被初始化,所以值可能是任何东西,所以会发生意想不到的事情,例如分段错误。

还有

【讨论】:

  • 似乎numTokenstokenize 应该根据输入字符串计算的东西。
  • 好的,所以我调整了一些东西,结果发现我的 numTokens 没有被计算。一旦我在 malloc 之前将计算插入到方法中,我的段错误就消失了。谢谢大家
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多