comp.lang.ada
 help / color / mirror / Atom feed
* Variant Record Sizes..
@ 2004-08-19 18:48 Ganesh Ramasivan
  2004-08-19 19:29 ` Martin Dowie
  2004-08-19 19:30 ` Jeffrey Carter
  0 siblings, 2 replies; 4+ messages in thread
From: Ganesh Ramasivan @ 2004-08-19 18:48 UTC (permalink / raw)


Hello,

I am having a hard time understanding variant records with regard to
memory usage. Does the size of the record become the size of the
largest element in that record?

So in case of the example below, is the size of the Test_Record 2
bytes, even though the "use 8" clause has been specified as the other
element of the record specifies "use 16".

Any input will be appreciated.

Thanks,

Ganesh

------------------------
with ada.text_io;

procedure Variant is

  package int_io is new ada.text_io.integer_io(Integer);

  type TEST_STATE_CHOICE     is (DISABLE, ENABLE);
  for  TEST_STATE_CHOICE'SIZE use 8;

  type OTHER_STATE_CHOICE     is (ON, OFF);
  for  OTHER_STATE_CHOICE'SIZE use 16;  

  type TEST_ICP_POI_MESSAGE_ID_CHOICE is (STATE_1, STATE_2);
  for TEST_ICP_POI_MESSAGE_ID_CHOICE'SIZE use 8;

  type TEST_ICP_POI_MESSAGE_TYPE 
   (ICP_POI_MESSAGE_ID : TEST_ICP_POI_MESSAGE_ID_CHOICE) is
    record
      case ICP_POI_MESSAGE_ID is
        when STATE_1 =>
          ICP_STATE : TEST_STATE_CHOICE;
	when STATE_2 =>
	  PING_STATE : OTHER_STATE_CHOICE;
        when others =>
          null;
      end case;
    end record;
    
    pragma pack (TEST_ICP_POI_MESSAGE_TYPE);
    
    Test_Record : TEST_ICP_POI_MESSAGE_TYPE(STATE_1);
    Record_Size : INTEGER := 0;
begin
  Record_Size := Test_Record'Size/8;
  ada.text_io.put("Record Size = ");
  int_io.put(Record_Size); ada.text_io.new_line;
end Variant;
--------------------------



^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: Variant Record Sizes..
  2004-08-19 18:48 Variant Record Sizes Ganesh Ramasivan
@ 2004-08-19 19:29 ` Martin Dowie
  2004-08-20 14:09   ` Ganesh Ramasivan
  2004-08-19 19:30 ` Jeffrey Carter
  1 sibling, 1 reply; 4+ messages in thread
From: Martin Dowie @ 2004-08-19 19:29 UTC (permalink / raw)


Ganesh Ramasivan wrote:
> Hello,
>
> I am having a hard time understanding variant records with regard to
> memory usage. Does the size of the record become the size of the
> largest element in that record?
>
> So in case of the example below, is the size of the Test_Record 2
> bytes, even though the "use 8" clause has been specified as the other
> element of the record specifies "use 16".
>
> Any input will be appreciated.

Couple of things:
     Test_Record : TEST_ICP_POI_MESSAGE_TYPE(STATE_1);

Constrains "Test_Record" to always be a subtype of TEST_ICP_POI_MESSAGE_TYPE
constrained to "STATE_1".

Because you didn't supply a default value to the descriminant you can never
change which variant you want Test_Record to be:

Both these things mean that the compile 'knows' that Test_Record need only
be 2 bytes long.

So,

1) Add a default to the descriminant if you every what to change which
variant you want the variable to take; and
2) Don't constrain the variable at declaration time.

Cheers

-- Martin





^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: Variant Record Sizes..
  2004-08-19 18:48 Variant Record Sizes Ganesh Ramasivan
  2004-08-19 19:29 ` Martin Dowie
@ 2004-08-19 19:30 ` Jeffrey Carter
  1 sibling, 0 replies; 4+ messages in thread
From: Jeffrey Carter @ 2004-08-19 19:30 UTC (permalink / raw)


Ganesh Ramasivan wrote:

> So in case of the example below, is the size of the Test_Record 2
> bytes, even though the "use 8" clause has been specified as the other
> element of the record specifies "use 16".

Since your object is statically constrained, it's probably 2 bytes: 1 
for the discriminant, and 1 for the field. If you use the other value 
for the discriminant, it will probably be 4 bytes: 1 for the 
discriminant, 2 for the field, and 1 for alignment.

-- 
Jeff Carter
"Sons of a silly person."
Monty Python & the Holy Grail
02




^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: Variant Record Sizes..
  2004-08-19 19:29 ` Martin Dowie
@ 2004-08-20 14:09   ` Ganesh Ramasivan
  0 siblings, 0 replies; 4+ messages in thread
From: Ganesh Ramasivan @ 2004-08-20 14:09 UTC (permalink / raw)


Thanks a lot for all your suggestions. 

I found that the result is compiler dependent. On the GNAT compiler,
the size of Test_Record is 2 bytes but on the Greenhills Ada compiler,
the result is 4 bytes.

On consulting with Greenhills, they mention the following:

"The size of the record includes the size of the largest variant
part".

This would confirm the results I am getting with the GHS compiler.

Cheers!!

Ganesh


"Martin Dowie" <martin.dowie@btopenworld.com> wrote in message news:<cg2v3f$73h$1@sparta.btinternet.com>...
> Ganesh Ramasivan wrote:
> > Hello,
> >
> > I am having a hard time understanding variant records with regard to
> > memory usage. Does the size of the record become the size of the
> > largest element in that record?
> >
> > So in case of the example below, is the size of the Test_Record 2
> > bytes, even though the "use 8" clause has been specified as the other
> > element of the record specifies "use 16".
> >
> > Any input will be appreciated.
> 
> Couple of things:
>      Test_Record : TEST_ICP_POI_MESSAGE_TYPE(STATE_1);
> 
> Constrains "Test_Record" to always be a subtype of TEST_ICP_POI_MESSAGE_TYPE
> constrained to "STATE_1".
> 
> Because you didn't supply a default value to the descriminant you can never
> change which variant you want Test_Record to be:
> 
> Both these things mean that the compile 'knows' that Test_Record need only
> be 2 bytes long.
> 
> So,
> 
> 1) Add a default to the descriminant if you every what to change which
> variant you want the variable to take; and
> 2) Don't constrain the variable at declaration time.
> 
> Cheers
> 
> -- Martin



^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2004-08-20 14:09 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2004-08-19 18:48 Variant Record Sizes Ganesh Ramasivan
2004-08-19 19:29 ` Martin Dowie
2004-08-20 14:09   ` Ganesh Ramasivan
2004-08-19 19:30 ` Jeffrey Carter

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