I am a beginner to C/C++, and I came across the following segment of code:
#define MAX_MSG 1000
char *szBuf = new char[MAX_MSG];
char *szBufRaw = new char[MAX_MSG];
memset(szBuf, ‘\0’, strlen(szBuf));
memset(szBufRaw, ‘\0’, strlen(szBufRaw));
I've read the tutorial about memset here:
http://www.java-samples.com/showtutorial.php?tutorialid=591
and I believe the above code is correct, but the original author of the code believes there is a bug within it, could anybody give me a hint? Thanks in advance.
strlen(szBuf)(and strlen(szBufRaw)) will return correct result only on valid strings. You should pass MAX_MSG instead.
strlen() looks for the first null value in the array, which means that you won't memset the right amount of bytes in memory.
Use MAX_MSG instead to set the entire array to null.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With