Solved

Why "packed record" ?

Posted on 2003-10-31
7
320 Views
Last Modified: 2010-04-05
Hi!

When should one use packed records ?  besides "compressing" the record, is there any advantage or disadvantae when using them ?

Also, what effect does packed record have when interfacing to a C dll ?
0
Comment
Question by:delphi112497
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
7 Comments
 
LVL 14

Accepted Solution

by:
DragonSlayer earned 25 total points
ID: 9662478
In the 32-bit world, data in a record is padded to fit a 4-byte boundary, e.g. a 3-byte variable will take up 4 bytes (including 1 padding) in order to speed up access.

In the olden days of 16-bit, records are padded to fit a 2-byte boundary.

let's assume the following structure in a 16-bit world:

type
  T16BitStructure = record
    a: Integer;
    b: array [0 .. 1] of Char;
    c: Integer;
  end;

back then, Integers are 16-bit data types. So the above structure will take up 6 bytes (I gave example in Pascal, but it applies to C too).

now back to the 32-bit world, for compatibility, you will change Integer to the 16-bit SmallInt:

type
  T32BitStructure = record
    a: SmallInt;
    b: array [0 .. 1] of Char;
    c: SmallInt;
  end;

however, the above will not work because each variable is actually padded to be 4 bytes. Hence, your new structure will not be able to read the 16-bit data structure correctly.

Add the word packed, and voila! It is solved :)

type
  T32BitStructure = packed record
    a: SmallInt;
    b: array [0 .. 1] of Char;
    c: SmallInt;
  end;
0
 
LVL 5

Expert Comment

by:DeerBear
ID: 9662497
Hi,

I would say you must use packed records at any time.
It allows for best speed and it's necessary when creating
API-style routines, in order to be compatible with other
programming languages.

HTH,

Andrew
0
 
LVL 11

Assisted Solution

by:robert_marquardt
robert_marquardt earned 25 total points
ID: 9662588
The alignment to 4 or 8 byte boundaries improves speed especially when using SSE or SSE2 instructions.
packed record (= alignment 1) usually gives you a small loss in speed which is countered by less memory requirement.
Usually the C structs of the Windows API are all packed structures so you need packed record to match the layout.
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 6

Expert Comment

by:swift99
ID: 9662909
DragonSlayer and robert_marquardt's explanations are both sound.
0
 
LVL 7

Expert Comment

by:Motaz
ID: 9666795
Thats a wonderful information, in the past I was thought that the items of un-packed array can be stored in different locations (Non contiguous allocation).
But now I have a question of Dragon, is that applied to typed files of that records too or only to memory.

Motaz
0
 
LVL 7

Expert Comment

by:Motaz
ID: 9666802
sorry, I mean I have a question for Dragon

Motaz
0
 
LVL 14

Expert Comment

by:DragonSlayer
ID: 9668124
it applies to files too.
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
This is a high-level webinar that covers the history of enterprise open source database use. It addresses both the advantages companies see in using open source database technologies, as well as the fears and reservations they might have. In this…
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…

696 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question