The first example does not work when you go to delete the pointer. The program either hangs when I add the null terminator or without it I get:
Debug Assertion Fai
In the first example, you have caused a memory leak.
Your variable at
is a pointer to a memory address, not the string itself. When you assign the address of "tw"
to the pointer, you have lost the original address that you got with new
. at
now points to an address that you did not allocate with new
, so you cannot delete
it.
If you think of pointers as integers, it will probably make more sense. I've assigned arbitrary numbers as addresses for the sake of discussion.
char *at = new char[3]; // 0x1000
at = "tw"; // 0x2000
at[2] = '\0'; // set char at 0x2002 to 0
delete at; // delete 0x2000 (whoops, didn't allocate that!)