comp.lang.ada
 help / color / mirror / Atom feed
From: "Dmitry A. Kazakov" <mailbox@dmitry-kazakov.de>
Subject: Re: Question on initialization of packages
Date: Tue, 17 Jun 2008 10:50:57 +0200
Date: 2008-06-17T10:50:57+02:00	[thread overview]
Message-ID: <1pok6brk3yyxf$.ct5gwnf4g97p$.dlg@40tude.net> (raw)
In-Reply-To: y4adnXtddsFN7crV4p2dnAA@telenor.com

On Tue, 17 Jun 2008 10:07:43 +0200, Reinert Korsnes wrote:

> Assume the package definition given below, 
> and the follow code in my program:
> 
>    type Message_t;
>    type Message_ta is access Message_t;
>    package Message_Stack_p is new Stacks(Message_ta);
>    Send_Stack    : Message_Stack_p.Stack;
> 
> Question: How can I be sure that "Send_Stack" is empty
> at the start of the program execution ?

Hmm, "sure" in which sense? To make it visible for the reader? To specify
in the contract of Stack that it is initially empty?

As for implementation you posted, the stack is empty, because instances of
access types are initialized with null (when not explicitly initialized
otherwise). Below you declare:

>   type Stack is access Cell;

And

   Send_Stack : Message_Stack_p.Stack; -- This will be null = empty


Now side comments:

1.

>   procedure Pop(S: in out Stack; X: out Item) is
>   begin
>     X := S.Value;
>     S := S.Next;
>   end;

This is a memory leak. If you allocated a stack element on push, you should
free it on pop.
 
2. Package initialization is achieved as follows:

package body P is
   ...
begin
   ... -- Initialize package stuff
end P;

3. Package initialization cannot help you here, because the package
declares an abstract data type of which objects can be created long after
the package itself was elaborated (initialized).

4. You have messages. Pointers to messages. These pointers are copied into
dynamically allocated linked list called stack.

How are you going to maintain this? Do you want to prevent messages from
being copied? Then you should reconsider the design of messages allowing
their queuing without stacks. Alternatively, do you want to copy messages
upon queueing (to marshal them)? Then the queue should deal with
unconstrained objects:

   generic
      type Message (<>) is private;
   package Queue is
      ...

-- 
Regards,
Dmitry A. Kazakov
http://www.dmitry-kazakov.de



  reply	other threads:[~2008-06-17  8:50 UTC|newest]

Thread overview: 14+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2008-06-17  8:07 Question on initialization of packages Reinert Korsnes
2008-06-17  8:50 ` Dmitry A. Kazakov [this message]
2008-06-17  9:14   ` Reinert Korsnes
2008-06-17 10:26     ` Dmitry A. Kazakov
2008-06-17 12:03       ` Reinert Korsnes
2008-06-17 14:12         ` Martin
2008-06-17 10:39     ` Georg Bauhaus
2008-06-17 16:41       ` Jeffrey R. Carter
2008-06-17 17:08         ` Robert A Duff
2008-06-17 17:33           ` Dmitry A. Kazakov
2008-06-17 18:29           ` Jeffrey R. Carter
2008-06-17 10:18   ` christoph.grein
2008-06-17 14:29 ` Robert A Duff
2008-06-17 16:39 ` Jeffrey R. Carter
replies disabled

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