Sign in to follow this  

delete problem after calling recvfrom()

This topic is 3315 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

This topic is 3315 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.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this