- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
在我的程序上运行 valgrind 后,我得到以下输出:
==17731== Thread 2:
==17731== Conditional jump or move depends on uninitialised value(s)
==17731== at 0x401CD8: poll_existing_connections (connmgr.c:112)
==17731== by 0x401ACD: connmgr_listen (connmgr.c:69)
==17731== by 0x40161A: connmgr (main.c:148)
==17731== by 0x5545609: start_thread (in /usr/lib64/libpthread-2.22.so)
==17731== Uninitialised value was created by a heap allocation
==17731== at 0x4C2AB8B: realloc (vg_replace_malloc.c:785)
==17731== by 0x401B64: poll_new_connection (connmgr.c:85)
==17731== by 0x401AB9: connmgr_listen (connmgr.c:68)
==17731== by 0x40161A: connmgr (main.c:148)
==17731== by 0x5545609: start_thread (in /usr/lib64/libpthread-2.22.so)
==17731==
我怀疑我使用 realloc 的方式有问题。我开始谷歌搜索并尝试了一些我发现对其他用户有用的解决方案,但这些解决方案都不适合我。我也尝试过使用不同的方式(malloc 新内存并将数组的旧值复制到新内存中)但是这导致了 valgrind 的相同类型的错误。
有什么可能出错的建议吗?
我的代码(connmgr.c:112):
sensor_conn_t * sensor_conn = dpl_get_element_at_index(sensor_sockets, i);
poll_action = poll_list[i].revents == POLLIN;
if(poll_action == POLLIN) {
//The sensor sent some data
read_data(sensor_conn, i, buffer);
} else {
//No data received from the sensor
check_timeout();
}
我的代码(connmgr.c:85):
//Add the new connection to an array so that it is pollable
struct pollfd * new_poll_list = realloc(poll_list, (nb_connections + 1) * sizeof(struct pollfd));
assert(new_poll_list != NULL);
poll_list = new_poll_list;
tcp_get_sd(client, &poll_list[nb_connections].fd);
poll_list[nb_connections].events = POLLIN;
最佳答案
您正在调用 realloc,但您没有初始化比原始缓冲区大的缓冲区内容。基本上,您将 new_poll_list 增长到的所有内存都未初始化。
调用 realloc 后,确保初始化原始缓冲区大小之后的区域。
关于C(Linux)-valgrind : Conditional jump or move depends on uninitialised value(s) after realloc,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37534568/
如果 realloc 失败并返回 NULL 是前一个缓冲区被释放还是保持不变?我没有在手册页中找到那条特定的信息,我不确定该怎么做。如果内存被释放,那么双重释放可能会有风险。如果没有,就会发生泄漏。
OS: Linux CC: GCC 4.8.2 目标:改变 char* 的大小 -> 变小 问题:更改后的大小相同... 行是带有数据的字符串... 代码片段: char * tmp = NUL
在一个函数中我使用了 malloc : void name1(struct stos* s) { s = malloc (4 * sizeof (int)); } 一切正常。但是后来我用了rea
我知道有一个 realloc允许我调整内存块大小的函数(它与一个免费函数配对)。但是,我正在尝试对一些成员指针使用 new 而不是 realloc 分配内存的 c++ 类执行相同的操作。在 C++ 中
我正在尝试在 C 中创建一个动态整数数组,它应该在填满后自动将其大小加倍。 要扩展数组的大小,我想使用 realloc 函数。不幸的是,指向我的 DynamicArray 和 GCC 崩溃的数据的指针
这是我被教导使用的方式 realloc() : int *a = malloc(10); a = realloc(a, 100); // Why do we do "a = .... ?" if(a
我尝试在每个循环中使用 realloc(),因此我只为 C 中的 int 数组使用必要的内存,但输出值已更改。尽管如此,在我的代码中使用 Valgrind 时,我得到了正确的值。 我在做 Advent
平台:Linux 3.2.0 x86 (Debian Wheezy) 编译器:GCC 4.7.2 (Debian 4.7.2-5) 我想知道如果我尝试 realloc() 一个已递增的指针会发生什么。
我知道可以在内核中使用 malloc 在 GPU 的全局内存上分配内存。是否也可以使用realloc? 最佳答案 您可以为您的数据类型编写自己的 realloc 设备函数。 只需为新数组分配新空间,将
我在对数组使用 malloc/realloc 命令时遇到了一些问题。我创建了一个包含一些整数的小数组,并尝试通过使用 realloc 扩展大小并添加值来为其添加一个值,但是当我这样做时,0 索引的值不
背景: 我使用 calloc() 创建了一个数组,一切都运行良好。然后我使用 realloc() 使数组更大。它似乎只是创建一个没有任何内容的新指针,并在我尝试访问数组中的元素时调用运行时错误。 我的
假设我已经使用 malloc() 分配了内存,如果我在我的代码中这样做: char *newline = realloc ( oldline , newsize ); // Assuming oldl
我正在尝试在下面的程序中使用 realloc 重新分配内存,并在我使用 malloc(i = (int*)malloc(5 * sizeof(int))) 使用react的 realloc 初始内存之
为什么下面的代码输出两次 4,而不是 8 和 20?谢谢 int size = 0; int *pointer; pointer = malloc(2 * sizeof(int)); size = s
我正在尝试将一堆 WCHAR 添加到缓冲区。这个函数就是将它添加到我的缓冲区中的原因.. DWORD add_to_buffer(BYTE *databuffer, WCHAR *path, WCHA
可能我的大脑现在不能正常工作......我想知道为什么我在我的代码中收到提到的错误: int ** zm; zm = (int**)calloc(1, sizeof(int*)); *zm = (in
我正在尝试用 C 语言编写代码,但遇到了 realloc 的问题。该代码在某个时间点工作正常,但在另一时间重新分配期间因堆损坏错误而崩溃。我已将填充数据的结构和函数粘贴到其中。谁能告诉我我是否在这里做
realloc 会改变它的第一个参数吗? 改变第一个参数是否取决于实现? 有什么理由不应该是const吗?作为反例,memcpy 将其 src 参数设为 const。 ISO C 标准,第 7.20.
我在 realloc 中遇到此错误,该错误仅发生在我学校的实验室计算机上,而不发生在我的计算机上。 在此程序中,我将行号存储在 File_Node 结构中。 File_Node 是一个链表的一部分,每
来自 man realloc:realloc() 函数返回一个指向新分配的内存的指针,该指针适合任何类型的变量,可能与 ptr 不同,如果请求失败,则返回 NULL . 因此在此代码片段中: ptr
我是一名优秀的程序员,十分优秀!