首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C++ >

delete 有关问题

2013-01-22 
delete 问题#include iostreamusing namespace stdint main(){char animal[]beerchar* psint nst

delete 问题
#include <iostream>

using namespace std;

int main()
{
char animal[]="beer";
char* ps;
int n=strlen(animal);

ps = new char(n+1);
if( NULL == ps)
{
cout<<"动态空间申请失败!" << endl;
return 0;
}

strcpy(ps, animal);
*( ps+n ) = '\0';

cout<<ps << endl;

delete [] ps;
return 0;
}

写的小代码,执行到delete那出错,大神们给看看!
[解决办法]
ps = new char(n+1);应该是
ps = new char[n+1];
因为分配空间不足导致越界
[解决办法]


ps = new char[n+1];

[解决办法]
ps = new char(n+1);,改成ps = new char[n+1];

[解决办法]
ps = new char(n+1);这个只是初始化。不是分配数组

热点排行