* Re: ++ of C in ada
2005-07-24 18:15 ++ of C in ada nicolas.b
@ 2005-07-24 18:22 ` Ludovic Brenta
2005-07-25 5:55 ` Jeffrey Carter
` (4 subsequent siblings)
5 siblings, 0 replies; 7+ messages in thread
From: Ludovic Brenta @ 2005-07-24 18:22 UTC (permalink / raw)
"nicolas.b" <nicolas.blanpain@fr.thalesgroup.com> writes:
> How can i implement the operator ++ in ada :
>
> type T_Ptr is access all T_Item;
> How can i implement : procedure Increment (Ptr : in out T_Ptr);
> thanks
Don't do this.
In C, arrays and pointers are interchangeable, and pointer++ is used
to traverse an array.
In Ada, arrays and pointers are different. You cannot increment a
pointer, but you can increment an array index:
type T_Item_Array is array (Positive range <>) of T_Item;
procedure Proc (A : in out T_Item_Array) is
begin
for J in A'Range loop
A (J) := ...
end loop;
end Proc;
The above demonstrates how Ada handles arrays of varying sizes,
without the need for pointers (the compiler uses pointers behind the
scenes, and also ensures you don't go past the end of the array,
forget to deallocate your array, pass the wrong array size to Proc, or
other such mistakes).
If you are not doing arrays, the answer to your question is probably
still "don't do this". If you explain what you are trying to do, we
will explain the proper Ada way (or ways) of doing it. In general,
pointer arithmetic is useful only when interfacing directly with your
hardware.
HTH
--
Ludovic Brenta.
^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: ++ of C in ada
2005-07-24 18:15 ++ of C in ada nicolas.b
2005-07-24 18:22 ` Ludovic Brenta
@ 2005-07-25 5:55 ` Jeffrey Carter
2005-07-25 13:18 ` Marc A. Criley
` (3 subsequent siblings)
5 siblings, 0 replies; 7+ messages in thread
From: Jeffrey Carter @ 2005-07-25 5:55 UTC (permalink / raw)
nicolas.b wrote:
> How can i implement the operator ++ in ada :
>
> type T_Ptr is access all T_Item;
> How can i implement : procedure Increment (Ptr : in out T_Ptr);
The fact that you're asking this indicates that you're at a stage in
your exposure to Ada when you will probably have no need whatsoever for
access types. Access types are rarely needed in Ada unless you're
creating a dynamic data structure, and even then you don't need pointer
arithmetic.
If you have some problem for which you think access types and pointer
arithmetic are required, let us know what the problem is and I suspect
we can show you why you're wrong, and what the Ada way is. In the
extremely rare case that you're not wrong, we can show you what to do.
--
Jeff Carter
"C++: The power, elegance and simplicity of a hand grenade."
Ole-Hjalmar Kristensen
90
^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: ++ of C in ada
2005-07-24 18:15 ++ of C in ada nicolas.b
2005-07-24 18:22 ` Ludovic Brenta
2005-07-25 5:55 ` Jeffrey Carter
@ 2005-07-25 13:18 ` Marc A. Criley
2005-07-25 17:00 ` Martin Krischik
` (2 subsequent siblings)
5 siblings, 0 replies; 7+ messages in thread
From: Marc A. Criley @ 2005-07-25 13:18 UTC (permalink / raw)
nicolas.b wrote:
> How can i implement the operator ++ in ada :
>
> type T_Ptr is access all T_Item;
> How can i implement : procedure Increment (Ptr : in out T_Ptr);
> thanks
>
See package Interfaces.C.Pointers. Section B.3.2 in the Ada LRM
(http://www.adapower.com/rm95/RM-B-3-2.html).
Think twice before going with this approach, though. Is it really
necessary, or are you just carrying through a C/C++ idiom?
-- Marc A. Criley
-- www.mckae.com
-- DTraq - XPath In Ada - XML EZ Out
^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: ++ of C in ada
2005-07-24 18:15 ++ of C in ada nicolas.b
` (2 preceding siblings ...)
2005-07-25 13:18 ` Marc A. Criley
@ 2005-07-25 17:00 ` Martin Krischik
2005-07-25 20:15 ` Robert A Duff
2005-08-02 5:32 ` Craig Carey
5 siblings, 0 replies; 7+ messages in thread
From: Martin Krischik @ 2005-07-25 17:00 UTC (permalink / raw)
nicolas.b wrote:
> How can i implement the operator ++ in ada :
>
> type T_Ptr is access all T_Item;
> How can i implement : procedure Increment (Ptr : in out T_Ptr);
> thanks
Other allready pointed out that what you are asking it is not a good idea.
May I suggest the following readings:
http://en.wikibooks.org/wiki/Programming:Ada:Control#for_loop_on_arrays
http://en.wikibooks.org/wiki/Programming:Ada:Types:array
Martin
--
mailto://krischik@users.sourceforge.net
Ada programming at: http://ada.krischik.com
^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: ++ of C in ada
2005-07-24 18:15 ++ of C in ada nicolas.b
` (3 preceding siblings ...)
2005-07-25 17:00 ` Martin Krischik
@ 2005-07-25 20:15 ` Robert A Duff
2005-08-02 5:32 ` Craig Carey
5 siblings, 0 replies; 7+ messages in thread
From: Robert A Duff @ 2005-07-25 20:15 UTC (permalink / raw)
"nicolas.b" <nicolas.blanpain@fr.thalesgroup.com> writes:
> How can i implement the operator ++ in ada :
>
> type T_Ptr is access all T_Item;
> How can i implement : procedure Increment (Ptr : in out T_Ptr);
You can implement such a thing using System.Storage_Elements and
System.Address_To_Access_Conversions.
Why do you want to use address arithmetic on T_Ptr?
I'm sure you'll get lots of responses saying you *don't* want to do
that, but there are some rare cases where it might be a good idea.
- Bob
^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: ++ of C in ada
2005-07-24 18:15 ++ of C in ada nicolas.b
` (4 preceding siblings ...)
2005-07-25 20:15 ` Robert A Duff
@ 2005-08-02 5:32 ` Craig Carey
5 siblings, 0 replies; 7+ messages in thread
From: Craig Carey @ 2005-08-02 5:32 UTC (permalink / raw)
On 24 Jul 2005 11:15:17 -0700, "nicolas.b"
<nicolas.blanpain@fr.thalesgroup.com> wrote:
>How can i implement the operator ++ in ada :
>
>type T_Ptr is access all T_Item;
>How can i implement : procedure Increment (Ptr : in out T_Ptr);
>thanks
A simple solution is to adapt GNAT source code. Then a problem for
a newbie might be: how to choose Linux and avoid daylight savings
bugs, unless Cygwin "cvs" runs. Here is some code I had already
written:
with System; with Ada.Unchecked_Conversion;
-- Similar to GNAT's i-cpoint.adb
package LL_Byte_Pointers is -- Low level byte pointers
function "+" (Left : in System.Address; Right : in Integer)
return System.Address;
pragma Convention (Intrinsic, "+");
end LL_Byte_Pointers;
package body LL_Byte_Pointers is
generic function AUC renames Ada.Unchecked_Conversion;
subtype Int is Integer;
type Addr is mod System.Memory_Size;
function To_System_Address is new AUC (Addr, System.Address);
function To_Addr is new AUC (System.Address, Addr);
function To_Addr is new AUC (Int, Addr);
Elmt_Size : constant Int := (String'Component_Size
+ System.Storage_Unit - 1) / System.Storage_Unit;
-- Strings are packed byte arrays. X'Address counts bytes
pragma Assert (String'Component_Size = 8);
pragma Assert (System.Storage_Unit = 8);
function "+" (Left : in System.Address; Right : in Int)
return System.Address is
begin
return To_System_Address (
To_Addr (Left) + To_Addr (Elmt_Size * Right));
end "+";
end LL_Byte_Pointers;
Checked in the September 2004 MinGW GNAT compiler:
L:/mingw/bin/gnatmake ll_byte_pointers.adb -gnata -gnato -gnatq -gnatf
-gnatU -m -i -O0 -g -gnatVa -gnatwacfHlpru -gnatR3s
-bargs -E -p -we -s -static -largs -v -v -L. -lwin32ada -luser32...
(Older and newever versions of that compiler don't run, and all versions
seem unusable on large programs. The May 2005 MinGW GNAT lacks a gnat1
file, and seemingly an e-mail address of the pacakger).
---
There was a list of 191 FreeBSD Ports that would be deleted, at the
"announce@freebsd.org" list (on 29 July 2005).
With Debian GNU/Linux, the failures seem to occur at my end (no fonts
in KDE, and a while back it was ill-explained data loss until IBM did
something for the opensource project).
A FreeBSD GNAT Ada cross-compiler seems to be a need.
I found that the FreeBSD Linuxulator can compete with and beat,
a native Linux distro (such a Debian system).
Craig Carey
research@ijs.co.nz
^ permalink raw reply [flat|nested] 7+ messages in thread