Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

D6 record, packed vs. 1 byte alignment, why dif?

Status
Not open for further replies.

sijgs

Programmer
Dec 29, 2001
100
CA
anyone:

I had problems switching from D5 to D6 due to differences in the alignment of "records".

I was "forced" to insert "packed record =" in lieu of "record =" to get back the same record length as D5 produced.

In looking at this, there is an "alignment" in the compiler tab which I have set to '1' byte alignment.

HOWEVER, the record STILL does not come out right unless the "packed record =" is in there.

Someone know why...???? this is VERY disturbing.....

TNX
JGS
 
I use this in my code and never had any problems with it :

Code:
type
{$ALIGN OFF}
Myrecord = record
            i : integer;
            b : byte;
            w : word;
           end;            
{$ALIGN ON}

this way, you can leave alignment to 8 and only specify parts which are critical to field alignment.

--------------------------------------
What You See Is What You Get
 
By the way this is what delphi(7) help has to say about this :

Type Switch
Syntax {$A+}, {$A-}, {$A1}, {$A2}, {$A4}, or {$A8}
{$ALIGN ON}, {$ALIGN OFF}, {$ALIGN 1}, {$ALIGN 2}, {$ALIGN 4}, or {$ALIGN 8}
Default {$A8}
{$ALIGN 8}
Scope Local
Remarks

The $A directive controls alignment of fields in Delphi record types and class structures.
In the {$A1} or {$A-} state, fields are never aligned. All record and class structures are packed.
In the {$A2} state, fields in record types that are declared without the packed modifier and fields in class structures are aligned on word boundaries.
In the {$A4} state, fields in record types that are declared without the packed modifier and fields in class structures are aligned on double-word boundaries.

In the {$A8} or {$A+} state, fields in record types that are declared without the packed modifier and fields in class structures are aligned on quad word boundaries.
Record type field alignment is described in the Delphi Language Guide. See Record types.
Regardless of the state of the $A directive, variables and typed constants are always aligned for optimal access. In the {$A8} state, execution will be faster.

--------------------------------------
What You See Is What You Get
 
and this is why you have this problem :

"Delphi Compiler options :

Delphi compiler options correspond to switch directives that you can also set directly in your program code.
Selecting an option is equivalent to setting the switch directive to its positive (+) state.

Code generation Effect

Optimization Enables compiler optimizations. Corresponds to {$O}.
Stack frames Forces compiler to generate stack frames on all procedures and functions. Corresponds to {$W}.
Pentium-safe FDIV Delphi only. Generates Delphi code that detects a faulty floating-point division instruction. Corresponds to {$U}.

Record field alignment Aligns elements in structures to the specified number of bytes (1, 2, 4, or 8). Choose the alignment type from the drop-down list. Corresponds to {$A}. Note: In older versions of Delphi, this option could be checked on or off. Choosing 1 is the equivalent of off and 8 is the equivalent of on."

you see, it's all there, just RTFM
[2thumbsup]




--------------------------------------
What You See Is What You Get
 
Thanks to all for the hints and tips, but no one answered the question...

Why doesn't the alignment WORK when it's set to 1 in the compiler options????

I set it to 1 and the record still comes out with the "wrong length" (being "bigger" than the D5 record length) and HAVE to put in "packed" (or possibly one of the other "SUPPOSEDLY UNNECESSARY" options)

THAT'S the question!

and I DID RTFM.... just for your info

JGS
 
could it be that some unit (none vcl) turns on the alignment switch but never turns it off??
anyway if you use my method, you'll never have to worry about this compiler setting...

--------------------------------------
What You See Is What You Get
 
Nope... scanned the entire set of directories with grep and only one test program has a "$A" and it's "{$A-} to boot.

BTW... all the maintenance I know about is on D6 as well

TFTH
JGS
 
In D6, alignment is about the field alignment, not about the whole size of the record. For non packed records the compiler can round the size up for efficiency purposes.

The only way to be sure a record have the needed size is using "packed record". BTW, this is the "standard" keyword for such a thing. Tricking the alignment was nothing but a "feature" of Borland compilers, doomed to failure at Borland's will (namely in D6 :).

buho (A).
 
I got help from tektips when attempting to fix aligment problems reading data into records that had been created from 'C' structures, the answer was packed arrays in packed records, this seems a simple syntacticly correct answer that should work with any version of Delphi (my code was in D3), I agree with buho.

Steve
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top