The vector indicates uninitialized bytes when used in a recvfrom call

In the function I'm writing, I'm trying to return a pointer to a vector of unsigned characters. The corresponding code is below.

std::vector<unsigned char> *ret = new std::vector<unsigned char>(buffSize,'0');
//Due to suggestions...
int n = recvfrom(fd_, ret, buffSize, &recvAddress, &sockSize);
//Forgot to include this in the original
ret->resize(n);
// display chars somehow just for testing
for(std::vector<unsigned char>::iterator it=ret->begin(); it<ret->end();it++)
{
    std::cout<<*it;
}
std::cout<<std::endl;
...
return ret;

When I run this through valgrind, I get errors telling how the buffer in recvfrom points to uninitialized bytes. I narrowed it down to a vector since I replaced it with an unsigned char array and everything works fine. Any suggestions?

Edit 1: Fixed some part of the code, doing this from memory / notes with the problem I was working with. The reason I started using valgrind was because I was getting a segmentation error in this place. I'll double check what I'm doing tomorrow.

+3
source share
2

:

int n = recvfrom(fd_, ret, buffSize, &recvAddress, &sockSize);

:

int n = recvfrom(fd_, &(*ret)[0], buffSize, 0, &recvAddress, &sockSize);

std::vector, (, ). , 3 , , . , ( std::vector<char> *) void* ( recvfrom).

, , , , , &(*ret)[0]. , [] , *.

( , flags recvfrom() - , , 0 .)

+7

n recvfrom(), , . , valgrind , .

0

Source: https://habr.com/ru/post/1783321/


All Articles