new char()和new char[]的区别
这样的代码运行正常
#include "stdafx.h "
#include "memory.h "
void main()
{
char *apple = new char[17];
char *temp = "apple and orange ";
memcpy(apple,temp,17);
delete apple;
apple = NULL;
}
若将main中第一行改为:char *apple = new char(17) ,delete apple就会出错,这个是什么原因?new char()和new char[]到底有什么区别?new char(17)难道是在全局数据区申请17个BYTE?
[解决办法]
new char[17] 动态分配17个字节的字符数组
new char(17) 动态分配一个字符,初试值为17