Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Problem using delete (Linked Lists)

Reply
Thread Tools

Problem using delete (Linked Lists)

 
 
Jonas Ferreira
Guest
Posts: n/a
 
      01-06-2007
Hi everyone, I'm trying to code a linked list example and everything
was working fine. Then I started to code my clear() (to clear my list)
but it won't work. Here is the code:

class list {
protected:
struct node {
char* data;
node* prev;
node* post;
node(): prev(NULL),post(NULL) {};
node(char* u): data(u),prev(NULL),post(NULL) {};
};
node* head;
node* tail;
int QS,qtd;
public:
list(int size,char* u);
void go(char* u);
void show();
void clear();
};

void list::clear()
{
node *L,*D;
int I;
for (L=head;L!=tail;L=D) { D=L->post; delete L; };
cout << "clear(): " <<endl;
};

I didn't post all the code here but I can send the rest of it if anyone
needs (wishes).
Well, I can compile it and even run it, but "clear()" won't work.
I wrote a main.cpp just to populate the list, then show its nodes,
clear the list, then show its nodes again (it should be 0 nodes, after
the clear()), but after the clear, its nodes are the very same before
the clear.

---main.cpp----
#include stuff
int main() {
list L(20,"teste");
L.go("papa01");
L.go("papa02");
L.go("papa03");
L.go("papa04");
L.show();
L.clear();
L.show();
};
----------------

the output is:
----
papa01
papa02
papa03
papa04
clear():
papa01
papa02
papa03
papa04
-----

If anyone can help just saying what is wrong, I would appreciate it.
Thanks, Jonas.

ps: Sorry about the poor english.

 
Reply With Quote
 
 
 
 
Alf P. Steinbach
Guest
Posts: n/a
 
      01-06-2007
* Jonas Ferreira:
> Hi everyone, I'm trying to code a linked list example and everything
> was working fine. Then I started to code my clear() (to clear my list)
> but it won't work. Here is the code:
>
> class list {
> protected:
> struct node {
> char* data;
> node* prev;
> node* post;
> node(): prev(NULL),post(NULL) {};
> node(char* u): data(u),prev(NULL),post(NULL) {};
> };
> node* head;
> node* tail;
> int QS,qtd;
> public:
> list(int size,char* u);
> void go(char* u);
> void show();
> void clear();
> };
>
> void list::clear()
> {
> node *L,*D;
> int I;
> for (L=head;L!=tail;L=D) { D=L->post; delete L; };
> cout << "clear(): " <<endl;
> };


Avoid non-idiomatic single char names. Don't use all uppercase
identifiers except for macros. Remember to delete the tail node and
null out 'prev' and 'post'.


> I didn't post all the code here but I can send the rest of it if anyone
> needs (wishes).


No need.


> Well, I can compile it and even run it, but "clear()" won't work.
> I wrote a main.cpp just to populate the list, then show its nodes,
> clear the list, then show its nodes again (it should be 0 nodes, after
> the clear()), but after the clear, its nodes are the very same before
> the clear.


That's just by happenchance: the memory for each deleted node has been
made available for reuse, but (with this C++ implementation) the
contents haven't been changed.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
 
Reply With Quote
 
 
 
 
Alf P. Steinbach
Guest
Posts: n/a
 
      01-06-2007
* Alf P. Steinbach:
> null out 'prev' and 'post'.


'head' and 'tail', I meant.

Those names are confusing.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
 
Reply With Quote
 
Jonas Ferreira
Guest
Posts: n/a
 
      01-06-2007
Sorry about the names, i had to change them to post here (they were all
in portuguese).
I setted the head and tail pointers to NULL and now everything is
working fine.

You said "with this C++ implementation" the contents remain unchanged.
Is there a way to "nullify" these content?? Is it really necessary?
Because I was taught that you don't need a destructor, because most
compilers already give you one that destroys the builtin types (does
this apply to char* type?).

Really, thanks for the help!

Alf P. Steinbach escreveu:

> * Alf P. Steinbach:
> > null out 'prev' and 'post'.

>
> 'head' and 'tail', I meant.
>
> Those names are confusing.
>
> --
> A: Because it messes up the order in which people normally read text.
> Q: Why is it such a bad thing?
> A: Top-posting.
> Q: What is the most annoying thing on usenet and in e-mail?


 
Reply With Quote
 
Grizlyk
Guest
Posts: n/a
 
      01-12-2007

Jonas Ferreira wrote:

1.
> node(): prev(NULL),post(NULL) {};
> node(char* u): data(u),prev(NULL),post(NULL) {};


much better do like this:

node(char* u=0): data(u),prev(NULL),post(NULL) {};

2.
List must have ctors and dtor

public:
list()throw():head(0),tail(0),QS(?),qtd(?){}
~list()throw(){clear();}

//at least disable copy&assign
private:
list(const list&)throw():head(0),tail(0),QS(?),qtd(?){exit(1) ;}
void operator= (const list&)throw(){exit(1);}

3.
> void list::clear()
> {
> node *L,*D;
> int I;
> for (L=head;L!=tail;L=D) { D=L->post; delete L; };
> cout << "clear(): " <<endl;
> };


Do not use uninitialized vars (*L,*D; I).
What the reason of D?

Learn C/C++ function structure. It is something like this:

chech incoming (bounds etc)
do actions
chech result

After function have returned, class must remain in defined correct
state.
What do with list after clear()?

Learn RAII resource control idiom.

http://www.hackcraft.net/raii

This is nothing wrong with naked pointers using, but It is often no
reasons to do it in new classes, because RAII wrapper often gives
better and clear structure of classes.

To use RAII you need define some types of "holders" with different
behaviour, (something like auto_ptr<>), keeping each naked resource
separatedly.

Note (if you are using unwrapped resources) that the C++ does not
allow automatic call of destructor if ctor throw.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Using delete instead of delete[] for 1 integer Money C++ 9 12-24-2006 08:21 PM
To delete or not to delete? Lethal Computer Support 15 07-24-2005 11:47 PM
Can someone tell me why I can't delete this file? and why it blue screens WinXP Pro on delete? zZz Computer Support 1 01-12-2005 02:37 AM
Cannot Delete, (The Delete Key Won't Work) Lee Something Computer Support 13 10-15-2003 09:51 PM
delete on delete ! Sandeep Grover C++ 19 07-22-2003 10:09 AM



Advertisments