Advertisement Jump to content
Sign in to follow this  

delete problem after calling recvfrom()

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

If you intended to correct an error in the post then please contact us.

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 this post

Link to post
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.

Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement

Important Information

By using, you agree to our community Guidelines, Terms of Use, and Privacy Policy. is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!