comp.lang.ada
 help / color / mirror / Atom feed
From: Pascal MALAISE <malaise@alphalink.com.au>
Subject: Re: Representation clauses and records
Date: 1997/12/16
Date: 1997-12-16T00:00:00+00:00	[thread overview]
Message-ID: <349646A2.1A013246@alphalink.com.au> (raw)
In-Reply-To: 347000b1.4909762@news.geccs.gecm.com


Brian Orpin wrote:

> The question is simple; when declaring a representation clause for a
> record there is an optional alignment clause using 'mod at'.  What effect
> does this have on the component representation clause and how does this
> relate to the storage size of the system?
> 
> Having read the LRM I get the impression that it is to do with word
> boundaries and alignment but I do not understand the implications of
> using different values for the 'at mod' statement.

Hope you know a little bit about C, this will help, but anyway.

You expect to receive a message of max 100 bytes.
You declare a buffer of 100 characters.
You call whatever procedure to fill your buffer with the message.
You discover that the beginning of the message is, in fact, an int.
You use any C pointer cast or ADA unchecked conversion to "map" these
first four bytes to an integer.

In C or ADA, you are likely to get a bus error or an unaligned access,
more or less recovered by the operating system or your application :-(

This is because processors make assumptions about the addresses of given
data types:
character/byte - any address
16 bits "word" - address is a multiple of 2
32 bits "int"  - address is is a multiple of 4
64 bits "long" - address is a multiple of 8

When you declare your array of bytes/characaters, the compiler
implements
the array starting at any address. When you cast, the compiler will
consider the new (integer) data as aligned and the processor will trap.

In C, the workaroud is to always define arrays of longs (64 bits) of
the according size.
In ADA, you can specify an alignment clause for your buffer/structure.

Note that, however, compilers are intelligent:
when you define a record or even a "union", the fields are implemented
(alignment of first field + gaps for the others) to match the
alignment requirements of each field. So, as long as you don't "cast"
it should be OK.


-- 
Pascal MALAISE			| E-mail:
52 Fletcher St			|  (priv) malaise@alphalink.com.au
HAWTHORN EAST    VIC    3123	|  (prof) malaise@thomson.starway.net.au
AUSTRALIA




  parent reply	other threads:[~1997-12-16  0:00 UTC|newest]

Thread overview: 13+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [not found] <347000b1.4909762@news.geccs.gecm.com>
1997-11-21  0:00 ` Representation clauses and records Brian Nettleton
1997-11-21  0:00   ` Matthew Heaney
1997-11-21  0:00   ` Brian Nettleton
1997-11-24  0:00     ` Martin M Dowie
1997-11-25  0:00       ` Robert Dewar
1997-11-27  0:00         ` Martin M Dowie
     [not found]   ` <347a8dc3.3438484@news.geccs.gecm.com>
1997-11-25  0:00     ` Matthew Heaney
1997-12-16  0:00 ` Pascal MALAISE [this message]
1997-12-16  0:00   ` Larry Kilgallen
1997-12-17  0:00     ` Pascal MALAISE
1997-12-18  0:00       ` David J. Fiander
1997-12-18  0:00         ` Tucker Taft
1997-12-18  0:00         ` Robert Dewar
replies disabled

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