- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我写了一个小的 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 种错误:
完整的 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/
所以我得到了这段代码,我应该将其放入我的代码中,该代码使用 getopt_long() 函数从命令行读取选项,并通过切换菜单传递它们。问题在于该函数,如果我不初始化 long_opts 的值,我会收到
现在,首先,我已经在该站点上阅读了其他答案,以及有关 jackson 序列化的其他答案,但是它们都提供了忽略null字段的方法。但是,在Java中,int不能是null。 我正在尝试将Java对象Ob
这个 valgrind 输出让我感到困惑,因为它似乎没有引用我的代码,而且我在使用 STL 编写的多个程序中都得到了它,尽管没有 HEAP SUMMARY 错误: $ valgrind -v --le
我写了一个小的 c 程序,它获取 3 个名称作为输入,然后将这三个值存储到一个节点。这三个值(名称)存储在数组 JOHNJAKEMATT 中,每个名称有 4 个字符。结果节点如下所示: /* poin
我正在写一个 linux 模块,这里是代码片段。它提示变量的统一化使用,但我想我已经检查过了在使用它之前有足够的检查。我认为这可能是相关的ERR_PTR 和 PTR_ERR 宏,但我不知道为什么。有人
我是 IOS 开发和处理内存泄漏的新手。在我的项目中,在分析项目时我遇到了一些内存泄漏。但是我无法在下面的代码中修复下面的逻辑错误。 CGRect labelframe; if ([questonmo
我分配一个二维数组并使用 memset 用零填充它。 #include #include #include void main() { int m=10; int n =10;
我有以下程序 http://ideone.com/1RPs8E .它使用可变参数函数 tlog 将在日志文件中打印一行。它接收打印行的级别、文件名、一行和调试信息的函数以及 printf 的格式和参数
我们正在尝试建立一个哈希表和一些用于设置、获取和删除表中值的过程。我们在第 35 行和第 53 行遇到“条件跳转或移动取决于未初始化的值”问题。 ==23720== Conditional jump
我正在尝试用 C 语言编写自己的 shell。下面的代码适用于没有管道的命令,但不适用于其他情况。 使用 --trace-children=yes 和 --track-origins=yes 运行 v
如何在 valgrind 的输出中抑制这个错误? ==16727== 1 errors in context 2 of 18: ==16727== Conditional jump or move d
我一直在使用 Valgrind 来查找我的代码中的内存泄漏,虽然没有发现内存泄漏,但报告了一些错误,所有这些错误都源于单个函数/类方法: ==17043== ERROR SUMMARY: 10100
该代码将编译并运行,并创建预期的输出,除非运行valgrind时才会出现这些内存泄漏。以下代码在Visual Studio上运行,不会出现任何警告或错误。 所以我的问题是,此内存泄漏发生在哪里?我是C
我正在为学校用 C 语言编写一个简约的 shell。它工作正常,但使用 Valgrind 我收到此错误,我无法弄清楚我做错了什么。我在程序开始时得到它: ==9396== Conditional ju
Valgrind 告诉我两个我无法解决的错误。我在以下函数 unplug_set 中收到“条件跳转或移动取决于未初始化的值”错误消息。是因为 NULL 检查吗?该函数应该从双链表中拔出元素。 void
这是我在第一节编程基础课后遇到的问题之一 gets() 工作正常(我认为),但“cin”拒绝工作 int main(void) { char *s1, *s2; puts("Enter
当我将我的项目与库的静态版本链接时,VALGRIND 开始报告错误编号“条件跳转或移动取决于未初始化的值”错误。问题是项目本身除了空的主函数外什么都没有,所以肯定是库的问题。 库的共享二进制文件和静态
我正在尝试编写一个将新结构添加到链表中的函数。无论我做什么,Valgrind 都会不断给我这个错误。这是代码: /* Stores a new address record to the linked
这是一些 Valgrind 日志的一部分: ==1652== Conditional jump or move depends on uninitialised value(s)==1652==
我在 valgrind 中遇到错误,不知道哪里出了问题。错误是:valgrind 输出: ==1112== Conditional jump or move depends on uninitiali
我是一名优秀的程序员,十分优秀!