先看看我的结构体,再看看我的链表的函数,有问题!
struct SmallNotion
{
char *m_strSNotion;//字符串
double m_fReal;//确信度
int m_bSNFlag;//访问搜索标志
//初始值,-1则已访问了不匹配,1则访问了匹配,0则没访问
struct SmallNotion* next;//指向下一个节点
};
{
char *m_strNotion;//字符串
int m_iNFlag;//访问搜索标志
//初始值,-1则已访问了不匹配,1则访问了匹配,0则没访问
struct SmallNotion* pSNChain;//子链表的头指针
struct Notion* next;//指向下一个节点
};
//////
............
//我建立链表时开辟的空间,这没问题!
pNDBTemp = new struct Notion;
pNDBTemp->m_strNotion = new char[80];
..............
pSNRealTemp = new struct SmallNotion;
pSNRealTemp->m_strSNotion = new char[80];
..............
///////
我的释放空间函数
void CNOTIONANALYSEDlg::FreeSNB(SmallNotion *snPoint)
{
struct SmallNotion* snp;
while(snPoint)
{
snp = snPoint->next;
delete snPoint->m_strSNotion;
snPoint->m_strSNotion = NULL;
snPoint->next = NULL;
snPoint = snp;
}
}
void CNOTIONANALYSEDlg::FreeNDB(Notion *nPoint)
{
struct Notion* np;
while(nPoint)
{
np = nPoint->next;
delete nPoint->m_strNotion;
nPoint->m_strNotion = NULL;
nPoint->next = NULL;
delete nPoint;
nPoint = np;
}
}
//以下报错.不能正常退出!
if(pNDBTemp != NULL) FreeNDB(pNDBTemp);
if(pSNRealTemp != NULL)FreeSNB(pSNRealTemp);
高手给点意见吧!我按时给分,不够再加!
我实在搞不清楚!说详细点,先谢了!
struct SmallNotion
{
char *m_strSNotion;//字符串
double m_fReal;//确信度
int m_bSNFlag;//访问搜索标志
//初始值,-1则已访问了不匹配,1则访问了匹配,0则没访问
struct SmallNotion* next;//指向下一个节点
};
{
char *m_strNotion;//字符串
int m_iNFlag;//访问搜索标志
//初始值,-1则已访问了不匹配,1则访问了匹配,0则没访问
struct SmallNotion* pSNChain;//子链表的头指针
struct Notion* next;//指向下一个节点
};
//////
............
//我建立链表时开辟的空间,这没问题!
pNDBTemp = new struct Notion;
pNDBTemp->m_strNotion = new char[80];
..............
pSNRealTemp = new struct SmallNotion;
pSNRealTemp->m_strSNotion = new char[80];
..............
///////
我的释放空间函数
void CNOTIONANALYSEDlg::FreeSNB(SmallNotion *snPoint)
{
struct SmallNotion* snp;
while(snPoint)
{
snp = snPoint->next;
delete snPoint->m_strSNotion;
snPoint->m_strSNotion = NULL;
snPoint->next = NULL;
snPoint = snp;
}
}
void CNOTIONANALYSEDlg::FreeNDB(Notion *nPoint)
{
struct Notion* np;
while(nPoint)
{
np = nPoint->next;
delete nPoint->m_strNotion;
nPoint->m_strNotion = NULL;
nPoint->next = NULL;
delete nPoint;
nPoint = np;
}
}
//以下报错.不能正常退出!
if(pNDBTemp != NULL) FreeNDB(pNDBTemp);
if(pSNRealTemp != NULL)FreeSNB(pSNRealTemp);
高手给点意见吧!我按时给分,不够再加!
我实在搞不清楚!说详细点,先谢了!
解决方案 »
- 关于WinCE中使用滚动条时逻辑坐标与设备坐标转换的问题
- 诸位大神!! 为什么我这个程序 的这条直线 不能变化大小啊!!!
- 如何创建自己指定好友的聊天QQ对话框
- 我在头文件中#include <vector>,但是编译器不认vector怎么办??
- 请问如何在IE中加入工具栏按钮(toolbar button)
- 高分求救!!界面显示问题,真是奇怪得很,请赐教!!
- 一个CDialog窗体,只要用popoup(stytle)方式打开就报错退出,贴出类结构,大家看看有什么问题?
- 请教,如何改变ListCtrl的一行的底色啊?
- VC中用什么控件显示磁盘目录和文件?
- DrawMenuBar有什么用
- 請高手賜教了:在odbc api中有一些函數並不支持,怎麽才能用到這些函數了,任何途徑都可以。。
- 来看看呀!图象采集问题
你可以调试看看什么地方出问题啊,或把代码写得详细点。下面的两个函数也有点问题:
void CNOTIONANALYSEDlg::FreeSNB(SmallNotion *snPoint)
{
struct SmallNotion* snp;
while(snPoint)
{
snp = snPoint->next;
delete snPoint->m_strSNotion;//最好是delete []snPoint->m_strSNotion,养成好习惯。这里不会错是因为char是基本的类型,如果是对象,delete []可以对每一个对象调用析构函数
snPoint->m_strSNotion = NULL;
snPoint->next = NULL;
delete snPoint; //漏掉了
snPoint = snp;
}
}
void CNOTIONANALYSEDlg::FreeNDB(Notion *nPoint)
{
struct Notion* np;
while(nPoint)
{
np = nPoint->next;
delete nPoint->m_strNotion;//同上
if (nPoint->pSNChain)
{
FreeNDB(nPoint->pSNChain);
}
delete nPoint;
nPoint = np;
}
}
while( ptail != phead )
{
if( ptail->plast )
{
ptail = ptail->plast;
delete ptail->pnext;
ptail->pnext = NULL;
}
}
{
.............//确定一次添加一个节点(Notion).
pNDBTemp = new struct Notion;
pNDBTemp->m_strNotion = new char[80];
memcpy(pNDBTemp->m_strNotion, (LPCTSTR)m_strNotionEdit, m_strNotionEdit.GetLength()+1);
pNDBTemp->pSNChain = NULL;
pNDBTemp->m_iNFlag = 0;
pNDBTemp->next = pNDB;
pNDB = pNDBTemp;
int m_iItemCount = m_cSNotionList.GetItemCount();
if(m_iItemCount)
{
for(int m_iListNum = 0; m_iListNum < m_iItemCount; m_iListNum ++)
{
CString m_strTemp1, m_strTemp2;
pSNRealTemp = new struct SmallNotion;
pSNRealTemp->m_strSNotion = new char[80];
m_strTemp1= m_cSNotionList.GetItemText(m_iListNum, 0);
memcpy(pSNRealTemp->m_strSNotion, (LPCTSTR)m_strTemp1, m_strTemp1.GetLength()+1);
pSNRealTemp->m_bSNFlag = 0;
m_strTemp2 = m_cSNotionList.GetItemText(m_iListNum, 1);
pSNRealTemp->m_fReal = atof(m_strTemp2);//字符转为数
pSNRealTemp->next = pNDBTemp->pSNChain;
pNDBTemp->pSNChain = pSNRealTemp;
}
}//添加到链表
.............
}
请帮我看看!
如果想释放掉整个连表就直接用sunyard()的方法FreeNDB(PNDB)
void CNOTIONANALYSEDlg::FreeNDB(Notion *nPoint)
{
struct Notion* np;
while(nPoint)
{
np = nPoint->next;
delete nPoint->m_strNotion;//同上
if (nPoint->pSNChain)
{
//FreeNDB(nPoint->pSNChain);//他误写了
FreeSNB(nPoint->pSNChain);//修改
}
delete nPoint;
nPoint = np;
}
}
其中FreeSNB为 sunyard()修改后的方法
: CDialog(CNOTIONANALYSEDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CNOTIONANALYSEDlg)
...........
//}}AFX_DATA_INIT
// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
............
struct SmallNotion* pSNConclusion = NULL;//结论链表的头指针
struct SmallNotion* pSNReal = NULL;//输入的已知事实(元概念链表)的头指针
struct Notion* pNDB = NULL;//概念库(概念链表)的头指针
struct Notion* pNUsed = NULL;//已使用的概念链表的头指针
struct SmallNotion* pSNRealTemp = NULL;
struct Notion* pNDBTemp = NULL;
.............
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}
初始化为NULL.应该没问题.谢谢 tiankong(sd).再帮我看看.我等了一上午了!
跟帖呀!
2。用修改后函数释放掉链表只要
if(pNDBTemp != NULL) FreeNDB(pNDBTemp);一句就会把整个连表释放(包括连表接点和连表接点为头的子连表都释放掉),
不要在写if(pSNRealTemp != NULL)FreeSNB(pSNRealTemp);来释放子连表了
1.我还没有对节点删除,只添加!
2.我的pSNRealTemp只是用来生成主链表时的交换,可以为局部变量的.
所以没有循环来删除!
谢谢 tiankong(sd).!
我再综合的试试!
再看看下面:
void CNOTIONANALYSEDlg::FreeNDB(Notion *nPoint)
{
struct Notion* np;
while(nPoint)
{
np = nPoint->next;//F5后,什么也没做,就点退出,光标最终停在此,
//报告显示c0000005:Access Violation
//我可是什么也没做一执行就按OK退出的
delete []nPoint->m_strNotion;
if (nPoint->pSNChain)
{
FreeNDB(nPoint->pSNChain);
}
nPoint->m_strNotion = NULL;
nPoint->next = NULL;
delete nPoint;
nPoint = np;
}
}
高手请再看看,给点提示.
你注意一下调用FreeNDB的指针是否有误
应为:
FreeSNB(nPoint->pSNChain);//修改
我认为刚初始化.
struct SmallNotion* pSNConclusion = NULL;//结论链表的头指针
struct SmallNotion* pSNReal = NULL;//输入的已知事实(元概念链表)的头指针
struct Notion* pNDB = NULL;//概念库(概念链表)的头指针
struct Notion* pNUsed = NULL;//已使用的概念链表的头指针
struct SmallNotion* pSNRealTemp = NULL;
struct Notion* pNDBTemp = NULL;
再点退出,应该不执行下面的!
if(pNDB != NULL) FreeNDB(pNDB);//error
if(pNDBTemp != NULL) FreeNDB(pNDBTemp);
if(pNUsed != NULL) FreeNDB(pNUsed);
if(pSNReal != NULL)FreeSNB(pSNReal);
if(pSNRealTemp != NULL)FreeSNB(pSNRealTemp);
if(pSNConclusion != NULL) FreeSNB(pSNConclusion);
再看看吧,我实在没办法了.
还有两天就检查了.
这个问题对这个程序来说太大了.
救救我!
if(pNDBTemp != NULL) FreeNDB(pNDBTemp);
if(pSNReal != NULL)FreeSNB(pSNReal);
if(pSNRealTemp != NULL)FreeSNB(pSNRealTemp);都是没必要的
报告显示c0000005:Access Violation
我可是什么也没做,一执行就按退出的
我认为刚初始化.接着马上就退出,应该不执行下面的!
if(pNDB != NULL) FreeNDB(pNDB);
怎么回事?
char *p=new char[10];
//process code
...
delete []p; //delete之后,p指向的内存被释放了,但指针p还在,而且p也不等于NULL,
//此时,p成了野指针。
while(p) //所以,执行{ }内代码。
{
....
}在看看你的while语句:
while(snPoint)
{
snp = snPoint->next;
delete snPoint->m_strSNotion;
snPoint->m_strSNotion = NULL;
snPoint->next = NULL;
snPoint = snp;
}
snPoint始终不等于NULL,所以死循环。
但运行后,什么也没做,就点退出,直接退出了.
我认为刚初始化.接着马上就退出,应该不执行下面的!
if(pNDB != NULL) FreeNDB(pNDB);
这时,pNDB = NULL,怎么回事?
但还是好像执行了FreeNDB(pNDB);
snPoint最后会是NULL,
给你误导,很抱歉!
特感谢 sunyard() tiankong(sd) devzhao(疯狂IT)
大哥 ColderRain(莫走重辄路) 帮我解决了实质的问题
也感谢其他的友情关注!