TEAM-ADA Archives

Team Ada: Ada Programming Language Advocacy

TEAM-ADA@LISTSERV.ACM.ORG

Options: Use Forum View

Use Monospaced Font
Show Text Part by Default
Show All Mail Headers

Message: [<< First] [< Prev] [Next >] [Last >>]
Topic: [<< First] [< Prev] [Next >] [Last >>]
Author: [<< First] [< Prev] [Next >] [Last >>]

Print Reply
Subject:
From:
Simon Wright <[log in to unmask]>
Reply To:
Simon Wright <[log in to unmask]>
Date:
Fri, 22 Nov 2002 17:45:41 GMT
Content-Type:
text/plain
Parts/Attachments:
text/plain (49 lines)
> From: [log in to unmask]

>       i had a small doubt regarding the size occupied by the variable of a
> boolean type.
> x : boolean;
> begin
>       put(boolean'size);  -- it is giving 1 bit
>       put(x'size); -- it is returning  8 bits
> end
>
> why is it so?

'Size is the minimum required, clearly a Boolean will fit in 1 bit if
it's packed in. However, if the compiler can generate more efficient
code by putting actual instances in a byte (or more) it can do so.

X'Size is the size of the actual object, 8 bits -> 1 byte.

If X'Size was 1, what would the compiler do with the other 7 bits in
the byte, given it probably couldn't use them for anything else?

You might look in the ALRM for 'Max_Size_In_Storage_Elements or in the
GNAT documentation for 'Object_Size.

> actuallly i want to type cast a array of boolean type into an integer
> value.
>
> type arr_type is array(integer range 1..2) of boolean;
>
> function uncheck is new unchecked_conversion(arr_type,integer);
>
> a(1) := TRUE
> a(2) := TRUE
>
> when converted, the value i am getting is 257 instead of 3

I guess you are running on an Intel machine? (probably not a VAX or an
Alpha). You have 2 array elements each of one byte, each with value 1
(for True), 256*1 + 1 -> 257.

Didn't your compiler complain about the other 2 bytes? (most compilers use
4 byte Integers nowadays).

Check out pragma Pack and the record representation attributes (ALRM
13.5.1).

I do wonder just what you're trying to achieve with this code, sounds
very un-Adaish.

ATOM RSS1 RSS2