comp.lang.ada
 help / color / mirror / Atom feed
* C++'s pointer vs Ada's Access type
@ 1996-07-31  0:00 Ding-yuan Sheu
  1996-08-01  0:00 ` David Weller
  1996-08-01  0:00 ` johndoe
  0 siblings, 2 replies; 6+ messages in thread
From: Ding-yuan Sheu @ 1996-07-31  0:00 UTC (permalink / raw)
  Cc: mkanko, kshomper, lawlis


Hello all Ada guru,

	I am doing some C++ programs conversion into Ada95 and I have
a problem in converting C++ pointer into Ada95's access type. I am not
an Ada expert. I hope someone can give me a hand.

	I think C++'s pointer type is basically similar to Ada's access
type. Therefore, for the following C++ declaraction:
	
	int*	ip;  

I convert it into Ada:
	type Integer_Ptr is access all Integer;
	ip : Integer_Ptr;

However, there comes a problem.
In C++, ip can be a pointer that points to a integer array.
Therefore, C++ programmers can do the following initialization:

	int	Array[100];
	ip = &Array;
	for (i:=0;i++,99) { (*ip+i)   = 0; }

My question is how I can do the same thing in Ada by using ip 
to initialize Array instead to directly initialize Array. 

I know my question might be stupid for you Ada experts.
Your comments are preciou to me. Any Help will be appreciated.
Thanks.


	Steven




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

* Re: C++'s pointer vs Ada's Access type
  1996-08-01  0:00 ` johndoe
@ 1996-08-01  0:00   ` Fraser Wilson
  1996-08-01  0:00   ` Mark A Biggar
  1996-08-01  0:00   ` Bryce Bardin
  2 siblings, 0 replies; 6+ messages in thread
From: Fraser Wilson @ 1996-08-01  0:00 UTC (permalink / raw)



stt@henning.camb.inmet.com (johndoe) writes:

>Ding-yuan Sheu (dsheu@afit.af.mil) wrote:

>: 	int	Array[100];
>: 	ip = &Array;
>: 	for (i:=0;i++,99) { (*ip+i)   = 0; }

[ ... ]

[Tuck]
>In any case, the corresponding Ada would generally not use
>pointers at all:

>    An_Array : array (1..100) of Integer;

>    for I in An_Array'Range loop
>        An_Array(I) := 0;
>    end loop;

You're being very polite, Tuck, and I guess the reason is that Sheu
might want something more general, but I just can't resist letting
C and C++ people know when Ada, for want of a better phrase, kicks
butt so well.  Array initialisation is one of those areas:

	An_Array : array (1 .. 100) of Integer;

	An_Array := (others => 0);

(The C++ way has to be more efficient though, because it's closer
to the machine.  Not.)

Fraser (a frustrated Ada programmer who has to speak C++ at the moment)




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

* Re: C++'s pointer vs Ada's Access type
  1996-07-31  0:00 C++'s pointer vs Ada's Access type Ding-yuan Sheu
  1996-08-01  0:00 ` David Weller
@ 1996-08-01  0:00 ` johndoe
  1996-08-01  0:00   ` Fraser Wilson
                     ` (2 more replies)
  1 sibling, 3 replies; 6+ messages in thread
From: johndoe @ 1996-08-01  0:00 UTC (permalink / raw)



Ding-yuan Sheu (dsheu@afit.af.mil) wrote:


: 	I am doing some C++ programs conversion into Ada95 and I have
: a problem in converting C++ pointer into Ada95's access type. I am not
: an Ada expert. I hope someone can give me a hand.

: 	I think C++'s pointer type is basically similar to Ada's access
: type. Therefore, for the following C++ declaraction:
: 	
: 	int*	ip;  

: I convert it into Ada:
: 	type Integer_Ptr is access all Integer;
: 	ip : Integer_Ptr;

: However, there comes a problem.
: In C++, ip can be a pointer that points to a integer array.
: Therefore, C++ programmers can do the following initialization:

: 	int	Array[100];
: 	ip = &Array;
: 	for (i:=0;i++,99) { (*ip+i)   = 0; }

: My question is how I can do the same thing in Ada by using ip 
: to initialize Array instead to directly initialize Array. 

This is somewhat odd looking C/C++ code.  I would have expected:

    for (ip = Array; ip < &Array[100]; ) *ip++ = 0;

In any case, the corresponding Ada would generally not use
pointers at all:

    An_Array : array (1..100) of Integer;

    for I in An_Array'Range loop
        An_Array[I] := 0;
    end loop;

If you are intent on using a pointer, the following would work, though
I can't imagine a case where it would be preferable to the above.

    type Int_Array is array(1..100) of Integer;

    type Array_Ptr is access all Int_Array;
    An_Array : aliased Int_Array;
    IP : Array_Ptr := An_Array'Access;

    for I in IP.all'Range loop
        IP.all[I] := 0;
    end loop;

 (both of the "IP.all" above could be shortened to simply "IP")

: I know my question might be stupid for you Ada experts.
: Your comments are preciou to me. Any Help will be appreciated.

Generally pointers to arrays are not used very much in Ada.  
Pointers to records are used, when you are allocating objects on the heap.
C/C++ programmers sometimes use pointers to perform manual "strength
reduction" when manipulating arrays, but in Ada one generally
just uses normal array indexing, while relying on the compiler to 
use pointer arithmetic at the machine level as appropriate.

: 	Steven

-Tucker Taft   stt@inmet.com   http://www.inmet.com/~stt/
Intermetrics, Inc.  Cambridge, MA  USA




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

* Re: C++'s pointer vs Ada's Access type
  1996-08-01  0:00 ` johndoe
  1996-08-01  0:00   ` Fraser Wilson
@ 1996-08-01  0:00   ` Mark A Biggar
  1996-08-01  0:00   ` Bryce Bardin
  2 siblings, 0 replies; 6+ messages in thread
From: Mark A Biggar @ 1996-08-01  0:00 UTC (permalink / raw)



In article <DvFu6E.Atz.0.-s@inmet.camb.inmet.com> stt@henning.camb.inmet.com (johndoe) writes:
>Ding-yuan Sheu (dsheu@afit.af.mil) wrote:
>: 	I think C++'s pointer type is basically similar to Ada's access
>: type. Therefore, for the following C++ declaraction:
>: 	int*	ip;  
>: I convert it into Ada:
>: 	type Integer_Ptr is access all Integer;
>: 	ip : Integer_Ptr;
>: However, there comes a problem.
>: In C++, ip can be a pointer that points to a integer array.
>: Therefore, C++ programmers can do the following initialization:
>: 	int	Array[100];
>: 	ip = &Array;
>: 	for (i:=0;i++,99) { (*ip+i)   = 0; }
>: My question is how I can do the same thing in Ada by using ip 
>: to initialize Array instead to directly initialize Array. 
>This is somewhat odd looking C/C++ code.  I would have expected:
>    for (ip = Array; ip < &Array[100]; ) *ip++ = 0;
>In any case, the corresponding Ada would generally not use
>pointers at all:
>    An_Array : array (1..100) of Integer;
>
>    for I in An_Array'Range loop
>        An_Array[I] := 0;
>    end loop;

Or even more likely:

     An_Array : array (1..100) of Integer := (1..100 => 0);

--
Mark Biggar
mab@wdl.lmco.com

>
>If you are intent on using a pointer, the following would work, though
>I can't imagine a case where it would be preferable to the above.
>
>    type Int_Array is array(1..100) of Integer;
>
>    type Array_Ptr is access all Int_Array;
>    An_Array : aliased Int_Array;
>    IP : Array_Ptr := An_Array'Access;
>
>    for I in IP.all'Range loop
>        IP.all[I] := 0;
>    end loop;
>
> (both of the "IP.all" above could be shortened to simply "IP")
>
>: I know my question might be stupid for you Ada experts.
>: Your comments are preciou to me. Any Help will be appreciated.
>
>Generally pointers to arrays are not used very much in Ada.  
>Pointers to records are used, when you are allocating objects on the heap.
>C/C++ programmers sometimes use pointers to perform manual "strength
>reduction" when manipulating arrays, but in Ada one generally
>just uses normal array indexing, while relying on the compiler to 
>use pointer arithmetic at the machine level as appropriate.
>
>: 	Steven
>
>-Tucker Taft   stt@inmet.com   http://www.inmet.com/~stt/
>Intermetrics, Inc.  Cambridge, MA  USA






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

* Re: C++'s pointer vs Ada's Access type
  1996-07-31  0:00 C++'s pointer vs Ada's Access type Ding-yuan Sheu
@ 1996-08-01  0:00 ` David Weller
  1996-08-01  0:00 ` johndoe
  1 sibling, 0 replies; 6+ messages in thread
From: David Weller @ 1996-08-01  0:00 UTC (permalink / raw)



In article <31FFD4A6.41C6@afit.af.mil>,
Ding-yuan Sheu  <dsheu@afit.af.mil> wrote:
>
>However, there comes a problem.
>In C++, ip can be a pointer that points to a integer array.
>Therefore, C++ programmers can do the following initialization:
>
>	int	Array[100];
>	ip = &Array;
>	for (i:=0;i++,99) { (*ip+i)   = 0; }
>
>My question is how I can do the same thing in Ada by using ip 
>to initialize Array instead to directly initialize Array. 
>

There are a lot of "implicit" types in C++, but not in Ada.  To
accomplish the code segment you have above, you must declare a type
that is an unconstrained array of integers:
	type Int_Array is array(Natural range <>) of Integer;
	-- I use Natural as the index to conceptually match C's arrays

Then you simply make a declaration:
	A : Int_Array(0..99) := (others=>0);

No pointer arithmetic involved :-)  (Although Ada does support it with
System.Storage_Elements package)

Did I answer the question, or misunderstand it?

-- 
    Visit the Ada 95 Booch Components Homepage: www.ocsystems.com/booch
           This is not your father's Ada -- lglwww.epfl.ch/Ada




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

* Re: C++'s pointer vs Ada's Access type
  1996-08-01  0:00 ` johndoe
  1996-08-01  0:00   ` Fraser Wilson
  1996-08-01  0:00   ` Mark A Biggar
@ 1996-08-01  0:00   ` Bryce Bardin
  2 siblings, 0 replies; 6+ messages in thread
From: Bryce Bardin @ 1996-08-01  0:00 UTC (permalink / raw)



Tucker Taft wrote:

(Details omitted)

> In any case, the corresponding Ada would generally not use
> pointers at all:
> 
>     An_Array : array (1..100) of Integer;
> 
>     for I in An_Array'Range loop
>         An_Array[I] := 0;
>     end loop;
> 

Or, more likely in this simple case, just:

   An_Array : array (1 .. 100) of Integer := (others => 0);

Bryce Bardin




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

end of thread, other threads:[~1996-08-01  0:00 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
1996-07-31  0:00 C++'s pointer vs Ada's Access type Ding-yuan Sheu
1996-08-01  0:00 ` David Weller
1996-08-01  0:00 ` johndoe
1996-08-01  0:00   ` Fraser Wilson
1996-08-01  0:00   ` Mark A Biggar
1996-08-01  0:00   ` Bryce Bardin

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