comp.lang.ada
 help / color / mirror / Atom feed
* some simple questions
@ 2003-02-21 22:56 Daniel Sch�le
  2003-02-22  2:15 ` David C. Hoos, Sr.
                   ` (3 more replies)
  0 siblings, 4 replies; 7+ messages in thread
From: Daniel Sch�le @ 2003-02-21 22:56 UTC (permalink / raw)


Hi all

I am learning Ada on doing
I have found some small examples on web and
now I'm trying to put the mosaic stones together

my questions are

given
    type pint is access integer;
    pi: pint:=new integer;

how do I delete the allocated object?
free(pi);    --doesn't work
(I'm pretty sure Ada doesn't have Garabge Collector)

is there a syntax available to initialize allocated object (like in C++)?
int * pi=new int(0);
now I am doing that this way
pi.all:=0;

what is the proper way to assure that pointer points to valid object?

and last one
what's "std::system("clear");" translated to Ada
(OS Linux)

sorry if it's faq

Thanks

Daniel





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

* Re: some simple questions
  2003-02-21 22:56 some simple questions Daniel Sch�le
@ 2003-02-22  2:15 ` David C. Hoos, Sr.
  2003-02-22  6:29   ` tmoran
  2003-02-22 11:22 ` Preben Randhol
                   ` (2 subsequent siblings)
  3 siblings, 1 reply; 7+ messages in thread
From: David C. Hoos, Sr. @ 2003-02-22  2:15 UTC (permalink / raw)
  To: comp.lang.ada mail to news gateway


----- Original Message -----
From: "Daniel Sch�le" <uval@rz.uni-karlsruhe.de>
Newsgroups: comp.lang.ada
To: <comp.lang.ada@ada.eu.org>
Sent: February 21, 2003 4:56 PM
Subject: some simple questions


> Hi all
>
> I am learning Ada on doing
> I have found some small examples on web and
> now I'm trying to put the mosaic stones together
>
> my questions are
>
> given
>     type pint is access integer;
>     pi: pint:=new integer;
>
> how do I delete the allocated object?
> free(pi);    --doesn't work
> (I'm pretty sure Ada doesn't have Garabge Collector)
You need to instantiate Ada.Unchecked_Deallocation for the type integer.
Then you call the instantiated procedure to deallocate the memory.
See section 13.11.2 of the Ada Reference Manual.

>
> is there a syntax available to initialize allocated object (like in C++)?

Yes.  You would say
pi : pint := new Integer'(1234);

See sections 4.8 and 4.7 of the Ada Reference Manual.
> int * pi=new int(0);
> now I am doing that this way
> pi.all:=0;
>
> what is the proper way to assure that pointer points to valid object?
>
> and last one
> what's "std::system("clear");" translated to Ada
> (OS Linux)
>
> sorry if it's faq
>
> Thanks
>
> Daniel
>
>
> _______________________________________________
> comp.lang.ada mailing list
> comp.lang.ada@ada.eu.org
> http://ada.eu.org/mailman/listinfo/comp.lang.ada
>
>





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

* Re: some simple questions
  2003-02-22  2:15 ` David C. Hoos, Sr.
@ 2003-02-22  6:29   ` tmoran
  0 siblings, 0 replies; 7+ messages in thread
From: tmoran @ 2003-02-22  6:29 UTC (permalink / raw)


>given
>    type pint is access integer;
>    pi: pint:=new integer;
  It *should* be the case that anything of type "pint" is deallocated
when you leave the scope of "type pint is access integer;", since
clearly none of it could ever again be referenced.  There are also
Storage Pools if you want to be sure.  In this case, of course, "pi"
has the same lifetime as "pint" so why not just say
  pi : integer := 1234;

> (I'm pretty sure Ada doesn't have Garabge Collector)
I only know of one Ada implementation with a garbage collector; most
don't.  It's up to the compiler vendor.



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

* Re: some simple questions
  2003-02-21 22:56 some simple questions Daniel Sch�le
  2003-02-22  2:15 ` David C. Hoos, Sr.
@ 2003-02-22 11:22 ` Preben Randhol
  2003-02-22 11:57 ` chris.danx
  2003-02-22 16:15 ` Steve
  3 siblings, 0 replies; 7+ messages in thread
From: Preben Randhol @ 2003-02-22 11:22 UTC (permalink / raw)


Daniel Sch�le wrote:
> Hi all
> 
> I am learning Ada on doing
> I have found some small examples on web and
> now I'm trying to put the mosaic stones together

Here is some recommended reading:

   http://www.adapower.com/learn/

as you can see there are 4 books online to read.

But here is one that perhaps is nice if you are a C++/C programmer:

   http://www.chl.chalmers.se/~franzen/kurser/pp/materials/ada95-tutorial.pdf

-- 
Preben Randhol ---------------- http://www.pvv.org/~randhol/ --
"Violence is the last refuge of the incompetent", Isaac Asimov



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

* Re: some simple questions
  2003-02-21 22:56 some simple questions Daniel Sch�le
  2003-02-22  2:15 ` David C. Hoos, Sr.
  2003-02-22 11:22 ` Preben Randhol
@ 2003-02-22 11:57 ` chris.danx
  2003-02-22 16:15 ` Steve
  3 siblings, 0 replies; 7+ messages in thread
From: chris.danx @ 2003-02-22 11:57 UTC (permalink / raw)


Daniel Sch�le wrote:
> Hi all
> 
> I am learning Ada on doing
> I have found some small examples on web and
> now I'm trying to put the mosaic stones together

If you know C++ or C try the Lovelace tutorial.




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

* Re: some simple questions
  2003-02-21 22:56 some simple questions Daniel Sch�le
                   ` (2 preceding siblings ...)
  2003-02-22 11:57 ` chris.danx
@ 2003-02-22 16:15 ` Steve
  2003-02-22 18:18   ` Jeffrey Carter
  3 siblings, 1 reply; 7+ messages in thread
From: Steve @ 2003-02-22 16:15 UTC (permalink / raw)


[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #1: Type: text/plain, Size: 2365 bytes --]

"Daniel Sch�le" <uval@rz.uni-karlsruhe.de> wrote in message
news:b36b5m$p0i$1@news.rz.uni-karlsruhe.de...
> Hi all
>
> I am learning Ada on doing
> I have found some small examples on web and
> now I'm trying to put the mosaic stones together
>
> my questions are
>
> given
>     type pint is access integer;
>     pi: pint:=new integer;
>
> how do I delete the allocated object?
> free(pi);    --doesn't work
> (I'm pretty sure Ada doesn't have Garabge Collector)
>
> is there a syntax available to initialize allocated object (like in C++)?
> int * pi=new int(0);
> now I am doing that this way
> pi.all:=0;
>
If you add:
    procedure free is new ada.unchecked_deallocation( integer, pint );
Then free(pi) works.

Since Ada is a type-safe language, you can't use the same function on any
old type of variable.  Generics are a way of creating generic functions and
procedures where you can specify a specific type by creating an instance of
the function or procedure.

The standard library for Ada includes a generic procedure for deallocating
memory.  You create an instance of the deallocator for your specifc type as
shown above.

Since you're new to Ada, I suggest perusing the standard library of
functions which you'll find at:

  http://www.adaic.org/standards/95lrm/html/RM-A.html

The reference manual isn't very good for learning Ada, but it is a good
reference for the functions in the standard library.

You will find a number of books on Ada at:

http://www.adapower.com/books/


> what is the proper way to assure that pointer points to valid object?

When you declare an access variable in Ada, it is set to null by default.
Usually you can test for a valid pointer by testing for null.

> and last one
> what's "std::system("clear");" translated to Ada
> (OS Linux)
>

I can only guess on this one (you'll probably get an answer from someone
else).
Here's my guess:
  If the profile for system looks something like:
  int system( char *str );

Then in Ada you can use that function by declaring:

  function System( str : Interfaces.C.Strings.Char_Array_Access ) return
integer;
  pragma Import( C, System, "system" );

And then treating the function just as you would any other Ada function.
One of Ada's strong points is interfacing with other languages.

I hope this helps,
Steve
(The Duck)

> sorry if it's faq
>
> Thanks
>
> Daniel
>
>





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

* Re: some simple questions
  2003-02-22 16:15 ` Steve
@ 2003-02-22 18:18   ` Jeffrey Carter
  0 siblings, 0 replies; 7+ messages in thread
From: Jeffrey Carter @ 2003-02-22 18:18 UTC (permalink / raw)


Steve wrote:
> 
> If you add:
>     procedure free is new ada.unchecked_deallocation( integer, pint );
> Then free(pi) works.

It seems more important to ask why the OP is using an access type. 
Pointers are needed far less often in Ada than in other languages; since 
the OP indicates that he knows [at least] one of these other languages, 
it is likely that this is a mind-set problem rather than a syntax question.

>>and last one
>>what's "std::system("clear");" translated to Ada
>>(OS Linux)
> 
> I can only guess on this one (you'll probably get an answer from someone
> else).
> Here's my guess:
>   If the profile for system looks something like:
>   int system( char *str );
> 
> Then in Ada you can use that function by declaring:
> 
>   function System( str : Interfaces.C.Strings.Char_Array_Access ) return
> integer;
>   pragma Import( C, System, "system" );

There's no need to use Char_Array_Access here; you can simply use 
Char_Array and the compiler will pass it correctly. You might want to 
avoid the name System, since it's the name of a standard package.

I have seen "system" imported and used successfully on Linux and 
Windows. You might also want to look for a compiler dependent package 
for access to the OS to do this. For example, GNAT provides the package 
GNAT.OS_Lib.

-- 
Jeff Carter
"Sons of a silly person."
Monty Python & the Holy Grail




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

end of thread, other threads:[~2003-02-22 18:18 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2003-02-21 22:56 some simple questions Daniel Sch�le
2003-02-22  2:15 ` David C. Hoos, Sr.
2003-02-22  6:29   ` tmoran
2003-02-22 11:22 ` Preben Randhol
2003-02-22 11:57 ` chris.danx
2003-02-22 16:15 ` Steve
2003-02-22 18:18   ` Jeffrey Carter

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