comp.lang.ada
 help / color / mirror / Atom feed
* Problem understanding procedure Adjust(.
@ 2001-04-07 13:28 Ayende Rahien
  2001-04-07 13:45 ` Mark Biggar
  2001-04-08  2:28 ` Jeffrey Carter
  0 siblings, 2 replies; 5+ messages in thread
From: Ayende Rahien @ 2001-04-07 13:28 UTC (permalink / raw)


I'm currently learning Ada from http://adahome.com/Tutorials/Lovelace/, I
reached the part where he talk about access variables
(http://adahome.com/Tutorials/Lovelace/s12sf.htm)
And I'm trying to read this package's body
(http://adahome.com/Tutorials/Lovelace/genestac.adb)
My problem is with this procedure:
    procedure Adjust(Object : in out Stack) is
    -- ...
    end Adjust;

As far as I understand, this procedure is called if I do assignment on
stacks (which is what this package is about).
If I got it correctly, when I do:
stack1 := stack2;
The Adjust procedure will be called.

Now, Object seems to be the variable on the right side (stack2), if so, how
come that stack1 get a new copy of the contents of stack2, without stack2
being affected?
Object is an in ou parameter, this mean that changes that are done inside
the procedure will persist after the procedure terminate, right?
So how do stack1 get a copy of stack2 without destroy stack2?

Another question, when using new, how do I find out if the memory allocation
was unsuccessful? I assume that an exception is raise, is this correct? If
so, what is it?

Thanks in advance,
Ayende Rahien









^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: Problem understanding procedure Adjust(.
  2001-04-07 13:28 Problem understanding procedure Adjust( Ayende Rahien
@ 2001-04-07 13:45 ` Mark Biggar
  2001-04-07 13:51   ` Ayende Rahien
  2001-04-08  2:28 ` Jeffrey Carter
  1 sibling, 1 reply; 5+ messages in thread
From: Mark Biggar @ 2001-04-07 13:45 UTC (permalink / raw)


Ayende Rahien wrote:
> And I'm trying to read this package's body
> (http://adahome.com/Tutorials/Lovelace/genestac.adb)
> My problem is with this procedure:
>     procedure Adjust(Object : in out Stack) is
>     -- ...
>     end Adjust;
> 
> As far as I understand, this procedure is called if I do assignment on
> stacks (which is what this package is about).
> If I got it correctly, when I do:
> stack1 := stack2;
> The Adjust procedure will be called.
> 
> Now, Object seems to be the variable on the right side (stack2), if so, how
> come that stack1 get a new copy of the contents of stack2, without stack2
> being affected?
> Object is an in ou parameter, this mean that changes that are done inside
> the procedure will persist after the procedure terminate, right?
> So how do stack1 get a copy of stack2 without destroy stack2?

No, Adjust is called on stack1, after a bitwise copy is made
of stack2.  The intent is that Adjust can do things like deep copies or
reference count changes.
 
> Another question, when using new, how do I find out if the memory allocation
> was unsuccessful? I assume that an exception is raise, is this correct? If
> so, what is it?

STORAGE_ERROR



^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: Problem understanding procedure Adjust(.
  2001-04-07 13:45 ` Mark Biggar
@ 2001-04-07 13:51   ` Ayende Rahien
  0 siblings, 0 replies; 5+ messages in thread
From: Ayende Rahien @ 2001-04-07 13:51 UTC (permalink / raw)



"Mark Biggar" <mark.a.biggar@home.com> wrote in message
news:3ACF1A25.E059BCBC@home.com...
> Ayende Rahien wrote:
> > And I'm trying to read this package's body
> > (http://adahome.com/Tutorials/Lovelace/genestac.adb)
> > My problem is with this procedure:
> >     procedure Adjust(Object : in out Stack) is
> >     -- ...
> >     end Adjust;
> >
> > As far as I understand, this procedure is called if I do assignment on
> > stacks (which is what this package is about).
> > If I got it correctly, when I do:
> > stack1 := stack2;
> > The Adjust procedure will be called.
> >
> > Now, Object seems to be the variable on the right side (stack2), if so,
how
> > come that stack1 get a new copy of the contents of stack2, without
stack2
> > being affected?
> > Object is an in ou parameter, this mean that changes that are done
inside
> > the procedure will persist after the procedure terminate, right?
> > So how do stack1 get a copy of stack2 without destroy stack2?
>
> No, Adjust is called on stack1, after a bitwise copy is made
> of stack2.  The intent is that Adjust can do things like deep copies or
> reference count changes.

Okay, that clear things up.

> > Another question, when using new, how do I find out if the memory
allocation
> > was unsuccessful? I assume that an exception is raise, is this correct?
If
> > so, what is it?
>
> STORAGE_ERROR

Thanks.





^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: Problem understanding procedure Adjust(.
  2001-04-07 13:28 Problem understanding procedure Adjust( Ayende Rahien
  2001-04-07 13:45 ` Mark Biggar
@ 2001-04-08  2:28 ` Jeffrey Carter
  2001-04-08 12:04   ` Ayende Rahien
  1 sibling, 1 reply; 5+ messages in thread
From: Jeffrey Carter @ 2001-04-08  2:28 UTC (permalink / raw)


Ayende Rahien wrote:
> 
> As far as I understand, this procedure [Adjust] is called if I do assignment on
> stacks (which is what this package is about).
> If I got it correctly, when I do:
> stack1 := stack2;
> The Adjust procedure will be called.

To fully understand what happens here (assignment to a variable of a
controlled type), first Finalize is called with Stack1 as its actual
parameter ("Stack1 is finalized."). The Stack2's bit pattern is copied
into Stack1's memory space ("assignment"). Then Adjust is called with
Stack1 as its actual parameter ("Stack1 is adjusted.").

So, it the stack type is implemented as a dynamically allocated linked
list, Finalize would free the list, one node at a time. After
assignment, both stacks would point at the same linked list. Adjust
would then make a deep copy of Stack2's linked list for Stack1 to point
at.

-- 
Jeff Carter
"We call your door-opening request a silly thing."
Monty Python & the Holy Grail



^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: Problem understanding procedure Adjust(.
  2001-04-08  2:28 ` Jeffrey Carter
@ 2001-04-08 12:04   ` Ayende Rahien
  0 siblings, 0 replies; 5+ messages in thread
From: Ayende Rahien @ 2001-04-08 12:04 UTC (permalink / raw)



"Jeffrey Carter" <jrcarter@acm.org> wrote in message
news:3ACFCCC6.9B4C9162@acm.org...
> Ayende Rahien wrote:
> >
> > As far as I understand, this procedure [Adjust] is called if I do
assignment on
> > stacks (which is what this package is about).
> > If I got it correctly, when I do:
> > stack1 := stack2;
> > The Adjust procedure will be called.
>
> To fully understand what happens here (assignment to a variable of a
> controlled type), first Finalize is called with Stack1 as its actual
> parameter ("Stack1 is finalized."). The Stack2's bit pattern is copied
> into Stack1's memory space ("assignment"). Then Adjust is called with
> Stack1 as its actual parameter ("Stack1 is adjusted.").
>
> So, it the stack type is implemented as a dynamically allocated linked
> list, Finalize would free the list, one node at a time. After
> assignment, both stacks would point at the same linked list. Adjust
> would then make a deep copy of Stack2's linked list for Stack1 to point
> at.

Thanks, that help understanding it.





^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2001-04-08 12:04 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2001-04-07 13:28 Problem understanding procedure Adjust( Ayende Rahien
2001-04-07 13:45 ` Mark Biggar
2001-04-07 13:51   ` Ayende Rahien
2001-04-08  2:28 ` Jeffrey Carter
2001-04-08 12:04   ` Ayende Rahien

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox