comp.lang.ada
 help / color / mirror / Atom feed
* C's static variable
@ 1997-09-05  0:00 RL
  1997-09-05  0:00 ` Matthew Heaney
  1997-09-07  0:00 ` Tom Moran
  0 siblings, 2 replies; 3+ messages in thread
From: RL @ 1997-09-05  0:00 UTC (permalink / raw)



Does anyone know how in Ada to produce the equivalent of a static
variable in C?




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

* Re: C's static variable
  1997-09-05  0:00 C's static variable RL
@ 1997-09-05  0:00 ` Matthew Heaney
  1997-09-07  0:00 ` Tom Moran
  1 sibling, 0 replies; 3+ messages in thread
From: Matthew Heaney @ 1997-09-05  0:00 UTC (permalink / raw)



In article <34109F10.52E2@communique.net>, RL <rladner@communique.net> wrote:

>Does anyone know how in Ada to produce the equivalent of a static
>variable in C?

Funny, this is probably the most frequently asked question about Ada.  

The answer is to declare the variable in a static scope, ie in a package
spec or body:

package P is

   A_Static_Variable : Integer;

end;

P is a library level declaration of a package.  It lives as long as the
application does, so any variables there are equivalent to C static
variables.

Check out the Ada FAQ at the Ada home page:

<http://www.adahome.com>

--------------------------------------------------------------------
Matthew Heaney
Software Development Consultant
<mailto:matthew_heaney@acm.org>
(818) 985-1271




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

* Re: C's static variable
  1997-09-05  0:00 C's static variable RL
  1997-09-05  0:00 ` Matthew Heaney
@ 1997-09-07  0:00 ` Tom Moran
  1 sibling, 0 replies; 3+ messages in thread
From: Tom Moran @ 1997-09-07  0:00 UTC (permalink / raw)




> Does anyone know how in Ada to produce the equivalent of a static
> variable in C?
1) Any variable declared in a package in Ada is analogous to a
'global'ly declared variable in C.  But in Ada it can be inside the
private part of the specification, or in the body of the package, in
which case it retains its value like a C global, but is invisible to
other packages, unlike C.
2) If you want something like declaring a static *inside* a C function,
but it makes no sense to have a separate package usable by anybody, then
you can make an internal package:
package body Whatever is
...
   package Special_Routine_With_A_Static is
     function F(...  ;
   private
     Static_Variable : integer_or_something := some_initial_value;
   end Special_Routine_With_A_Static;
   package body Special_Routine_With_A_Static is
     function F(...
        -- uses and modifies Static_Fariable (in a task-safe/unsafe way)
     end F;
   end Special_Routine_With_A_Static;
...  -- more of package Whatever, including calls 
     -- on Special_Routine_With_A_Static.F
     -- and perhaps a 'renames' of that to a better name
end Whatever;





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

end of thread, other threads:[~1997-09-07  0:00 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
1997-09-05  0:00 C's static variable RL
1997-09-05  0:00 ` Matthew Heaney
1997-09-07  0:00 ` Tom Moran

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