Sunday, July 12, 2009

Some C++ pointer and dynamic memory help please!? How do I delete a struct?

I am trying to make a linked data type in C++ which acts like array, but is a linked structure made up of nodes, where each node knows the next and previous node, there for linking it.





so say I had a linked data type of X, ( I am doing this in a class), how do I delete a node?





EX;





template %26lt;class Type%26gt;


struct a{





Type data;





Node* nextNode;


Node* previousNode;





};





template %26lt;class Type%26gt;


class list{


public:


void insert();


void remove();





private:


Node%26lt;Type%26gt;* frontOfList;


usigned int size;


};








template %26lt;typename Type%26gt;


void Stack%26lt;Type%26gt;::remove()


{





// the front pointer does not point to a next pointer because it


//is the front so it is null


front-%26gt;previousNode-%26gt;nextNode = NULL;





Node%26lt;Type%26gt;* temp = front;








delete front;





// is this deleting the pointer front?


//or actually the node the front points to, and deleting the data, and the pointers to the next and previous node?





front = temp-%26gt;previousNode;








delete temp;


--size;


}








void X::insert(){


-


adds an element to the front of arr

Some C++ pointer and dynamic memory help please!? How do I delete a struct?
Ok, you can find a good description of linked lists here:





http://www.inversereality.org/tutorials/...





It highlights every aspect including deleting.
Reply:The easy answer to your question is to use STL lists. They do exactly what you want and are efficient and reliable. But that doesn't help you do it for a class.





The way you're implementing this, you don't ever want to delete the item itself. This is because your list structure has a copy of the item (Type data) instead of a pointer to the data (Type * data). If you do it this way (the hard way), you will want to use the following algorithm:





1. When inserting an item into your list, create a new element (a * listItem = new a) then copy the data in (a.data = dataToInsert). If you want to support lists of items with non-default constructors you should do both in one line (a * listItem = new a(dataToInsert)) which means you'll want a constructor for your class that takes a reference to the new data.





2. Insert the item into the list.





3. When removing, don't try to delete data. You can't. Delete the whole thing (delete listItem) once you're sure no previous or next pointers are pointing to it.





An easier way is to make data a pointer to your type. (Type * data instead of Type data). In this case, you'd follow the algorithm above with the following differences: First, it's easier to create a link item because you don't have to worry about whether you can create an empty data object. Create the item, then create a new data object using the copy constructor (this is important, otherwise you don't know when it's safe to delete it later. Create a copy, then you can delete it whenever you need). Second, before you delete your struct, delete the data it holds.





Incidentally, if you are creating a stack (only adding/removing from the front), there's no need for a doubly-linked list. You only need a "next" pointer. Add and remove from the head.





Here's a 25-cent overview of new and delete:


1. When you allocate memory by using new, you are responsible for deleting it. The corrolary of that is, never use delete on something you didn't "new", such as a local variable.


2. Never delete an object that something is still pointing to


3. Never delete something twice


4. Never reference memory you've deleted


5. If you allocate using new[] (for allocating arrays) delete using delete[].





Good luck.
Reply:templates are difficult concept. may be you can contact a C++ expert. Check websites like http://askexpert.info/

kudzu

No comments:

Post a Comment