SizeOf(record) not what expected

General discussion on mikroPascal for dsPIC30/33 and PIC24.
Post Reply
Author
Message
anton
Posts: 807
Joined: 23 Sep 2004 09:16
Location: South-Africa
Contact:

SizeOf(record) not what expected

#1 Post by anton » 09 Mar 2009 17:23

Hi mE,

When I make use of record that contains bytes on the 16bit compiler, SizeOf(record) returns the wrong length for the record,

For example

Code: Select all

var tmpByte : byte;

begin
  result := SizeOf(tmpByte);
end;
returns 1 that is correct, but

Code: Select all

type TTempByte = record
  n : byte;
end;

var tmpByte : TTempByte;

begin
  result := SizeOf(tmpByte);
end;
returns 2. The same goes for more than one byte for example,

Code: Select all

type TTempByte = record
  n : byte;
  m : word;
end;

var tmpByte : TTempByte;

begin
  result := SizeOf(tmpByte);
end;
returns 4 instead of 3.

Anton
Another proud user of LV 24-33A Development System and mikroPascal PRO for dsPIC :)
PortA not working? Add CMCON := 7; PortD not working? Add ADCON1 := 6;
To paste code on the forum, please use the [b] Code [/b] button !! ;)

SamY Fareast
Posts: 46
Joined: 05 Aug 2007 07:15
Location: Shizuoka JAPAN

#2 Post by SamY Fareast » 10 Mar 2009 16:05

Hi anton.

At first I'm not mE guy, and my english is very bad, so Please pardon it when you do not understand my explanation.

Returned value is correct, even if those are different from your expected.
16Bit PIC can byte data even if it allocated at odd address, but can not handle word data allocated at odd address.

Maybe this is the reason why byte data in record type aligned even address.
Think about this type of record.

Code: Select all

Type
  Tsomerec = record
    b1: byte;
    w1: word;
    b2: byte;
    w2: word;
  end;
If this type of record is 'PACKED', w1 and w2 may be allocated at odd address.
Since record type can contain various type of data, each fields aligned on boundary. On other side array type contains single type of data, there is no need to align on word boundary. So "siaeof(arraytype)" may returns value as your expected.

Anyway sometime when I need to save ram (or EEPROM) (ex. Data logger App.) , I use cheap tricks like this.

Code: Select all

  type
    TSomerecord = record
      W1: word;
      Dummy: word;
      W3: word;
  var
    rec: TSomerecord;
   bdata1, bdata2: ^byte;

begin
  bdata1 := @rec + 2;
  bdata2 := @rec + 3;
  bdata1^ := anybytevalue;
  ...
end.
Usually these kind of codings are not recommended, sometimes cause unexpected overwrite, but if use with care, very useful.

Post Reply

Return to “mikroPascal for dsPIC30/33 and PIC24 General”