我想知道为什么valgrind
说:
==9952== 30 bytes in 6 blocks are definitely lost in loss record 1 of 1
==9952== at 0x4C2BF0E: realloc (vg_replace_malloc.c:662)
==9952== by 0x40131F: setCharsPositions (paramsExec.c:99)
==9952== by 0x400CF3: main (main.c:87)
我无法弄清楚我的问题是什么realloc()
(您不必说每次重新分配内存效率低下......);变量char **passwordSet2
是全局的——也许这就是问题所在……如果我做错了什么,请告诉我!我要疯了!
void setCharsPositions(char *charsPos){
int i, k;
char *posStr = NULL;
for(i = 0; i < strlen(charsPos); i++){
posStr = malloc(sizeof(char));
if(charsPos[i] == '['){
for(k = 0, i++; charsPos[i] != ','; i++, k++){
posStr[k] = charsPos[i];
posStr = realloc(posStr, (k+2)*sizeof(char));
}
posStr[k] = '\0';
passwordSet2[atoi(posStr)-1] = malloc(sizeof(char));
for(k = 0, i++; charsPos[i] != ']'; i++, k++){
passwordSet2[atoi(posStr)-1][k] = charsPos[i];
passwordSet2[atoi(posStr)-1] = realloc(passwordSet2[atoi(posStr)-1], (k+2)*sizeof(char));
}
passwordSet2[atoi(posStr)-1][k] = '\0';
}
free(posStr);
}
}