From: kevq@banana.demon.co.uk (Kevin F. Quinn)
Subject: Re: raised Constraint_Error?
Date: 1995/04/04
Date: 1995-04-04T00:00:00+00:00 [thread overview]
Message-ID: <19950404.003251.35@banana.demon.co.uk> (raw)
In-Reply-To: Pine.SUN.3.91.950403111430.12054B-100000@babbage
In article <Pine.SUN.3.91.950403111430.12054B-100000@babbage>,
winchest@csee.usf.edu (Jeff Winchester) wrote:
> Could anyone tell me why the following code would cause a constraint
> error? I've been pulling out hairs for a couple of days now and am
> obviously overlooking something simple. Thanks.
Indeed you are - dereferencing null pointers you naughty boy :)
> current := head;
> while current /= null loop
OK, assume here that current.next = NULL (which it will be, eventually,
assuming the list isn't looped)...
> if (current.name (current.nl)) = (sname (snl)) then
> current := current.next;
So now current=NULL
> if (current.class (current.cl)) = (sclass (scl)) then
Crash and burn.
> current := current.next;
> if (current.number = snumber) then
> current := current.next;
> pay := current.cost;
Ditto here, of course (if current.next.next on entry to loop is NULL).
> [...deletia...]
> text_io.put ("Thank you. Your change is $");
> flt_io.put (item => pcost, aft => 2, exp => 0);
> else current := current.next; \
> end if; | Phew! Scratch
> else current := current.next; | this lot for a
> end if; \ start! Put the
> else current := current.next; / c := c.next
> end if; | outside all the ifs.
> else current := current.next; |
> end if; /
> exit when current = null; <--- This is redundant, BTW
> end loop;
> end price;
I'm not doing both for you :)
A tip for the above, I would suggest flattening stuff a bit.
Inside the loop, it's best only to consider current.*, and not
any deeper than that. Only traverse the list at the end of the
loop, i.e. immediately before the check "current /= NULL":
while current /= NULL loop
[Lots of wibble about current.<whatever> here - don't
change current]
current := current.next;
end loop;
BTW Why not ask your course lecturer? That's what they're for...
--
Kevin F. Quinn * "No-one ever made any money out of good
kevq@banana.demon.co.uk * looks and charm."
kevq@cix.compulink.co.uk * "You obviously haven't met Lady Hamilton..."
Compu$erve: 100025,1525 * Blackadder III
prev parent reply other threads:[~1995-04-04 0:00 UTC|newest]
Thread overview: 4+ messages / expand[flat|nested] mbox.gz Atom feed top
1995-04-03 0:00 raised Constraint_Error? Jeff Winchester
1995-04-03 0:00 ` Michael Feldman
1995-04-03 0:00 ` Jeff Winchester
1995-04-04 0:00 ` Kevin F. Quinn [this message]
replies disabled
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox