"PSN" <pr********@gmail.com> wrote in message
news:11**********************@g14g2000cwa.googlegr oups.com...
Hi all ..
here is the code which crashes at the time of freeing the pointer
memory.
************************************
char *complete_command;
complete_command = new char[strlen(command) + strlen(" >> moto_msg")];
strcpy(complete_command, command);
strcat (complete_command, " >> moto_msg");
<SOME IRRELEVANT FUNCTIONS/CODE>
if (flag != 1)
ULTI_ERROR = -6;
delete [] complete_command; // CRASHES HERE
**************************************
Can someone explain me what am i doing wrong ??
You're writing past the end of your allocated memory.
After that, the behavior of 'delete[]' (and anything else
in your program) is undefined, so anything could happen.
Both 'strcpy()' and 'strcat()' append a terminator ('\0')
after they copy. You didn't provide enough storage for
both your strings and the terminator.
complete_command = new char[strlen(command) + strlen(" >> moto_msg") + 1];
You can eliminate all the headaches of managing your own string
memory by using the std::string type instead.
-Mike