我编写了一个小的C程序,该程序将3个名称作为输入,然后将这三个值存储到一个节点上。三个值(名称)存储在数组JOHNJAKEMATT
中,每个名称都有4个字符。结果节点看起来像这样:
/*
pointer_to_linked_list
|
v
node[2] node[1] node[0]
| | |
| | |
+------+---+ +------+---+ +-------+------+
| MATT | o-----> | JAKE | o--------> | JOHN | NULL |
+------+---+ +------+---+ +-------+------+
*/
一切都很好,一切都按预期运行。每个名称都存储在数组中,然后移动到节点中的data
。为了实现这一目标,我分配了内存,如果发生故障,我编写了一个函数,如果内存分配失败,则应释放所有内存:
void freeNode(struct _Node_ *current_node)
{
struct _Node_ *tmp_node;
while(current_node != NULL) {
tmp_node = current_node;
current_node = current_node->next;
free(tmp_node);
}
return;
}
我也写了一个功能,它打印出我的链接列表:
void printNodes(struct _Node_ *current_node) {
while(current_node != NULL) {
printf("%sn", current_node->data);
current_node = current_node->next;
}
}
控制台中的结果是每个值的链接列表:
MATT
JAKE
JOHN
一切都起作用,然后我想检查Valgrind(valgrind --leak-check=yes --track-origins=yes ./main
)是否有任何错误 - 是的,有两种错误:
- 没有才能实现的价值,我真的不知道它们可以在哪里..
- 3个内存泄漏,有人知道他们可以在哪里吗?
完整的Valgrind报告:
==20043== Memcheck, a memory error detector
==20043== Copyright (C) 2002-2017, and GNU GPL'd, by Julian Seward et al.
==20043== Using Valgrind-3.13.0 and LibVEX; rerun with -h for copyright info
==20043== Command: ./main
==20043==
MATT
==20043== Conditional jump or move depends on uninitialised value(s)
==20043== at 0x1094BC: printNodes (in /home/osboxes/Dropbox/ESP/main)
==20043== by 0x10928F: main (in /home/osboxes/Dropbox/ESP/main)
==20043== Uninitialised value was created by a stack allocation
==20043== at 0x10935C: saveToLinkedList (in /home/osboxes/Dropbox/ESP/main)
==20043==
==20043== Use of uninitialised value of size 8
==20043== at 0x1094A0: printNodes (in /home/osboxes/Dropbox/ESP/main)
==20043== by 0x10928F: main (in /home/osboxes/Dropbox/ESP/main)
==20043== Uninitialised value was created by a stack allocation
==20043== at 0x10935C: saveToLinkedList (in /home/osboxes/Dropbox/ESP/main)
==20043==
JAKE
==20043== Use of uninitialised value of size 8
==20043== at 0x1094AF: printNodes (in /home/osboxes/Dropbox/ESP/main)
==20043== by 0x10928F: main (in /home/osboxes/Dropbox/ESP/main)
==20043== Uninitialised value was created by a stack allocation
==20043== at 0x10935C: saveToLinkedList (in /home/osboxes/Dropbox/ESP/main)
==20043==
JOHN
==20043== Conditional jump or move depends on uninitialised value(s)
==20043== at 0x1094F4: freeNode (in /home/osboxes/Dropbox/ESP/main)
==20043== by 0x10929B: main (in /home/osboxes/Dropbox/ESP/main)
==20043== Uninitialised value was created by a stack allocation
==20043== at 0x10935C: saveToLinkedList (in /home/osboxes/Dropbox/ESP/main)
==20043==
==20043== Use of uninitialised value of size 8
==20043== at 0x1094DB: freeNode (in /home/osboxes/Dropbox/ESP/main)
==20043== by 0x10929B: main (in /home/osboxes/Dropbox/ESP/main)
==20043== Uninitialised value was created by a stack allocation
==20043== at 0x10935C: saveToLinkedList (in /home/osboxes/Dropbox/ESP/main)
==20043==
==20043== Conditional jump or move depends on uninitialised value(s)
==20043== at 0x4838931: free (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==20043== by 0x1094EE: freeNode (in /home/osboxes/Dropbox/ESP/main)
==20043== by 0x10929B: main (in /home/osboxes/Dropbox/ESP/main)
==20043== Uninitialised value was created by a stack allocation
==20043== at 0x10935C: saveToLinkedList (in /home/osboxes/Dropbox/ESP/main)
==20043==
==20043==
==20043== HEAP SUMMARY:
==20043== in use at exit: 15 bytes in 3 blocks
==20043== total heap usage: 7 allocs, 4 frees, 1,087 bytes allocated
==20043==
==20043== 5 bytes in 1 blocks are definitely lost in loss record 1 of 2
==20043== at 0x483774F: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==20043== by 0x1091D4: copyToNewNode (in /home/osboxes/Dropbox/ESP/main)
==20043== by 0x10942E: saveToLinkedList (in /home/osboxes/Dropbox/ESP/main)
==20043== by 0x10932F: readInput (in /home/osboxes/Dropbox/ESP/main)
==20043== by 0x109271: main (in /home/osboxes/Dropbox/ESP/main)
==20043==
==20043== 10 bytes in 2 blocks are definitely lost in loss record 2 of 2
==20043== at 0x483774F: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==20043== by 0x1091D4: copyToNewNode (in /home/osboxes/Dropbox/ESP/main)
==20043== by 0x109459: saveToLinkedList (in /home/osboxes/Dropbox/ESP/main)
==20043== by 0x10932F: readInput (in /home/osboxes/Dropbox/ESP/main)
==20043== by 0x109271: main (in /home/osboxes/Dropbox/ESP/main)
==20043==
==20043== LEAK SUMMARY:
==20043== definitely lost: 15 bytes in 3 blocks
==20043== indirectly lost: 0 bytes in 0 blocks
==20043== possibly lost: 0 bytes in 0 blocks
==20043== still reachable: 0 bytes in 0 blocks
==20043== suppressed: 0 bytes in 0 blocks
==20043==
==20043== For counts of detected and suppressed errors, rerun with: -v
==20043== ERROR SUMMARY: 14 errors from 8 contexts (suppressed: 0 from 0)
main.c
的完整代码:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct _Node_
{
char *data;
struct _Node_ *next;
};
struct _Node_ *copyToNewNode(char *pointer_value_linked_list, struct _Node_ *nextnode);
struct _Node_ *readInput(char *pointer_buffer_config, int len_char);
struct _Node_ *saveToLinkedList(char *pointer_tmp_buffer, int str_len, int pos_node);
void printNodes(struct _Node_ *current_node);
void freeNode(struct _Node_ *current_node);
struct _Node_ *copyToNewNode(char *pointer_value_linked_list, struct _Node_ *nextnode)
{
struct _Node_ *new_node_pointer;
new_node_pointer = malloc(sizeof(struct _Node_));
if (!new_node_pointer)
{
freeNode(new_node_pointer);
return NULL;
}
new_node_pointer->data = malloc(strlen(pointer_value_linked_list) + 1);
if (!new_node_pointer->data)
{
freeNode(new_node_pointer);
return NULL;
}
strcpy(new_node_pointer->data,pointer_value_linked_list);
new_node_pointer->next = nextnode;
return(new_node_pointer);
}
int main()
{
char buffer_config[] = "JOHNJAKEMATT";
struct _Node_ *pointer_to_linked_list;
pointer_to_linked_list = readInput(buffer_config, strlen(buffer_config));
if(pointer_to_linked_list == NULL)
{
//nodes have already been freed
return 0;
}
printNodes(pointer_to_linked_list);
freeNode(pointer_to_linked_list);
return 1;
}
struct _Node_ *readInput(char *pointer_buffer_config, int len_char)
{
char tmp_buffer_input[5];
int counter;
struct _Node_ *pointer_last_node; // points to last successful saved node
int pos_node = 0;
int counter_next_node = 0;
while(counter_next_node != 3)
{
for(counter = 0; counter < 4; counter++)
{
tmp_buffer_input[counter] = *pointer_buffer_config;
pointer_buffer_config++;
}
tmp_buffer_input[4] = ' ';
pointer_last_node = saveToLinkedList(tmp_buffer_input, strlen(tmp_buffer_input), pos_node);
++pos_node;
counter_next_node++;
}
return pointer_last_node;
}
struct _Node_ *saveToLinkedList(char *pointer_tmp_buffer, int str_len, int pos_node)
{
char value_linked_list[str_len+1];
int counter = 0;
while(counter != str_len)
{
value_linked_list[counter] = *(pointer_tmp_buffer+counter);
counter++;
}
value_linked_list[counter] = ' ';
struct _Node_* node[3];
if(pos_node == 0)
{
node[pos_node] = copyToNewNode(value_linked_list, NULL);
}
else
{
node[pos_node] = copyToNewNode(value_linked_list, node[pos_node-1]);
}
return node[pos_node];
}
void printNodes(struct _Node_ *current_node) {
while(current_node != NULL) {
printf("%sn", current_node->data);
current_node = current_node->next;
}
}
void freeNode(struct _Node_ *current_node)
{
struct _Node_ *tmp_node;
while(current_node != NULL) {
tmp_node = current_node;
current_node = current_node->next;
free(tmp_node);
}
return;
}
如果有人能帮助我找到错误,即使阅读了Valgrind手册,我也会非常感激,我也不知道错误会在哪里。谢谢。
进入savetolinkedlist
struct _Node_* node[3];
if(pos_node == 0)
{
node[pos_node] = copyToNewNode(value_linked_list, NULL);
}
else
{
node[pos_node] = copyToNewNode(value_linked_list, node[pos_node-1]);
}
struct _Node_* node[3]
不是初始化的,当pos_node不是0时,您将使用矢量的条目,而不是初始化=> copyToNewNode
将创建一个带有随机的下一个指针
struct _Node_* node[3];
必须为 static struct _Node_* node[3];
在freeNode()
中,您错过了释放字段数据,必须像:
void freeNode(struct _Node_ *current_node)
{
struct _Node_ *tmp_node;
while(current_node != NULL) {
tmp_node = current_node;
current_node = current_node->next;
if (tmp_node->data != NULL)
free(tmp_node->data);
free(tmp_node);
}
}
2018年没有发现另一个错误。此复制循环:
while(counter != str_len)
无法复制零终止字符。strcpy
功能应仅使用。修复它的另一种方法是while (counter <= str_len)
或零启动目标数组。
这是识别非初始化堆栈字节的可能来源,因为复制已进入VLA,然后将其复制到链接列表中的动态存储中。