comp.lang.ada
 help / color / mirror / Atom feed
From: wojtek@power.com.pl (Wojtek Narczynski)
Subject: Re: Representing data differently
Date: 7 Feb 2003 16:24:25 -0800
Date: 2003-02-08T00:24:25+00:00	[thread overview]
Message-ID: <5ad0dd8a.0302071624.384d0cf6@posting.google.com> (raw)
In-Reply-To: 686be06c.0302070615.3943b629@posting.google.com

dallex@erols.com (Daniel Allex) wrote in message news:<686be06c.0302070615.3943b629@posting.google.com>...

> In C I can represent the same data multiple ways using 
> structs and unions. 

Ada doesn't have unions, because they are unsafe. For example consider
two simple types:

type Foo is Integer range 1..12;
type Bar is Integer range 20..100;

If two variables, one of each type, were using the same chunk of
memory the value of one would always be invalid.

So if there were unions in Ada the compiler could not check your
program for you.

>How and can I do this in Ada? 

Often you can use a variant record instad of a union.

There are also tricks, but of limited applicability. For example:

declare
  F : Foo := 3;
  B : Bar;
  for B'Address use F'Address;
begin
  -- Et violla! Constraint_Error
  B := B + 1;
end;

Another is to do Uncecked_Conversion between (constrained) access
types.

So this can be done, but you really need to know what you are doing.

Regards,
Wojtek



  parent reply	other threads:[~2003-02-08  0:24 UTC|newest]

Thread overview: 11+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2003-02-07 14:15 Representing data differently Daniel Allex
2003-02-07 18:07 ` tmoran
2003-02-09  4:39   ` Craig Carey
2003-02-10 12:47     ` Colin Paul Gloster
2003-02-13 17:21       ` Craig Carey
2003-02-08  0:24 ` Wojtek Narczynski [this message]
2003-02-12 18:52 ` Martin Krischik
2003-02-22 19:09   ` Robert A Duff
2003-02-23 13:06     ` Martin Krischik
2003-02-23 21:09 ` Craig Carey
2003-02-23 21:59   ` tmoran
replies disabled

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