~~~~~~~~~~~~为什么堆空间没有被破坏~~~~~~~~~~~
#include<iostream>
#include <string>
#include <cstring>
using namespace std;
int main()
{
string str1(1048576,' ');
char* s = new char[2];
strcpy(s, str1.c_str());
delete[]s;
return 0;
}
#include<iostream>
#include <string>
using namespace std;
int main()
{
string str1;
char* s = new char[2];
cin >> str1;
strcpy(s, str1.c_str());
delete [] s; //这一步可能会崩溃哦,你没有delete 本身程序就错了
return 0;
}
//你再这样试试
#include<iostream>
#include <string>
using namespace std;
int main()
{
string str1;
char* s = new char[2];
char* s2 = new char[2];
cin >> str1;
strcpy(s, str1.c_str());
delete [] s; //这一步可能会崩溃哦,你没有delete 本身程序就错了
delete []s2;
return 0;
}