delete的错误

IT_yangjing 2012-03-10 09:51:58
写的是一个记事本程序,可是我按Backspace键时。运行出错,

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;
}

这是那一段代码,我也debug了,明明没看出delete出错了,

你们看下我节点的定义 和上面粘贴的代码就可以了, 我也把完整的代码贴上来,

/*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,
hInstance,
NULL);
head=InitClist();
curp=head;
prep=NULL;
ShowWindow(hwnd,nCmdShow);
UpdateWindow(hwnd);
while(GetMessage(&msg,NULL,0,0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return 0;
}

LRESULT CALLBACK WndProc(HWND hwnd,UINT message,WPARAM wParam ,LPARAM lParam)
{
int xchar=0,ychar=0; //存放当前串所占的宽度和高度
int num;
LNode* print;
static BOOL Word= TRUE;
RECT rtClient;
HDC hdc;
HCURSOR hCursor;
TEXTMETRIC tm;
PAINTSTRUCT ps;
SIZE size;
static char buf[2];
static int nNumChar=0;
static int nArrayPos=0;
switch(message)
{
case WM_CREATE:
hdc = GetDC(hwnd);
GetTextMetrics(hdc,&tm);
ReleaseDC(hwnd,hdc);
CreateCaret(hwnd,NULL,2,tm.tmHeight-2 ); //创建一个标记,比字符高度小2个逻辑单位
ShowCaret(hwnd); //显示标记
break;
case WM_PAINT: //应增加 改变窗口大小就全部重绘的代码
hdc=BeginPaint(hwnd,&ps);
SetTextColor(hdc,RGB(255,0,0));

GetTextMetrics(hdc,&tm);
ychar= tm.tmHeight+tm.tmExternalLeading ;
if(head->next != NULL)
{
print=head->next ;
for(num=0;num<head->data ;num++)
{
if(print->data <128)
{
if(print->data !=13)
{
buf[0]= char(print->data );
GetTextExtentPoint32(hdc,&buf[0],1,&size);
xchar = xchar+size.cx;
::GetClientRect(hwnd,&rtClient);
if(xchar>=rtClient.right )
{
xchar = size.cx ;
ychar = ychar+ size.cy + tm.tmExternalLeading ;
}
TextOut(hdc,xchar-size.cx,ychar-size.cy,buf,1);
}
else
{
xchar = 0;
ychar = ychar+ size.cy + tm.tmExternalLeading ;
}
print=print->next ;
}
else
{
buf[0]=print->data /256;
buf[1]=print->data %256;
GetTextExtentPoint32(hdc,buf,2,&size);
xchar = xchar+size.cx;
::GetClientRect(hwnd,&rtClient);
if(xchar>=rtClient.right )
{
xchar = size.cx ;
ychar = ychar+ size.cy + tm.tmExternalLeading ;
}
TextOut(hdc,xchar-size.cx,ychar-size.cy,buf,2);
print=print->next ;
}
}
}
SetCaretPos(xchar,ychar-size.cy+2 );

EndPaint(hwnd,&ps);
break;

case WM_SIZE:
::InvalidateRect (hwnd,NULL,TRUE);
break;

case WM_CHAR:
if(wParam==VK_BACK)
{
if(head->next==NULL)
MessageBox(hwnd,"最前端",NULL,MB_OK);
else
{
if(curp->next == NULL)
{
prep = prep->pre ;
// prep->next->next =NULL;
delete curp;
curp = prep->next ;
curp->next =NULL;
}
else
{
prep->next = curp->next ;
curp->next ->pre = prep;
delete curp;
curp = prep->next ;
}

InvalidateRect(hwnd,NULL,TRUE);
}
break;
}



if((unsigned)wParam<128)
{
prep=curp;
curp = new LNode;
curp->pre = prep;
prep->next = curp;
curp->data = (unsigned)wParam;
curp->next = NULL;
head->data +=1;


InvalidateRect(hwnd,NULL,FALSE);

}
else
{
if(Word) //Word为1表示汉字的高位录入
{
prep=curp;
curp = new LNode;
prep->next = curp;
curp->pre = prep;
curp->next = NULL;
Word=!Word;
curp->data = (unsigned)wParam;
curp->data=curp->data <<8;
head->data +=1;
}
else
{
curp->data =curp->data + (unsigned)wParam;
Word=!Word;
InvalidateRect(hwnd,NULL,TRUE);
}

}

break;
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return DefWindowProc(hwnd,message,wParam,lParam);
}

return 0;
}



这个程序的功能主要是想实现记事本的功能,还有很多功能没有写,但是BackSpace出错了, 哪位高手帮忙解答一下。
...全文
119 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
IT_yangjing 2012-03-12
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 linuxcard 的回复:]
if(curp->next == NULL)
{
prep = prep->pre ;
delete curp; //这里已经被删除
curp = prep->next ;//这里又给他赋值
curp->next =NULL; //debug到这一步是,可以看到curp开始指向的节点 已 //经删除,并且移向了前一节点,即实现了删除,为什么运行时还会报错
}
[/Quote]
我已经找到错误了, 不过告诉你啊,你的说法是错误的, delete curp 不是把curp删掉了, 而是把curp所指向的节点删掉了,当然而后我有可以把其他节点的地址赋给他。 这里调试我可以看到没错,确实是这样,而是我再一次刷新显示的时候,head->data里的没有减1,所以每次一删除,就说我不能读空内存。 刚刚发现这个错误,不过我删到最后一次字符的时候又有错了。我再好好看看。
LinuxCard 2012-03-11
  • 打赏
  • 举报
回复
if(curp->next == NULL)
{
prep = prep->pre ;
delete curp; //这里已经被删除
curp = prep->next ;//这里又给他赋值
curp->next =NULL; //debug到这一步是,可以看到curp开始指向的节点 已 //经删除,并且移向了前一节点,即实现了删除,为什么运行时还会报错
}
IT_yangjing 2012-03-11
  • 打赏
  • 举报
回复
都没人回答啊, 是不是看代码太多了, 其实其实只要看我前面粘贴的那段代码就可以了的

1,221

社区成员

发帖
与我相关
我的任务
社区描述
C++ Builder Windows SDK/API
社区管理员
  • Windows SDK/API社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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