I am a c++ newbie and just try to write some code to have experiment myself.
Recently I have encountered a problem that I cannot debug.
char acExpReason[128];
char acReason[] = "An error message is information displayed when an unexpected condition occurs, usually on a computer or other device. On modern operating systems.";
memcpy(acExpReason, acReason, sizeof(acExpReason));
std::string strExpReason(acExpReason);
I use VS2005 to add breakpoints to every line to debug.
when it reaches the breakpoint on the second line, the variable name and value info in the Autos is:
acReason 0x00f6f78c "ÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌ" char [147]
acExpReason 0x00f6f828 "ÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌ̺" char [128]
when it reaches the breakpoint on the third line, the variable name and value info in the Autos is:
acExpReason 0x00f6f828 "ÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌ̺" char [128]
acReason 0x00f6f78c "An error message is information displayed when an unexpected condition occurs, usually on a computer or other device. On modern operating systems." char [147]
when it reaches the breakpoint on the fourth line, the variable name and value info in the Autos is:
acExpReason 0x00f6f828 "An error message is information displayed when an unexpected condition occurs, usually on a computer or other device. On modern ÌÌÌÌÌÌÌ̺" char [128]
acReason 0x00f6f78c "An error message is information displayed when an unexpected condition occurs, usually on a computer or other device. On modern operating systems." char [147]
strExpReason Bad Ptr
after the last line is executed, the info in Autos is:
acExpReason 0x00f6f828 "An error message is information displayed when an unexpected condition occurs, usually on a computer or other device. On modern ÌÌÌÌÌÌÌ̺" char [128]
acReason 0x00f6f78c "An error message is information displayed when an unexpected condition occurs, usually on a computer or other device. On modern operating systems." char [147]
strExpReason "An error message is information displayed when an unexpected condition occurs, usually on a computer or other device. On modern ÌÌÌÌÌÌÌ̺"
basically what my code wants to do is just to have a complete line of msg which is stored in acReason[], and also there is a copy of the complete msg in fixed length(here is 128).
but I don't know why acExpReason and strExpReason(the string version of the acExpReason) will end with some strange characters "ÌÌÌÌÌÌÌ̺" which I don't want(as I will use this string to compare with other string later).
I tried using memcpy, strcpy, and strncpy and they all ended up having that set of strange characters in the end of the string.
Can anyone help? Many thanks in advance.
In C all string functions like strcpy and also the constructor for c++'s std::string
take char*
as a parameter but the char*
must be terminated with a byte containing `\0`.
acExpReason does not have a zero ending it so all the string functions look for the next 0 byte in memory. acReason does have a trailing `\0`. Normal strcpy would work as it also copies the 0 however as @VladLazarenko says the buffer size is too small which will cause all memory to be overwritten.
To make memcpy work you need to copy one less byte than the buffer and make the last byte of the buffer 0. e.g.
memcpy(acExpReason, acReason, sizeof(acExpReason)-1);
acReason[sizeof(acExpReason)-1] = 0;