Solved

Why "packed record" ?

Posted on 2003-10-31
7
314 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
Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

 
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

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…
Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

706 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

19 Experts available now in Live!

Get 1:1 Help Now