c++ - "points to uninitialised byte(s)"Valgrind 错误

标签 c++ sockets valgrind memory-alignment

我一直在使用 Valgrind 来查找我的代码中的内存泄漏,虽然没有发现内存泄漏,但报告了一些错误,所有这些错误都源于单个函数/类方法:

==17043== ERROR SUMMARY: 10100 errors from 3 contexts (suppressed: 0 from 0)
==17043== 
==17043== 100 errors in context 1 of 3:
==17043== Syscall param socketcall.sendto(msg) points to uninitialised byte(s)
==17043==    at 0x5441DA2: send (send.c:28)
==17043==    by 0x404C2D: unix_socket::sendMsg(char, double) (in /home/joao/CloudPT/Bolsa/Webots/controllers/darwin-pi2/client)
==17043==    by 0x404F1C: unix_socket::sendVectorXd(Eigen::Matrix<double, -1, 1, 0, -1, 1> const&) (in /home/joao/CloudPT/Bolsa/Webots/controllers/darwin-pi2/client)
==17043==    by 0x401F2A: main (in /home/joao/CloudPT/Bolsa/Webots/controllers/darwin-pi2/client)
==17043==  Address 0x7feffff61 is on thread 1's stack
==17043==  Uninitialised value was created by a stack allocation
==17043==    at 0x404BE6: unix_socket::sendMsg(char, double) (in /home/joao/CloudPT/Bolsa/Webots/controllers/darwin-pi2/client)
==17043== 
==17043== 
==17043== 100 errors in context 2 of 3:
==17043== Syscall param socketcall.sendto(msg) points to uninitialised byte(s)
==17043==    at 0x5441DA2: send (send.c:28)
==17043==    by 0x404C2D: unix_socket::sendMsg(char, double) (in /home/joao/CloudPT/Bolsa/Webots/controllers/darwin-pi2/client)
==17043==    by 0x404E8A: unix_socket::sendVectorXd(Eigen::Matrix<double, -1, 1, 0, -1, 1> const&) (in /home/joao/CloudPT/Bolsa/Webots/controllers/darwin-pi2/client)
==17043==    by 0x401F2A: main (in /home/joao/CloudPT/Bolsa/Webots/controllers/darwin-pi2/client)
==17043==  Address 0x7feffff61 is on thread 1's stack
==17043==  Uninitialised value was created by a stack allocation
==17043==    at 0x404BE6: unix_socket::sendMsg(char, double) (in /home/joao/CloudPT/Bolsa/Webots/controllers/darwin-pi2/client)
==17043== 
==17043== 
==17043== 9900 errors in context 3 of 3:
==17043== Syscall param socketcall.sendto(msg) points to uninitialised byte(s)
==17043==    at 0x5441DA2: send (send.c:28)
==17043==    by 0x404C2D: unix_socket::sendMsg(char, double) (in /home/joao/CloudPT/Bolsa/Webots/controllers/darwin-pi2/client)
==17043==    by 0x404EE8: unix_socket::sendVectorXd(Eigen::Matrix<double, -1, 1, 0, -1, 1> const&) (in /home/joao/CloudPT/Bolsa/Webots/controllers/darwin-pi2/client)
==17043==    by 0x401F2A: main (in /home/joao/CloudPT/Bolsa/Webots/controllers/darwin-pi2/client)
==17043==  Address 0x7feffff61 is on thread 1's stack
==17043==  Uninitialised value was created by a stack allocation
==17043==    at 0x404BE6: unix_socket::sendMsg(char, double) (in /home/joao/CloudPT/Bolsa/Webots/controllers/darwin-pi2/client)
==17043== 
==17043== ERROR SUMMARY: 10100 errors from 3 contexts (suppressed: 0 from 0)

错误指向的 sendMsg(const char _type, const double _value)unix_socket 类的一部分:

//...
typedef struct{
    char type;    
    double value; 
} MESSAGE;

//...
int unix_socket::sendMsg(const char _type, const double _value){
    MESSAGE msg;
    msg.type=_type;
    msg.value=_value;
    int n = send(client_sock, &msg, sizeof(msg), 0);
    if (n < 0) {
        perror("send");
        return -1;
    } 
    c_sent=msg.type;
    v_sent=msg.value;
    return 0;
}

我看不出有什么问题。未初始化的值到底在哪里?或者我应该忽略 Valgrind 报告的错误?

最佳答案

查看MESSAGE 结构:

typedef struct{
    char type;    
    double value; 
} MESSAGE;

由于数据结构对齐,value 的地址可能被迫对齐到字长的倍数地址。因此,在 MESSAGE::typeMESSAGE::value 之间填充了几个未使用的字节。这些是未初始化的字节,因此由 Valgrind 报告。

作为解决方法,您可以通过 memset() 强制初始化整个结构。

MESSAGE msg;
memset(&msg, 0, sizeof(MESSAGE));
msg.type=_type;
msg.value=_value;

关于c++ - "points to uninitialised byte(s)"Valgrind 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19364942/

相关文章:

Python Socket 编程简单的 Web 服务器,尝试从服务器访问 html 文件

C 链表 - 条件跳转或移动取决于未初始化值链表

c++ - If-else循环寻找解决方案

c++ - 使用 MHook 的系统范围的钩子(Hook)

c++ - 与 Visual Studio 2010 中的 MD 相比,无法在 MT 模式下编译简单的 Qt 程序

c++ - 将通用 protobuf 复制到堆上的新对象中

c++ - 使异步套接字服务器和客户端工作(用 C 编写)

java - 具有内部类和最终变量的线程化 Java 服务器

c - 出现在 C 数组末尾的随机字符

c++ - 重写 += 运算符 C++