gpt4 book ai didi

c - 如何修复 'Use of uninitialised value' 以及如何找到泄漏位置?

转载 作者:行者123 更新时间:2023-12-01 13:17:06 25 4
gpt4 key购买 nike

我写了一个小的 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("%s\n", current_node->data);
current_node = current_node->next;
}
}

控制台中的结果是每个值的链表:

MATT
JAKE
JOHN

一切正常,然后我想检查 valgrind (valgrind --leak-check=yes --track-origins=yes ./main) 是否有任何错误 - 是的,有2 种错误:

  • 未初始化的值,我真的不知道它们可能在哪里..
  • 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] = '\0';

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] = '\0';

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("%s\n", 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 手册之后我也不知道错误在哪里。谢谢。

最佳答案

保存到LinkedList

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 时,您将使用未初始化 vector 的条目 => 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);
}
}

关于c - 如何修复 'Use of uninitialised value' 以及如何找到泄漏位置?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53868644/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com