#include <iostream>
#include <string>
int main()
{
char a = 'a';
char* cstr = new char[5];
cstr = &a;
std::cout << *cstr<<std::endl;
delete[] cstr;
}
是不是因为cstr的地址是new出来的(堆),后面把cstr指向栈里的地址a,改变了指针地址,delete不能释放栈里的内存
#include <iostream>
#include <string>
int main()
{
char a = 'a';
char* cstr = new char[5];
cstr = &a;
std::cout << *cstr<<std::endl;
delete[] cstr;
}
是不是因为cstr的地址是new出来的(堆),后面把cstr指向栈里的地址a,改变了指针地址,delete不能释放栈里的内存