Solved

Why "packed record" ?

Posted on 2003-10-31
7
318 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
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
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
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…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

821 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