delete的异常
delete的错误
写的是一个记事本程序,可是我按Backspace键时。运行出错,
[code=C/C++]
if(wParam==VK_BACK)
{
if(head-> next==NULL)
MessageBox(hwnd, "最前端 ",NULL,MB_OK);
else
{
if(curp-> next == NULL)
{
prep = prep-> pre ;
delete curp;
curp = prep-> next ;
curp-> next =NULL; //debug到这一步是,可以看到curp开始指向的节点 已 //经删除,并且移向了前一节点,即实现了删除,为什么运行时还会报错
}
else //当光标不在最后的位置时,删除当前节点,移动的光标功能我还没写,所以这里可以不要看
{
prep-> next = curp-> next ;
curp-> next -> pre = prep;
delete curp;
curp = prep-> next ;
}
InvalidateRect(hwnd,NULL,TRUE);
}
break;
}
[/code]
这是那一段代码,我也debug了,明明没看出delete出错了,
你们看下我节点的定义 和上面粘贴的代码就可以了, 我也把完整的代码贴上来,
[code=C/C++]
/*3月10日,全部刷新显示*/
#include <windows.h>
#include <stdlib.h>
#include <string.h>
struct LNode
{
UINT data;
struct LNode* next;
struct LNode* pre;
};
LNode* InitClist()
{
LNode* head = new LNode;
head-> next = NULL;
head-> pre = NULL;
head-> data=0;
return head;
}
LNode* curp; //当前节点指针
LNode* prep; //前一节点指针
LNode* head; //头节点指针
LRESULT CALLBACK WndProc(HWND,UINT,WPARAM,LPARAM);
int APIENTRY WinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nCmdShow)
{
// TODO: Place code here.
HWND hwnd;
WNDCLASS wndclass;
MSG msg;
char lpszClassName[]= "Window ";
char lpszTitle[]= "NotePad ";
wndclass.style = CS_OWNDC;
wndclass.lpfnWndProc = WndProc;
wndclass.cbClsExtra = 0;
wndclass.cbWndExtra = 0;
wndclass.hInstance = hInstance;
wndclass.hIcon = LoadIcon(NULL,IDI_APPLICATION);
wndclass.hCursor = LoadCursor(NULL,IDC_ARROW);
wndclass.hbrBackground = (HBRUSH) GetStockObject(WHITE_BRUSH);
wndclass.lpszMenuName = NULL;
wndclass.lpszClassName = lpszClassName ;
if(!RegisterClass(&wndclass))
return FALSE;
hwnd= CreateWindow(lpszClassName,lpszTitle,WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT,CW_USEDEFAULT,
CW_USEDEFAULT,CW_USEDEFAULT,
NULL,NULL,
写的是一个记事本程序,可是我按Backspace键时。运行出错,
[code=C/C++]
if(wParam==VK_BACK)
{
if(head-> next==NULL)
MessageBox(hwnd, "最前端 ",NULL,MB_OK);
else
{
if(curp-> next == NULL)
{
prep = prep-> pre ;
delete curp;
curp = prep-> next ;
curp-> next =NULL; //debug到这一步是,可以看到curp开始指向的节点 已 //经删除,并且移向了前一节点,即实现了删除,为什么运行时还会报错
}
else //当光标不在最后的位置时,删除当前节点,移动的光标功能我还没写,所以这里可以不要看
{
prep-> next = curp-> next ;
curp-> next -> pre = prep;
delete curp;
curp = prep-> next ;
}
InvalidateRect(hwnd,NULL,TRUE);
}
break;
}
[/code]
这是那一段代码,我也debug了,明明没看出delete出错了,
你们看下我节点的定义 和上面粘贴的代码就可以了, 我也把完整的代码贴上来,
[code=C/C++]
/*3月10日,全部刷新显示*/
#include <windows.h>
#include <stdlib.h>
#include <string.h>
struct LNode
{
UINT data;
struct LNode* next;
struct LNode* pre;
};
LNode* InitClist()
{
LNode* head = new LNode;
head-> next = NULL;
head-> pre = NULL;
head-> data=0;
return head;
}
LNode* curp; //当前节点指针
LNode* prep; //前一节点指针
LNode* head; //头节点指针
LRESULT CALLBACK WndProc(HWND,UINT,WPARAM,LPARAM);
int APIENTRY WinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nCmdShow)
{
// TODO: Place code here.
HWND hwnd;
WNDCLASS wndclass;
MSG msg;
char lpszClassName[]= "Window ";
char lpszTitle[]= "NotePad ";
wndclass.style = CS_OWNDC;
wndclass.lpfnWndProc = WndProc;
wndclass.cbClsExtra = 0;
wndclass.cbWndExtra = 0;
wndclass.hInstance = hInstance;
wndclass.hIcon = LoadIcon(NULL,IDI_APPLICATION);
wndclass.hCursor = LoadCursor(NULL,IDC_ARROW);
wndclass.hbrBackground = (HBRUSH) GetStockObject(WHITE_BRUSH);
wndclass.lpszMenuName = NULL;
wndclass.lpszClassName = lpszClassName ;
if(!RegisterClass(&wndclass))
return FALSE;
hwnd= CreateWindow(lpszClassName,lpszTitle,WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT,CW_USEDEFAULT,
CW_USEDEFAULT,CW_USEDEFAULT,
NULL,NULL,