[英]C - how to get rid of memory leaks?
How can I get rid of memory leaks from for example this function:我怎样才能摆脱 memory 泄漏,例如这个 function:
void AddData(Data **head, char *sentence, int number) {
Words *words = NULL;
char delimiters[] = " \n\0";
char *token = strtok(sentence, delimiters);
while (token != NULL) {
if (IsAlphabetical(token) == 1) {
char *string = (char *)malloc((strlen(token) + 1) * sizeof(char));
strcpy(string, token);
AddWords(&words, string);
free(string);
}
token = strtok(NULL, delimiters);
}
Data *temp = *head;
Data *newData = (Data *)malloc(sizeof(Data));
newData->lineNumber = number;
newData->words = words;
newData->pNext = NULL;
if (*head == NULL)
*head = newData;
else {
while (temp->pNext != NULL)
temp = temp->pNext;
temp->pNext = newData;
}
}
My personal opinion is the leaks appear because of newData
, temp
and words
variables.我个人的看法是由于
newData
、 temp
和words
变量而出现泄漏。
I have a few similar functions that cause the same problem.我有一些类似的功能会导致同样的问题。 I have also function for deleting Data struct, but when I call it at the end of previous function in such way
DeleteData(&temp)
program will not execute.我还有 function 用于删除数据结构,但是当我在之前的 function 的末尾调用它时,
DeleteData(&temp)
程序将不会执行。 I think it's because my whole list is deleted.我认为这是因为我的整个列表都被删除了。
void DeleteData(Data **head) {
Data *temp = *head;
while (temp->pNext != NULL) {
Data *next = temp->pNext;
DeleteWords(&temp->words);
free(temp);
temp = next;
}
free(temp); /* to check that */
*head = NULL;
}
How can I fix this?我怎样才能解决这个问题?
Here are some problems I identified:以下是我发现的一些问题:
The trailing \0
in char delimiters[] = " \n\0";
char delimiters[] = " \n\0";
中的尾随\0
= " \n\0"; is useless.没用。
Testing if (IsAlphabetical(token) == 1)
might be too restrictive.测试
if (IsAlphabetical(token) == 1)
可能过于严格。 In C anything non 0 is true so you might test if (IsAlphabetical(token) != 0)
or just if (IsAlphabetical(token))
.在 C 中,任何非 0 都为真,因此您可以测试
if (IsAlphabetical(token) != 0)
或if (IsAlphabetical(token))
。
Why allocate a copy of the string to pass to AddWords(&words, string);
为什么要分配一个字符串的副本传递给
AddWords(&words, string);
and then free(string)
thereafter?然后是
free(string)
? If Addwords()
does not keep the pointer it gets, there is no need to allocate a copy.如果
Addwords()
不保留它获得的指针,则无需分配副本。
Does AddWords()
call strtok()
? AddWords()
是否调用strtok()
? strtok()
is non-reentrant, which means that if AddWords()
or any other function called in this loop, such as IsAlphabetical()
calls strtok()
, the context used in the loop will be corrupted. strtok()
是不可重入的,这意味着如果在此循环中调用AddWords()
或任何其他 function,例如IsAlphabetical()
调用strtok()
,则循环中使用的上下文将被破坏。 You should use strtok_r()
instead.您应该改用
strtok_r()
。
in function DeleteData
, why do you iterate testing while (temp->pNext != NULL)
?在 function
DeleteData
中,为什么要while (temp->pNext != NULL)
迭代测试? The last item in the list does not get freed properly, the DeleteWords(&temp->words);
列表中的最后一项没有正确释放,
DeleteWords(&temp->words);
is not called.不叫。 This may cause a memory leak .
这可能会导致 memory 泄漏。 You should just write:
你应该只写:
void DeleteData(Data **head) { Data *temp = *head; while (temp;= NULL) { Data *next = temp->pNext; DeleteWords(&temp->words); free(temp); temp = next; } *head = NULL; }
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.