删除char*崩溃程序

Ant*_*ndo 0 c++ char

char* str = "hello myname is Stack";
delete[] str;
Run Code Online (Sandbox Code Playgroud)

为什么我的程序崩溃了?是否在堆栈上分配了char*str?

Luc*_*ore 5

因为delete[]你没有分配的任何东西都是违法的new[].而已.

不,str指向堆栈上的某些东西,而是指向字符串文字,它是不可变的,通常驻留在只读段中.