Because a char*
isn't a string. It's just a pointer to some character, with the convention that there might be more characters to follow and that after the last one there is a '\0'
.
A string literal in C (and thus in C++) like "abc"
is just an array of characters, with the compiler silently adding a '\0'
. When you assign an array to a pointer, the array silently converts a pointer to the first element. The result is that
at = "tw";
means, the pointer at
is assigned the address of the first character in the string literal "tw"
. By this, it will lose its old value. Since this was the address of a dynamically allocated character array, you are leaking this array.
When you later assign to a character in the array at
now points to, you are assigning a new value to some character in the string literal. That's invoking undefined behavior and the program hanging or crashing immediately is probably the best that could happen to you when you do this. (On many platforms you're writing to read-only memory doing so.)
Later you pass at
to delete[]
(and not delete
, since you called new[]
, not new
). In doing so, you pass it the address of the string literal, instead of the allocated character array. This will, of course, mess up the heap manager. (VC's runtime library catches this in Debug mode.)
std::strcpy
, on the other hand, copies a string character by character from one array to another array. No pointers will be changed, only pieces of memory are copied. The pointer to the target array still points to the target array afterwards, only the data in that array has changed.
Let me add this: As a beginner in C++, you should use std::string
, rather than C strings. That does all the dirty work for you and has sane semantics.
delete[]
– Barterat = "tw";
and thenat[2] = '\0';
is redundant."tw"
creates a string literal that is already null-terminated. The memory for the string"tw"
looks like[ 't' | 'w' | '\0' ]
. Not only that, butat[2] = '\0';
will also result in undefined behavior;"tw"
creates a string literal, which is a read-only string which is unwritable, so writing to this read-only string literal will invoke undefined behavior. To actually assign something this way, you'd have to doconst char *at = "tw";
which will create a string literal and haveat
point to the same location. – Huguenot