deleting ok unless the first one to be deleted 
Author Message
 deleting ok unless the first one to be deleted

Just for a change, it's me again, with another procedure that I am
working on, and hopefully this is the last time (for now) that I will
be bothering you.

I have a procedure that deletes a student given the id, which works up
to the point that i want to delete the very first node in the list.
When I do that, the program crashes, taking the computer with it
alltogether.
Here is the procedure
(* deletes a student entry based on the given id *)
procedure deletestud(var l, und: studpointer);
var
  id: idrange;
  prev, this, undelete: studpointer;
  found: boolean;
  c: char;
begin
  writeln('Enter ID of student to be deleted: ');
  readln(id);
  this := l;
  prev := nil;
  found := false;
  while this <> nil do
    if this^.id = id then
     begin
      new(und); (* pointer to store the data for the deleted node *)
      und := this;
      prev^.link := this^.link;
      dispose(this);
      this := nil;
      writeln('Student deleted');
      found := true;
      writeln('Do you want to restore the delete the student? ');
      readln(c);
      if c = 'y' then
      begin     (* undoes the deletion of the last deleted student *)
         writeln('This is the deleted list: ');
         printthelist(l);
         und^.link := prev^.link;
         prev^.link := und;
         writeln('student with id ', und^.id, ' is now restored ');
         printthelist(l);
      end;
    end
     else
    begin
      prev := this;
      this := this^.link;
    end;
end;

Btw, I manage to crash my computer while working with linked lists more
than I ever thought possible to. I guess I'll have to use two
computers, to work on one while I am rebooting the second one...
Why is working with pointers crash the computer, I mean except for the
reason that I am 'newbie', what is the technical reason for it?

For what it's worth, thanks again for your inputs,

D.

Sent via Deja.com
http://www.*-*-*.com/



Mon, 30 Jun 2003 19:49:16 GMT  
 deleting ok unless the first one to be deleted

Quote:

> Just for a change, it's me again, with another procedure that I am
> working on, and hopefully this is the last time (for now) that I will
> be bothering you.

> I have a procedure that deletes a student given the id, which works up
> to the point that i want to delete the very first node in the list.
> When I do that, the program crashes, taking the computer with it
> alltogether.
> Here is the procedure
> (* deletes a student entry based on the given id *)
> procedure deletestud(var l, und: studpointer);
> var
>   id: idrange;
>   prev, this, undelete: studpointer;
>   found: boolean;
>   c: char;
> begin
>   writeln('Enter ID of student to be deleted: ');
>   readln(id);
>   this := l;
>   prev := nil;
>   found := false;
>   while this <> nil do
>     if this^.id = id then
>      begin
>       new(und); (* pointer to store the data for the deleted node *)
>       und := this;
>       prev^.link := this^.link;

What if 'prev' is nil here?

- Show quoted text -

Quote:
>       dispose(this);
>       this := nil;
>       writeln('Student deleted');
>       found := true;
>       writeln('Do you want to restore the delete the student? ');
>       readln(c);
>       if c = 'y' then
>       begin     (* undoes the deletion of the last deleted student *)
>          writeln('This is the deleted list: ');
>          printthelist(l);
>          und^.link := prev^.link;
>          prev^.link := und;
>          writeln('student with id ', und^.id, ' is now restored ');
>          printthelist(l);
>       end;
>     end
>      else
>     begin
>       prev := this;
>       this := this^.link;
>     end;
> end;

> Btw, I manage to crash my computer while working with linked lists more
> than I ever thought possible to. I guess I'll have to use two
> computers, to work on one while I am rebooting the second one...
> Why is working with pointers crash the computer, I mean except for the
> reason that I am 'newbie', what is the technical reason for it?

My own (bitter at the time, but educational in retrospect) experience
is that crashes result, when I try to write something to a record
accessed
via a pointer that actually points at NIL. At least in the good old DOS
the
pointer NIL=PTR($0000:$0000) points at the beginning of the interrupt
vector
list.
Overwriting an interrupt vector will quickly hang the computer. I don't
know
for sure, which interrupt vector will be the first to go. My guess is
that
the timer interrupt (normally called 18 times per second) is near the
beginning,
so losing the address to an appropriate timer interrupt handler leaves
your
system with a life expectancy of 55 milliseconds at max.

Quote:

> For what it's worth, thanks again for your inputs,

> D.

> Sent via Deja.com
> http://www.deja.com/

--
Jyrki Lahtonen, Ph.D.
Department of Mathematics,
University of Turku,
FIN-20014 Turku, Finland

http://users.utu.fi/lahtonen



Mon, 30 Jun 2003 21:15:20 GMT  
 deleting ok unless the first one to be deleted

Quote:

>Just for a change, it's me again, with another procedure that I am
>working on, and hopefully this is the last time (for now) that I will
>be bothering you.

>I have a procedure that deletes a student given the id, which works up
>to the point that i want to delete the very first node in the list.
>When I do that, the program crashes, taking the computer with it
>alltogether.
>Here is the procedure
>(* deletes a student entry based on the given id *)
>procedure deletestud(var l, und: studpointer);
>var
>  id: idrange;
>  prev, this, undelete: studpointer;
>  found: boolean;
>  c: char;
>begin
>  writeln('Enter ID of student to be deleted: ');

Do not combine input and processing in that way. The routines that
process should be free of console IO (with possible exception of error
messages if one wants to play dirty). Instead they should receive and
pass the information through parameters.

When I read the code, I thought that und is the student to be deleted.
Then when you  ask the student ID i got really confused.

Quote:
>  readln(id);

No error checking => F

Quote:
>  this := l;
>  prev := nil;
>  found := false;
>  while this <> nil do
>    if this^.id = id then
>     begin
>      new(und); (* pointer to store the data for the deleted node *)

WHY? You already have the node. Use it. There is no need to do a new and
dispose. You might want to clear the next pointer though so that it will
not point to the list anymore. (not critical as the pointer should not
be used anyway)

Quote:
>      und := this;

That is a memory leak!!!!

After you do a new() you cannot change the value of the pointer.

Did you mean:  und^:=this^;???

In anyway keep the und:=this; and remove the new().

Quote:
>      prev^.link := this^.link;

What is prev is NIL??? That is the first node case. You need to handle
it separately. Since this is a school work I do not say how but it
involves modifying the parameter l.

Quote:
>      dispose(this);

Since you set und to this, you dispose und. Get rid of that.

Quote:
>      this := nil;

What's that?

Quote:
>      writeln('Student deleted');
>      found := true;
>      writeln('Do you want to restore the delete the student? ');

Have you thought that fully?

- Show quoted text -

Quote:
>      readln(c);
>      if c = 'y' then
>      begin     (* undoes the deletion of the last deleted student *)
>         writeln('This is the deleted list: ');
>         printthelist(l);
>         und^.link := prev^.link;
>         prev^.link := und;
>         writeln('student with id ', und^.id, ' is now restored ');
>         printthelist(l);
>      end;
>    end
>     else
>    begin
>      prev := this;
>      this := this^.link;
>    end;
>end;

The whole routine is a mess. There is no concept of structured
programming where the program is broken into logical short components what
have descriptive names.

Instead try an interface with:

Procedure deletestud(var l:studpointer; id: studrange; var und: studpointer);

Then no IO on there, instead use the parameters for passing info in and
out. The und could be NIL if the id was not found.

Then you could write a new routine that uses the above and has a name
for example

Procedure AskAndDeleteSud(var L,und:studpointer);

Also document every parameter you use.

This if you wanted to port it to a different system like Windows (I do
not mean Wincrt but proper windows program), or if you wanted to change
the data structure into a tree. Wouldn't that be easier if the IO and
processing was separated.

Quote:

>Btw, I manage to crash my computer while working with linked lists more
>than I ever thought possible to.

If you have BP, then you should use protected mode. It catches things
like referencing a nil pointer which corrupts the interrupt vector
table (the most critical data structure there is) on real mode. I say
Intel was really smart in that. When DEC made VAX they specifically
made low addresses unusable because of nil pointer references. Intel
should have done same. Also Borland failed in nor providing checking for
nil pointer references.

Quote:
> I guess I'll have to use two
>computers, to work on one while I am rebooting the second one...
>Why is working with pointers crash the computer, I mean except for the
>reason that I am 'newbie', what is the technical reason for it?

As I said NIL pointer references. If you write through a nil pointer you
destroy the interrupt vectors. This includes the timer interrupt so your
computer will crash within the next 55 milliseconds.

Osmo



Mon, 30 Jun 2003 22:42:12 GMT  
 
 [ 3 post ] 

 Relevant Pages 

1. Delete a the first line in an ASCII Text file

2. DBASE: permanently deleting deleted records

3. Deleting records in a one to many db.

4. How to delete one column in DBGrid

5. Ok, one more question...

6. Master-Detail=ok / Table=not ok !?

7. Insert : OK. Modify and delete : "Update Failed"

8. Where am I going wrong on DELETE query on Interbase DB?

9. I'm stuck and need advise - cannot fix Record/Key Deleted

10. data aware grid and table - record/key deleted

11. how to delete a database in SQL editor

12. I CAN'T DELETE A TABLE

 

 
Powered by phpBB® Forum Software