Terry
Terry

Reputation: 97

Delete C++ structure from STL list using iterator

I have this test program. I don't know how to delete struct in the list using iterator.

#include<iostream>
#include<list>
using namespace std;
typedef struct Node
{
    int * array;
    int id;
}Node;

void main()
{
    list<Node> nlist;
    for(int i=0;i<3;i++)
    {
        Node * p = new Node;//how to delete is later?

        p->array = new int[5];//new array
        memset(p->array,0,5*sizeof(int));

        p->id = i;

        nlist.push_back(*p);//push node into list
    }

    //delete each struct in list
    list<Node>::iterator lt = nlist.begin();
    while( lt != nlist.end())
    {
        delete [] lt->array;

        delete &(*lt);//how to delete the "Node"?

        lt++;
    }
}

I know how to delete the struct separately. It's like this:

Node * p = new Node;
p->array = new int[5];

delete [] p->array; //delete the array
delete p;//delete the struct

However, when it is pushed back into list, I don't know how to delete it according to the list iterator.

list<Node>::iterator lt = nlist.begin();
while( lt != nlist.end())
{
    delete [] lt->array;

    delete &(*lt);//how to delete the "Node"?

    lt++;
}

Upvotes: 2

Views: 4600

Answers (3)

Ajay Nair
Ajay Nair

Reputation: 1867

You could use the list erase to delete a node from anywhere in between the list.

list<Node>::iterator it = nlist.begin();
advance(it,n); \\n is the node you want to delete, make sure its less than size of list
it = mylist.erase (it); 

Alternatively, if you want to delete elements from either ends of the list you can use the pop_back or the pop_front member functions.

Upvotes: 1

perreal
perreal

Reputation: 98048

Since you are declaring the list with list<Node> when you do:

nlist.push_back(*p)

it is actually creating a Node() and copying the data from the node you just dynamically allocated but not using the actual pointer. And then you try to delete a pointer from the object that the system will automatically delete:

delete &(*lt); // this causes double free

You need to declare the list like list<Node*> so that the pointer is inserted into the list. Although you should not really deal with this kind of allocation in c++, with a couple of modifications your code should work:

int main()
{
  list<Node*> nlist;
  for(int i=0;i<3;i++)
  {
    Node *p = new Node;//how to delete is later?

    p->array = new int[5];//new array
    memset(p->array,0,5*sizeof(int));

    p->id = i;

    nlist.push_back(p);//push node into list
  }

  //delete each struct in list
  list<Node*>::iterator lt = nlist.begin();
  while( lt != nlist.end())
  {
    delete [] (*lt)->array;

    delete *lt;//how to delete the "Node"?

    lt++;
  }

  return 0;
}

Upvotes: 0

kassak
kassak

Reputation: 4194

use list.erase But you are really doing that non-c++ way. You do not need allocate int[5] with new. Writing int[5] does what you want. Your Node type defined in c-way. In c++ you do not need to wrap it with typedef

Upvotes: 0

Related Questions