局部变量,全局delete

zhangwuji154 2010-03-07 04:53:47
void CPointerDlg::OnButton1()
{
// TODO: Add your control notification handler code here
int* p[10];
for (int i=0; i<10; i++)
{
int* j = new int(i);
p[i] = j;
}
...
delete [] p;
}

在for循环里申明/new的变量,想在外边delete,
delete [] p;这句有个warning,这样写对吗
...全文
108 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
白云飘飘飘 2010-03-07
  • 打赏
  • 举报
回复
void CPointerDlg::OnButton1()
{
// TODO: Add your control notification handler code here
int* p[10];
for (int i=0; i <10; i++)
{
int* j = new int(i);
p[i] = j;
}
for (int i=0; i <10; i++)
{
delete p[i];
}
}
zyyoung 2010-03-07
  • 打赏
  • 举报
回复
不能对数组使用 delete,因此编译器将数组转换为指针。

1,649

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC 非技术类
社区管理员
  • 非技术类社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧