C 大小为1的读取无效

C 大小为1的读取无效,c,valgrind,C,Valgrind,所以我在使用Valgrind时得到了这个错误:大小为1的无效读取。这是程序中出现错误的部分。至于节目做什么。msgArr是一组存储为字符串的整数。然后我从**msgArr中提取int值,并将其作为索引从**wordArr中获取字符。我得到的字符,我存储它们**解码。然后我使用了一个void函数来打印**解码的内容,它正确地打印了出来,但是我仍然没有弄清楚为什么会发生这个错误。你能解释一下吗?还有什么是(vg_替换_malloc.c:762)? 编辑:第285行:main()调用decode ms

所以我在使用Valgrind时得到了这个错误:大小为1的无效读取。这是程序中出现错误的部分。至于节目做什么。msgArr是一组存储为字符串的整数。然后我从**msgArr中提取int值,并将其作为索引从**wordArr中获取字符。我得到的字符,我存储它们**解码。然后我使用了一个void函数来打印**解码的内容,它正确地打印了出来,但是我仍然没有弄清楚为什么会发生这个错误。你能解释一下吗?还有什么是(vg_替换_malloc.c:762)? 编辑:第285行:main()调用decode msg(),第242行:decode msg()调用decode words()

以下是出现错误的部分:

175 void printDecodedMsg(char **decoded, int wc){
176     int i;
177     for(i = 0; i < wc; i++)
178     |   printf("%s ", decoded[i]);
179 
180     printf("\n\n");
181 }
182 
183 void decodeWords(char **msgArr, int wc, char **wordArr, int cpWc){
184     char *str, *token, *search = ",", **decoded;
185     int i,j, row, col, charCount;
186 
187     decoded = calloc(wc, sizeof(char*));
188 
189     for(i = 0; i < wc; i++){
190     |   str = calloc(5000,sizeof(char));
191     |   strcpy(str, msgArr[i]);
192     |   decoded[i] = calloc(CHARMAX, sizeof(char));
193     |   token = strtok(str, search);
194     |   charCount = j = 0;
195     |   while(token != NULL){
196     |   |   if(charCount % 2 != 0){
197     |   |   |   col = atoi(token);
198     |   |   |   if(col >= CHARMAX){
199     |   |   |   |   printf("%s!!!The word does not have that many characters!!!", SPACES);
200     |   |   |   |   exit(-1);
201     |   |   |   }
202     |   |   |   decoded[i][j++] = wordArr[row][col];
203     |   |   }
204     |   |   else{
205     |   |   |   row = atoi(token);
206     |   |   |   if(row >= cpWc){
207     |   |   |   |   printf("%s!!!The cipher text does not have that many words!!!", SPACES);
208     |   |   |   |   exit(-1);
209     |   |   |   }
210     |   |   }
211     |   |   token = strtok(NULL, search);
212     |   |   charCount++;
213     |   }
214     |   free(str);
215     }
216     printDecodedMsg(decoded, wc);
217     freeMemChar(decoded, wc);
218 }
175无效打印解码DMSG(字符**已解码,整数wc){
176国际一级;
177表示(i=0;i=CHARMAX){
199 | | | | printf(“%s!!!单词没有那么多字符!!!”,空格);
200 | | | |出口(-1);
201     |   |   |   }
202 | | |解码的[i][j++]=wordArr[row][col];
203     |   |   }
204 | |其他{
205 | | |行=atoi(令牌);
206 | | |如果(行>=cpWc){
207 | | | | printf(“%s!!!密文没有那么多单词!!!”,空格);
208 | | | |出口(-1);
209     |   |   |   }
210     |   |   }
211 | | token=strtok(空,搜索);
212 | | charCount++;
213     |   }
214 |自由(str);
215     }
216打印解码DMSG(解码,wc);
217 freemchar(解码,wc);
218 }
  • printf(“%s”,已解码[i])

  • 这是一行,我们正在读取
    decode[i]
    array(接近行号181)

  • 解码的[i]=calloc(CHARMAX,sizeof(char))

  • 这是第行,在这里我们将内存分配给
    解码[i]
    数组(接近第195行)。Valgrind说,已经分配了15个字节

  • 您需要为每个
    解码[i]
    字符数组分配足够的内存来存储
    \0
    (空)。有关更多信息,请查看此链接:

  • PS:CHARMAX的值无法提供精确的解决方案。valgrind提供的行号可能与编辑器(IDE)的行号不同。

    我认为最好在调用堆栈中显示所有调用方及其参数(即:调用DecodeWord的位置及其参数)。此外,代码中使用的常量值在此代码段中不可用(即:CHARMAX)
    175 void printDecodedMsg(char **decoded, int wc){
    176     int i;
    177     for(i = 0; i < wc; i++)
    178     |   printf("%s ", decoded[i]);
    179 
    180     printf("\n\n");
    181 }
    182 
    183 void decodeWords(char **msgArr, int wc, char **wordArr, int cpWc){
    184     char *str, *token, *search = ",", **decoded;
    185     int i,j, row, col, charCount;
    186 
    187     decoded = calloc(wc, sizeof(char*));
    188 
    189     for(i = 0; i < wc; i++){
    190     |   str = calloc(5000,sizeof(char));
    191     |   strcpy(str, msgArr[i]);
    192     |   decoded[i] = calloc(CHARMAX, sizeof(char));
    193     |   token = strtok(str, search);
    194     |   charCount = j = 0;
    195     |   while(token != NULL){
    196     |   |   if(charCount % 2 != 0){
    197     |   |   |   col = atoi(token);
    198     |   |   |   if(col >= CHARMAX){
    199     |   |   |   |   printf("%s!!!The word does not have that many characters!!!", SPACES);
    200     |   |   |   |   exit(-1);
    201     |   |   |   }
    202     |   |   |   decoded[i][j++] = wordArr[row][col];
    203     |   |   }
    204     |   |   else{
    205     |   |   |   row = atoi(token);
    206     |   |   |   if(row >= cpWc){
    207     |   |   |   |   printf("%s!!!The cipher text does not have that many words!!!", SPACES);
    208     |   |   |   |   exit(-1);
    209     |   |   |   }
    210     |   |   }
    211     |   |   token = strtok(NULL, search);
    212     |   |   charCount++;
    213     |   }
    214     |   free(str);
    215     }
    216     printDecodedMsg(decoded, wc);
    217     freeMemChar(decoded, wc);
    218 }