?
Solved

Why "packed record" ?

Posted on 2003-10-31
7
Medium Priority
?
321 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 100 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 100 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
Industry Leaders: 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

Technology Partners: 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

This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…
Suggested Courses
Course of the Month14 days, 14 hours left to enroll

770 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