Why must a pointer to a char array need strcpy to assign characters to its array and double quotes assignment will not work?

前端 未结 9 1781
耶瑟儿~
耶瑟儿~ 2021-02-14 09:23

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

9条回答
  •  不知归路
    2021-02-14 09:45

    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!)
    

提交回复
热议问题