![]() |
#2
alice_usnet2016-04-09 16:30
|

#include<iostream>
#include<cstring>
#include<cstdlib>
using namespace std;
class str
{ private:
char *p;
public:
str(char *s)
{
p = new char[strlen(s) + 1];
if (p == NULL)
{
cout << "动态内存建立不成功,程序终止" << endl;
exit(0);
}
strcpy(p,s);
cout << "成功创造了对象" << endl;
cout << "字符数组的内容是:" << endl;
}
~str()
{
if (p)
delete[]p;
cout << "撤销对象" << endl;
}
};
void main()
{
str m("创建动态内存");
cout << "main()函数" << endl;
}
将 strcpy(p,s)改为strcpy_s(p,sizeof(p),s)后,表面上是没问题,但是运行起来就出现问题,无法运行。
前辈们求解。