Dereferencing deleted pointers always result in an Access Violation?

后端 未结 6 2067
情话喂你
情话喂你 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

    Dereferencing a pointer after delete is undefined behavior - anything can happen, including but not limited to:

    • data corruption
    • access violation
    • no visible effects

    exact results will depend on multiple factors most of which are out of your control. You'll be much better off not triggering undefined behavior in the first place.

提交回复
热议问题