Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/sockets/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 赋值时的Valgrind错误_C++_Sockets_Debugging_Gdb_Valgrind - Fatal编程技术网

C++ 赋值时的Valgrind错误

C++ 赋值时的Valgrind错误,c++,sockets,debugging,gdb,valgrind,C++,Sockets,Debugging,Gdb,Valgrind,我被难住了。我正在为一个学校作业开发一个小型数据服务器,该服务器应该在本次迭代中通过套接字进行通信。大部分都是有效的,但我不太明白valgrind在抱怨什么,但下面是全部内容 瓦尔格林说 Conditional jump or move depends on uninitialised value(s) at 0x4C2ABD9: strlen (in /usr/lib64/valgrind/vgpreload_memcheck-amd64-linux.so) by 0x510F0EF:

我被难住了。我正在为一个学校作业开发一个小型数据服务器,该服务器应该在本次迭代中通过套接字进行通信。大部分都是有效的,但我不太明白valgrind在抱怨什么,但下面是全部内容

瓦尔格林说

 Conditional jump or move depends on uninitialised value(s)
 at 0x4C2ABD9: strlen (in /usr/lib64/valgrind/vgpreload_memcheck-amd64-linux.so)
 by 0x510F0EF: std::basic_string<char, std::char_traits<char>, std::allocator<char>             >::basic_string(char const*, std::allocator<char> const&) (in /usr/lib64/libstdc++.so.6.0.17)
 by 0x4078C9: netreq::cread() (netreq.c:120)
 by 0x402585: event(void*) (simpleclient.C:166)
条件跳转或移动取决于未初始化的值
在0x4C2ABD9:strlen(在/usr/lib64/valgrind/vgpreload_memcheck-amd64-linux.so中)
通过0x510F0EF:std::basic_string::basic_string(char const*,std::allocator const&)(在/usr/lib64/libstdc++.so.6.0.17中)
通过0x4078C9:netreq::cread()(netreq.c:120)
通过0x402585:事件(void*)(simpleclient.C:166)
gdb似乎在过程中的同一点接收到一个信号管道

这就是它所抱怨的功能

string netreq::cread()
{
    char buf[255];

    if(read(fd,buf, 255) < 0)
         cout << "I cants read dat right, sorry"<<endl;

    return (string)buf; //this is line 120 in netreq.c
}
string netreq::cread()
{
char-buf[255];
如果(读取(fd,buf,255)<0)
cout
read()
不会通过
\0
来终止数组以指示结束。您应该自己完成

int len;
if ((len = read(fd, buf, 255)) < 0) {
/* ... */
} else {
    buf[len] = '\0';
}

return string(buf);
int-len;
如果((len=read(fd,buf,255))<0){
/* ... */
}否则{
buf[len]='\0';
}
返回字符串(buf);

read
函数的作用是什么?保存
read()
调用的结果。如果函数没有失败,您将需要它。在这个过程中,您可能希望将读取限制为254。@402,可能是POSIX函数
read
+1,但请注意,如果
read()函数
max填充缓冲区。传递的max len应该是
sizeof(buf)-1
。这似乎解决了问题,谢谢!我想我之前尝试类似于此的方法并没有解决问题。