Dereferencing deleted pointers always result in an Access Violation?

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

    Accessing memory through a deleted pointer is undefined behavior. You can't expect any reliable/repeatable behavior.

    Most likely it "works" in the one case because the string is still "sitting there" in the now available memory -= but you cannot rely on that. VS fills memory with debug values to help force crashes to help find these errors.

提交回复
热议问题