Is that safe to do something like this:
char* charArray = new char[10];
strcat(charArray, "qwertyuiop");
charArray[3] = '\0';
delete [] charArray;
Will everything be deleted? Or that what is after \0 won’t be? I don’t know if I’m leaving garbage.
EDIT: should be strcpy
If you wanted to write
strcpyinstead ofstrcat, then that is safe and correct. But it seems you’ve a misconception aboutdelete [] charArray. It doesn’t delete characters, it deletes the memory pointed to bycharArray. The memory even afterdelete [] charArraymight contain those characters, it is not guaranteed though.However, if you really wanted to write
strcat, and it is not a typo, then your code invokes undefined behavior, becausecharArraycontains garbage to whichstrcatwill attempt to concatenate the second string.