C指向指针的指针的问题,非常感谢!!!

[复制链接]
查看11 | 回复3 | 2008-10-31 22:13:48 | 显示全部楼层 |阅读模式
先看一下代码:
#include
using namespace std;
void main()
{
char** str = new char*[3];
str[0] = new char[20];
str[1] = new char[20];
str[2] = new char[20];
str[0] = "Nihao";
str[1] = "NihaoMa";
str[2] = "NihaoYa";
int i;
for(i=0; i<3; i++)
cout<<str<<endl;
char** str1 = new char*[2];
str1[0] = str[0];
str1[1] = str[2];
for(i=0; i<2; i++)
cout<<str1<<endl;
// delete str[1];
delete [] str;
str = str1;
/* for(i=0; i<2; i++)
delete str;*/
delete []str;
}
上面代码可以运行,
但是,一旦将两个注释处的其中任意一处去掉,将会出现问题,
VC++弹出一个对话框,显示如下:
Debug Assertion Failed!
Program: ...\数据库实验\NewDBS\Debug\MYDBS.exe
File: dbgheap.c
Line: 1044
Expression: _CrtIsValidHeapPointer(pUserData)
For information on how your program can cause assertion
failure, see the Visual C++ documentation on asserts.
(Press Retry to debug the application)
麻烦各位帮我看看这是怎么一回事?

回复

使用道具 举报

千问 | 2008-10-31 22:13:48 | 显示全部楼层
将str[0] = "Nihao"; str[1] = "NihaoMa"; str[2] = "NihaoYa"; 改成strcpy(str[0], "Nihao");strcpy(str[1], "NihaoMa");strcpy(str[2], "NihaoYa");就OK啦!不客气!...
回复

使用道具 举报

千问 | 2008-10-31 22:13:48 | 显示全部楼层
你好你好吗你好呀这程序很搞笑...
回复

使用道具 举报

千问 | 2008-10-31 22:13:48 | 显示全部楼层
delete []是与new[]配对的,你用了new[]声明,只能用delete []删除整个数组空间,而不能用delete一个一个删除。...
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

主题

0

回帖

4882万

积分

论坛元老

Rank: 8Rank: 8

积分
48824836
热门排行