Record Size

This is a discussion on Record Size within the Delphi forums in Programming Languages category; All, We have the following structure in Delphi 5: TMyRecord = record Int1: integer; Int2: integer; Int3: integer; Date1: TDateTime; end; The size of this structure is 24 bytes (presumably since the record is unpacked). We are in the process of porting our code to Delphi 2007 and the record size for this is 20 bytes (presumably because D2007 packs by default even without the "packed record" syntax). Unfortunately this legacy code that we are porting requires us to continue to use a few of these unpacked records because there are contained in legacy binary files at client sites that ...

Go Back   Application Development Forum > Programming Languages > Delphi

Object Mix

Register FAQ Calendar Search Today's Posts Mark Forums Read
  #1  
Old 08-07-2008, 12:12 PM
Ed Salgado
Guest
 
Default Record Size

All,

We have the following structure in Delphi 5:

TMyRecord = record
Int1: integer;
Int2: integer;
Int3: integer;
Date1: TDateTime;
end;

The size of this structure is 24 bytes (presumably since the record is
unpacked). We are in the process of porting our code to Delphi 2007 and the
record size for this is 20 bytes (presumably because D2007 packs by default
even without the "packed record" syntax). Unfortunately this legacy code
that we are porting requires us to continue to use a few of these unpacked
records because there are contained in legacy binary files at client sites
that need to still be read by the system. My question is, how can I emulate
these records in D2007? I'm hoping that there is an "unpacked record" or
some such that we can go to each record and fix them manually, or some
compiler directive to do the same.

Thanks!

-ed


Reply With Quote
  #2  
Old 08-07-2008, 12:23 PM
John Herbster
Guest
 
Default Re: Record Size


"Ed Salgado" <ed@onedomain.com> wrote
> ... requires us to continue to use a few of these unpacked
> records
> because there are contained in legacy binary files at client sites
> that need to still be read by the system. My question is, how can
> I emulate these records in D2007?


Just edit your old records definitions to be packed and add padding
as required. Then they will work in both old and new systems with
your legacy data.

One other thing, be sure to admonish the programmer(?) of the
old system to not make that kind of mistake again.

HTH, JohnH

Reply With Quote
  #3  
Old 08-07-2008, 12:28 PM
Jens Gruschel
Guest
 
Default Re: Record Size

> TMyRecord = record
> Int1: integer;
> Int2: integer;
> Int3: integer;
> Date1: TDateTime;
> end;
>
> The size of this structure is 24 bytes (presumably since the record is
> unpacked). We are in the process of porting our code to Delphi 2007 and the
> record size for this is 20 bytes (presumably because D2007 packs by default
> even without the "packed record" syntax). Unfortunately this legacy code
> that we are porting requires us to continue to use a few of these unpacked
> records because there are contained in legacy binary files at client sites
> that need to still be read by the system.


If size does matter, never use an unpacked record, always a packed one.
So simply add the packed keyword and add dummy fields to make it
compatible with your existing binary files. You should also avoid
generic types like integer but use fundamental types like longint instead...

TMyRecord = packed record
Int1: Longint;
Int2: Longint;
Int3: Longint;
Dummy1: array[0..3] of Byte;
Date1: TDateTime;
end;


--
Jens Gruschel
http://www.pegtop.net
Reply With Quote
  #4  
Old 08-07-2008, 12:29 PM
Ed Salgado
Guest
 
Default Re: Record Size

Thanks for the input. Can we assume that all records need to be padded at
the end or is there some field alignment being performed as well?


"John Herbster" <herb-sci1_AT_sbcglobal.net> wrote in message
news:489b2178@newsgroups.borland.com...
>
> "Ed Salgado" <ed@onedomain.com> wrote
>> ... requires us to continue to use a few of these unpacked records
>> because there are contained in legacy binary files at client sites that
>> need to still be read by the system. My question is, how can
>> I emulate these records in D2007?

>
> Just edit your old records definitions to be packed and add padding
> as required. Then they will work in both old and new systems with
> your legacy data.
>
> One other thing, be sure to admonish the programmer(?) of the
> old system to not make that kind of mistake again.
>
> HTH, JohnH



Reply With Quote
  #5  
Old 08-07-2008, 12:49 PM
Jens Gruschel
Guest
 
Default Re: Record Size

> Thanks for the input. Can we assume that all records need to be padded at
> the end or is there some field alignment being performed as well?


There is some field alignment. Following record has a size of 20 bytes
(well, the Delphi versions I tested):

TMyRecord = record
A: Byte;
B: Longint;
C: Byte;
D: Longint;
E: Byte;
end;

After each byte field 3 padding bytes are inserted. If you are not sure
you can do following test:

var
R: TMyRecorde;
begin
OffsetOfC := Integer(@R.C) - Integer(@R);


--
Jens Gruschel
http://www.pegtop.net
Reply With Quote
  #6  
Old 08-07-2008, 01:53 PM
Ed Salgado
Guest
 
Default Re: Record Size

Wow. This is going to be a problem. There are literally hundreds of
records which we will have to field align. Any other way to handle this?
Any other setting we can use to unpack all these records?

Thanks again.


"Jens Gruschel" <nospam@thisurldoesnotexist.com> wrote in message
news:489b2788@newsgroups.borland.com...
>> Thanks for the input. Can we assume that all records need to be padded
>> at the end or is there some field alignment being performed as well?

>
> There is some field alignment. Following record has a size of 20 bytes
> (well, the Delphi versions I tested):
>
> TMyRecord = record
> A: Byte;
> B: Longint;
> C: Byte;
> D: Longint;
> E: Byte;
> end;
>
> After each byte field 3 padding bytes are inserted. If you are not sure
> you can do following test:
>
> var
> R: TMyRecorde;
> begin
> OffsetOfC := Integer(@R.C) - Integer(@R);
>
>
> --
> Jens Gruschel
> http://www.pegtop.net



Reply With Quote
  #7  
Old 08-07-2008, 02:03 PM
Ed Salgado
Guest
 
Default Re: Record Size

Another question...

TMyRecord = record
Int1: integer; {4 bytes}
Int2: integer; {4 bytes}
Int3: integer; {4 bytes}
Date1: TDateTime; {8 bytes}
end;

This logically looks like a 20 byte record and looks to be as packed as it
can get so using the "packed record" shouldn't make a difference (or does
it?). Why would this record be 24 bytes in D2007?

-ed

"Ed Salgado" <ed@onedomain.com> wrote in message
news:489b1f79$1@newsgroups.borland.com...
> All,
>
> We have the following structure in Delphi 5:
>
> TMyRecord = record
> Int1: integer;
> Int2: integer;
> Int3: integer;
> Date1: TDateTime;
> end;
>
> The size of this structure is 24 bytes (presumably since the record is
> unpacked). We are in the process of porting our code to Delphi 2007 and
> the record size for this is 20 bytes (presumably because D2007 packs by
> default even without the "packed record" syntax). Unfortunately this
> legacy code that we are porting requires us to continue to use a few of
> these unpacked records because there are contained in legacy binary files
> at client sites that need to still be read by the system. My question is,
> how can I emulate these records in D2007? I'm hoping that there is an
> "unpacked record" or some such that we can go to each record and fix them
> manually, or some compiler directive to do the same.
>
> Thanks!
>
> -ed
>
>



Reply With Quote
Reply


Thread Tools
Display Modes


All times are GMT -5. The time now is 07:53 AM.


Powered by vBulletin® Version 3.7.2
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.2.0
vB Ad Management by =RedTyger=

In an effort to better serve ads to our visitors, cookies are used on objectmix.com. For more information, check out our Privacy Policy.