[fpc-pascal] alignment of records

Xiangrong Fang xrfang at gmail.com
Fri Aug 7 05:25:24 CEST 2015


It seems that $packrecord does not work at all.  I did some research and
found this document:

http://www.freepascal.org/docs-html/ref/refsu19.html

compiled and ran the example on that page, I got:

​​Size Trec1 : 4 Offset B : 2
Size Trec2 : 3 Offset B : 1
Size Trec3 : 2 Offset B : 1
Size Trec4 : 2 Offset B : 1
Size Trec5 : 5 Offset B : 1 Offset C : 4
Size Trec6 : 5 Offset B : 1 Offset C : 4
Size Trec7 : 9 Offset B : 1 Offset C : 8
Size Trec8 : 9 Offset B : 1 Offset C : 8

While the expected output, as stated in the document above, should be:

Size Trec1 : 4 Offset B : 2
Size Trec2 : 3 Offset B : 1
Size Trec3 : 2 Offset B : 1
Size Trec4 : 2 Offset B : 1
Size Trec5 : 8 Offset B : 4 Offset C : 7
Size Trec6 : 8 Offset B : 4 Offset C : 7
Size Trec7 : 12 Offset B : 4 Offset C : 11
Size Trec8 : 16 Offset B : 8 Offset C : 15



2015-08-07 10:48 GMT+08:00 Xiangrong Fang <xrfang at gmail.com>:

> Hi All,
>
> I try to port a program from C to Pascal, see the following example:
>
> #include <stdio.h>
> #include <sys/socket.h>
> #include <linux/if.h>
> void main() {
>     printf("ifmap=%ld\n", sizeof(struct ifmap));
> }
>
> I converted it to:
>
> est.c  test.pas
>
>          X
> program test;
> {$mode objfpc}{$PACKRECORDS C}
> type
>   ifmap = record
>     mem_start: dword;
>     mem_end: dword;
>     base_addr: word;
>     irq: byte;
>     dma: byte;
>     port: byte;
>   end;
> begin
>   WriteLn('ifmap=', SizeOf(ifmap));
> end.
>
> The C struct's size is 24, but pascal record is 16.    The ifmap struct is
> defined as:
>
> struct ifmap {
>     unsigned long mem_start;
>     unsigned long mem_end;
>     unsigned short base_addr;
>     unsigned char irq;
>     unsigned char dma;
>     unsigned char port;
>     /* 3 bytes spare */
> };
>
> How can I instruct FPC to align records exactly same as GCC?
>
> Thanks!
>
>
>
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.freepascal.org/pipermail/fpc-pascal/attachments/20150807/84319f2a/attachment.html>


More information about the fpc-pascal mailing list