typedef struct dlistclient{
int x,y;
CString name;
CString address;
struct dlistclient *prior,*next;
}DListClient;
typedef DListClient *DLinkList;
DLinkList listhead;void CMainFrame::creatlist()
{
  DListClient *s=new(DListClient);//=malloc(sizeof(DListClient));
  s->next=NULL;
  s->x=10;
  s->name=strname;
  if(listhead==NULL)
  {
  listhead=new(DListClient);
  listhead=s;
  listhead->prior=NULL;
  }
  else
  {
      listhead->next=s;
      s->prior=listhead;
      //listhead=s;
  }
  delete s;
 //free(s);}void CMainFrame::OnClickList1(NMHDR* pNMHDR, LRESULT* pResult) 
{
// TODO: Add your control notification handler code here
    CString s;
if(listhead!=NULL)
{
s=listhead->name;//程序运行到此问题
}
if(listhead->next!=NULL)
{
s=listhead->next->name;
}
*pResult = 0;
}错误提示:
            提示
Unhandled exception in newsrtuct.exe(MFC42D.DLL):0xC000005:Access Violation
如果不写 delete s;和 //free(s);就没问题
可是不是明明大家说要内存要回收吗?好多书上也是这样介绍的啊!!

解决方案 »

  1.   

    typedef struct dlistclient{
    int x,y;
    CString name;
    CString address;
    struct dlistclient *prior,*next;
    }DListClient;
    typedef DListClient *DLinkList;DLinkList listhead = NULL;void CMainFrame::creatlist()
    {
      DListClient *s=new(DListClient);//=malloc(sizeof(DListClient));
      s->next=NULL;
      s->x=10;
      s->name=strname;
      
      if(listhead==NULL)
      {
      listhead=s;
      listhead->prior=NULL;
      }
      else
      {
          listhead->next=s;
          s->prior=listhead;
          listhead=s;
      }
    }void CMainFrame::OnClickList1(NMHDR* pNMHDR, LRESULT* pResult) 
    {
    // TODO: Add your control notification handler code here
        CString s;
    if(listhead!=NULL)
    {
    s=listhead->name;//程序运行到此问题
    }
    if(listhead->next!=NULL)
    {
    s=listhead->next->name;
    }
    *pResult = 0;
    }
      

  2.   

    我知道如果不写 delete s;和 //free(s);就没问题
    可是不是明明大家说要内存要回收吗?好多书上也是这样写的啊!!
    为什么这里不能用啊!!!!!!!!!