Solved

Why "packed record" ?

Posted on 2003-10-31
7
315 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
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 Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
As a trusted technology advisor to your customers you are likely getting the daily question of, ‘should I put this in the cloud?’ As customer demands for cloud services increases, companies will see a shift from traditional buying patterns to new…

910 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

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now