Dereferencing deleted pointers always result in an Access Violation?

后端 未结 6 2063
情话喂你
情话喂你 2020-11-30 13:51

I have a very simple C++ code here:

char *s = new char[100];
strcpy(s, \"HELLO\");
delete [] s;
int n = strlen(s);

If I run this code from

6条回答
  •  南方客
    南方客 (楼主)
    2020-11-30 14:35

    I also want to know if it's possible to stably reproduce the Access Violation crash caused from accessing deleted area?

    Instead of plain delete you could consider using an inline function that also sets the value of the deleted pointer to 0/NULL. This will typically crash if you reference it. However, it won't complain if you delete it a second time.

    Is this kind of crash rare in real-life?

    No, this kind of crash is probably behind the majority of the crashes you and I see in software.

提交回复
热议问题