Re: accessing record element (question to the pros)



"Simon Wright" <simon@xxxxxxxxxxxx> wrote in message
news:m2oe0w7f3c.fsf@xxxxxxxxxxxxxxxx
"Jeffrey R. Carter" <spam@xxxxxxxx> writes:

However, in general, you can't do what you seem to be trying to
do. Variant records do not allow unchecked type conversion; the
language rules prohibit it. That's what Ada.Unchecked_Conversion is
for.

GNAT (certainly in recent versions) has pragma Unchecked_Union

(http://gcc.gnu.org/onlinedocs/gcc-4.0.2/gnat_rm/Pragma-Unchecked_005fUnion.
html#Pragma-Unchecked_005fUnion).

NB, compiler-specific.

It is present in a number of Ada 95 compilers (at least the Ada
Magic-derived ones as well as GNAT), and is part of Ada 2005 (see
http://www.adaic.com/standards/05rm/html/RM-B-3-3.html). So it's not really
compiler-specific.

OTOH, Unchecked_Union is *only* intended for interfacing to C. In
particular, using it to get the effect of Unchecked_Conversion is strongly
discouraged. Indeed, any such use is defined to be erroneous (see the Note
at the end of B.3.3 - there's no explicit rule because it follows from the
rules for check suppression). It might work, but a compiler could raise an
exception or reformat your hard disk as well...

If you want this to work reliably, use Unchecked_Conversion. I don't endorse
tricks with 'Address, either, because they can confuse optimizers and are
just too tricky to understand for the maintenance programmers to follow. Ada
has a special dispensation that Unchecked_Conversion doesn't have to make a
copy (unlike other functions), so there's really no good reason for using
tricky overlays. If Unchecked_Conversion is too slow, lean on your compiler
vendor!!

Randy.


.



Relevant Pages

  • Re: Extending discriminant types
    ... Because the poster states that he can compiler the code. ... I used Gnat 3.15p using Ada 95 specs. ... type Child is new KBase with null record; ...
    (comp.lang.ada)
  • Re: Ravenscar-compliant bounded buffer
    ... the mid 1980's most compiler are FRONT_END compilers aka ... any system build around GNAT will contain the C code that GNAT ... The XGC Ada which is based on GNAT and uses C for its Real Time ... with VAXes for data processing applications. ...
    (comp.lang.ada)
  • Re: GNAT verses A# for soft-realtime system
    ... I have a choice of GNAT or A# for my programming environment. ... Whether you go with a supported compiler, or with a free version of GNAT ... version of Ada you want to use. ... Windows target but could also use .net on top of that. ...
    (comp.lang.ada)
  • Re: GNAT verses A# for soft-realtime system
    ... I have a choice of GNAT or A# for my programming environment. ... Whether you go with a supported compiler, or with a free version of GNAT ... version of Ada you want to use. ... Windows target but could also use .net on top of that. ...
    (comp.lang.ada)
  • Re: ada compiler?
    ... The compiler uses the FPU ... -- learning and debugging feature that is build-in to Ada. ... -- one will suggest that this should be reported to Adacore. ... This is stated on a number of Adacore and GNAT source files ...
    (comp.lang.ada)