c中的递归函数中的内存泄漏
Memory leak in a recursive function in c
我需要一些帮助来解决我的 C 程序中的内存泄漏问题。以下函数搜索基数特里树以查找具有给定数字的单词。它在每个递归调用中分配一些内存,我不知道如何整理它以便分配的块不会丢失。请帮忙。
char *substring(char *str, int position, int length) {
int c = 0;
char *sub = malloc(length*(sizeof(char))+1);
while (c < length) {
sub[c] = str[position + c];
c++;
}
return sub;
}
void prev(int w, int start, int end) {
char *newWord = "";
bool found = false;
void prevRec(struct tNode *t,
int w, int start, int end, char *soFar, int prevLength) {
if (t != NULL) {
char *updatedWord = malloc(strlen(soFar) + strlen(t->word));
strcpy(updatedWord,soFar);
strcat(updatedWord,t->word);
printf("%s\n", updatedWord);
int length = strlen(t->word);
if (t->count == w) {
found = true;
if ((start > -1) && (end <= strlen(updatedWord))) {
newWord = updatedWord;
} else {
newWord = "";
}
} else {
struct tNode *tmp = t->child;
struct tNode *tmp1 = NULL;
while ((tmp != NULL) && (!found)) {
prevRec(tmp,w,start,end,updatedWord,length);
tmp1 = tmp;
tmp = tmp->brother;
updatedWord = substring(updatedWord, 0, strlen(updatedWord) - prevLength);
}
}
}
}
prevRec(root,w,start,end,newWord,0);
printf("%s\n",newWord);
if (strlen(newWord) == 0) printf("ignored");
else {
char *tmp = substring(newWord,start,end - start + 1);
insert(tmp);
free(tmp);
}
您必须释放您分配的内容。在你的情况下你可以做某事。像那样:替换
updatedWord = substring(updatedWord, 0, strlen(updatedWord) - prevLength);
来自
char *sub = substring(updatedWord, 0, strlen(updatedWord) - prevLength);
free( updatedWord );
updatedWord = sub;
并添加另一个
free( updatedWord );
作为 if( t != NULL )
块的最后一行。
除了@Eregith 在他的评论中已经提到的,您分配的长度中缺少 NULL 的“+1”。你还应该添加一些错误检查,因为 malloc()
可能 return NULL
我需要一些帮助来解决我的 C 程序中的内存泄漏问题。以下函数搜索基数特里树以查找具有给定数字的单词。它在每个递归调用中分配一些内存,我不知道如何整理它以便分配的块不会丢失。请帮忙。
char *substring(char *str, int position, int length) {
int c = 0;
char *sub = malloc(length*(sizeof(char))+1);
while (c < length) {
sub[c] = str[position + c];
c++;
}
return sub;
}
void prev(int w, int start, int end) {
char *newWord = "";
bool found = false;
void prevRec(struct tNode *t,
int w, int start, int end, char *soFar, int prevLength) {
if (t != NULL) {
char *updatedWord = malloc(strlen(soFar) + strlen(t->word));
strcpy(updatedWord,soFar);
strcat(updatedWord,t->word);
printf("%s\n", updatedWord);
int length = strlen(t->word);
if (t->count == w) {
found = true;
if ((start > -1) && (end <= strlen(updatedWord))) {
newWord = updatedWord;
} else {
newWord = "";
}
} else {
struct tNode *tmp = t->child;
struct tNode *tmp1 = NULL;
while ((tmp != NULL) && (!found)) {
prevRec(tmp,w,start,end,updatedWord,length);
tmp1 = tmp;
tmp = tmp->brother;
updatedWord = substring(updatedWord, 0, strlen(updatedWord) - prevLength);
}
}
}
}
prevRec(root,w,start,end,newWord,0);
printf("%s\n",newWord);
if (strlen(newWord) == 0) printf("ignored");
else {
char *tmp = substring(newWord,start,end - start + 1);
insert(tmp);
free(tmp);
}
您必须释放您分配的内容。在你的情况下你可以做某事。像那样:替换
updatedWord = substring(updatedWord, 0, strlen(updatedWord) - prevLength);
来自
char *sub = substring(updatedWord, 0, strlen(updatedWord) - prevLength);
free( updatedWord );
updatedWord = sub;
并添加另一个
free( updatedWord );
作为 if( t != NULL )
块的最后一行。
除了@Eregith 在他的评论中已经提到的,您分配的长度中缺少 NULL 的“+1”。你还应该添加一些错误检查,因为 malloc()
可能 return NULL