• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 362
  • Last Modified:

Holes in records

How can I avoid a hole in the memory when defining a record?
example:

var x: record
a: byte;
b: word;
c: byte;
end;

here is the sizeOf(x) = 6
I want it to be 4, so no memory holes between the bytes and the word!
0
barzangy
Asked:
barzangy
1 Solution
 
MotazCommented:
By default there is no holes between record fields, even if you define an array of records there is no holes btween each record and the another one

Motaz
0
 
AttarSoftwareCommented:
Does the packed keyword do this?

ie:

var x : packed record

I might not have the syntax right, but the help files should help with this...

Tim.
0
 
LischkeCommented:
Tim is right. The "holes" are there to align members of the record at a particular boundary to inrease access speed. In order to avoid record alignment use:

var
  x: packed record
    a: byte;
    b: word;
    c: byte;
  end;

Ciao, Mike
0
 
sburckCommented:
Beyond declaring a record as packed, you have a compiler option for record field alignment $A+/-.  This is from the help file:

The $A directive controls alignment of fields in record types.
In the {$A+} state, fields in record types that are declared without the packed modifier are aligned. In the {$A-} state, fields in record types are never aligned. Record type field alignment is described in the Object Pascal Language Guide.
Regardless of the state of the $A directive, variables and typed constants are always aligned for optimal access. In the {$A+} state, execution will be faster.

In your case, however, use packed, which will override this.
0

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now