// 采用指针链表操作
struct selected_pixel
{
int pixel_x;
int pixel_y;
struct selected_pixel * next;
}; struct selected_pixel curr_pixel;struct selected_pixel * p_head, * p_tail, * new_p, * p_ori;
p_ori = p_head = p_tail = &curr_pixel;
之后进行了一系列的操作,操作完成后,我想把链表占用的内存释放掉。于是
         while(p_ori)
{
          new_p=p_ori;
  p_ori=p_ori->next;
  delete new_p;
}         每次运行到delete new_p;的时候就会出现user breakpoint called from code at 0x7c921230,
        难道是不能用delete 吗??请教各位大虾了。

解决方案 »

  1.   

    看样子你的操作过程可能使用了new来创建新的元素。
    但是你的*new_p 当指向 curr_pixel时候,使用 delete new_p就是错误的。
    且如果->next为指向null的时候不需要使用delete链表的操作
    http://www.diybl.com/course/3_program/c/c_js/2007109/77271.html
      

  2.   

    我中间的操作过程是这样的,如下:确实使用到了new 来创建新元素。
    while( p_head != NULL)
    {
    curr_i = p_head->pixel_x;
    curr_j = p_head->pixel_y;
    if ( ( curr_i > 1 && curr_i < height-1) && ( curr_j >1 && curr_j < width-1) )
    {
    top_point = (curr_i-1)*width + curr_j;
    bottom_point = (curr_i+1)*width + curr_j;
    left_point = curr_i*width + curr_j-1;
    right_point = curr_i*width + curr_j+1; if ( YOUT[top_point] == 255 )
    {
    *total_pixel_num += 1;
    YOUT[top_point] = area_label_no;
    new_p = new selected_pixel[1];
    p_tail->next = new_p;
    new_p->pixel_x = curr_i-1;
    new_p->pixel_y = curr_j;
    new_p->next = NULL;
    p_tail = p_tail->next;
    }
    if ( YOUT[bottom_point] == 255 )
    {
    *total_pixel_num += 1;
    YOUT[bottom_point] = area_label_no;
    new_p = new selected_pixel[1];
    p_tail->next = new_p;
    new_p->pixel_x = curr_i+1;
    new_p->pixel_y = curr_j;
    new_p->next = NULL;
    p_tail = p_tail->next;
    }
    if ( YOUT[left_point] == 255 )
    {
    *total_pixel_num += 1;
    YOUT[left_point] = area_label_no;
    new_p = new selected_pixel[1];
    p_tail->next = new_p;
    new_p->pixel_x = curr_i;
    new_p->pixel_y = curr_j-1;
    new_p->next = NULL;
    p_tail = p_tail->next;
    }
    if ( YOUT[right_point] == 255 )
    {
    *total_pixel_num += 1;
    YOUT[right_point] = area_label_no;
    new_p = new selected_pixel[1];
    p_tail->next = new_p;
    new_p->pixel_x = curr_i;
    new_p->pixel_y = curr_j+1;
    new_p->next = NULL;
    p_tail = p_tail->next;
    } }
    p_head = p_head->next;
    }
      

  3.   

    不知道楼主的需求是什么,要实现什么功能,在MFC或者STL中有list、vector、map等很多容器可以使用,如果不是练习,何必还自己做一套呢?
      

  4.   

    curr_pixel不是指针,不能delete,因为第一次循环时new_p=&curr_pixel,所以出错,应该从curr_pixel->next开始循环delete,curr_pixel本身不需要delete
      

  5.   

    多谢6楼,问题已经解决了,就是从curr_pixel->next开始循环释放,非常感谢。