Programme crashes while deallocating a character array

后端 未结 6 1632
刺人心
刺人心 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:10

    When you do this:

    buf = "Hello";
    

    You're basically changing the pointer value (memory address) at which buf points to a read-only memory area, because "Hello" is a string literal and therefore is stored in read-only memory.

    Then you attempt to free that memory, hence the crash.

    Also, "Hello" is 6-bytes long, not 5.

提交回复
热议问题