Programme crashes while deallocating a character array

后端 未结 6 1634
刺人心
刺人心 2021-01-21 14:36

When I run the .exe being created with the below code in debug mode , it shows some assertion failure and the programme crashes But when i run the same exe created from the rele

6条回答
  •  终归单人心
    2021-01-21 15:03

    You're trying to deallocate the character-string literal "Hello". This line:

    buf = "Hello";
    

    redirects the pointer buf to point at the literal "Hello". You probably meant to do this:

    char *buf = new char[6]; //need one extra space for terminating NUL character
    strcpy(buf, "Hello");
    

提交回复
热议问题