Vector is pointing to uninitialized bytes when used in recvfrom call
Posted
by
Adam A.
on Stack Overflow
See other posts from Stack Overflow
or by Adam A.
Published on 2011-01-04T01:25:12Z
Indexed on
2011/01/04
1:53 UTC
Read the original article
Hit count: 560
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?
© Stack Overflow or respective owner