# delete problem after calling recvfrom()

This topic is 3391 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

this whats happening : char *pcName = new char[ strQueryName.length() ]; -> length is four recvfrom( socketUser, pcName, sizeof(pcName), 0, (SOCKADDR*)&sockClientAddress, &AddressSize ); delete [] pcName; it gives me the HEAP CORRUPTION DETECTED: CRT detected that the application wrote to memory after end of heap buffer. why is this? it has to do with the recvfrom() function because i can easily delete it before the call, with out any errors

##### Share on other sites
Your use of sizeof() is incorrect. The result of sizeof() on a pointer is a fixed value. You want to use the size of the buffer - which you would have to track seperately. In C++, we can use std::vector as a temporary buffer. std::vector is guaranteed to be contigious, we can obtain a pointer to the first element by using &vec.front(), and the size by use vec.size().
#include <vector>//...std::vector<char> buffer(queryName.length());recvfrom( socketUser, &buffer.front(), buffer.size(), 0, (SOCKADDR*)&sockClientAddress, &AddressSize );

Finally, note the lack of "delete []" calls. std::vector<> does that for us.