Vector is pointing to uninitialized bytes when used in recvfrom call
- by Adam A.
In a function that I am writing I am trying to return a pointer to a vector of unsigned chars. The relevant code is below.
std::vector<unsigned char> *ret = new std::vector<unsigned char>(buffSize,'0');
int n = recvfrom(fd_, &((*ret)[0]) ,buffSize, &recvAddress, &sockSize);
//This should work too... recvfrom(fd_, ret ,buffSize, &recvAddress, &sockSize);
// display chars somehow just for testing
for(std::vector<unsigned char>::iterator it=ret->begin(); it<it->end();i++)
{
std::cout<<*it;
}
std::cout<<std::endl;
...
return ret;
When I run this through valgrind I get errors talking about how the buffer in recvfrom is pointing to uninitialized bytes. I've narrowed this down to the vector since I swapped it out for an unsigned char array and everything works fine. Any suggestions?