Flexible Array Member: what changed
Revision 1 to 2. Removed lines have a minus sign; added lines have a plus sign.
| Old | New | Line | |
|---|---|---|---|
| 1 | - | ||
| 1 | + | Is a rarely used feature of C language. It was introduced with C99 standard. The feature was never adopted by C++ compiler (and that might be the reason of the rate use). Some C-like APIs could benefit from the feature, especially low level ones. | |
| 2 | 2 | ||
| 3 | - | ||
| 3 | + | ## Contents | |
| 4 | 4 | ||
| 5 | - | ||
| 5 | + | * 1 Introduction | |
| 6 | + | * 2 Pascal | |
| 7 | + | * 2.1 Direct Addressing | |
| 8 | + | * 2.2 Using Additional Record | |
| 9 | + | * 2.3 Using Advanced Record | |
| 10 | + | * 3 See Also | |
| 6 | 11 | ||
| 7 | - | ||
| 12 | + | ## Introduction | |
| 8 | 13 | ||
| 9 | - | ||
| 14 | + | **Disclaimer:** _this section is pretty much a copy-paste from[Wikipedia article](<https://en.wikipedia.org/wiki/Flexible_array_member>)_ | |
| 15 | + | ||
| 16 | + | Flexible Array Member is a member of a struct, which is an array without a given dimension. It must be the last member of such a struct and it must be accompanied by at least one other member, as in the following example: | |
| 17 | + | ``` | |
| 10 | 18 | typedef struct mystruct | |
| 11 | - | ||
| 12 | - | ||
| 13 | - | ||
| 14 | - | ||
| 15 | - | ||
| 19 | + | { | |
| 20 | + | DWORD a; | |
| 21 | + | DWORD b; | |
| 22 | + | DWORD c[]; // the flexible array member must be last | |
| 23 | + | }; | |
| 16 | 24 | ``` | |
| 17 | 25 | ||
| 18 | - | ||
| 19 | - | ||
| 20 | - | ||
| 26 | + | Typically, such structures serve as the header in a larger, variable memory allocation: | |
| 27 | + | ``` | |
| 21 | 28 | struct mystruct* st= malloc(...); | |
| 22 | - | ||
| 23 | - | ||
| 24 | - | ||
| 29 | + | ... | |
| 30 | + | for (int i = 0; i < st->b; i++) | |
| 31 | + | st->c[i] = ...; | |
| 25 | 32 | ``` | |
| 26 | 33 | ||
| 27 | - | ||
| 34 | + | The sizeof operator on such a struct gives the size of the structure as if the flexible array member was empty. This may include padding added to accommodate the flexible member; the compiler is also free to re-use such padding as part of the array itself. | |
| 28 | 35 | ||
| 29 | - | ||
| 36 | + | It is common to allocate sizeof(struct) + array_len*sizeof(array element) bytes | |
| 30 | 37 | ||
| 31 | 38 | ## Pascal | |
| 32 | 39 | ||
| 33 | - | ||
| 34 | - | ||
| 35 | - | ||
| 40 | + | Pascal doesn't have flexible array member, thus the structure should be declared without the additional member, in order for sizeof(mystruct) to return the same value as in C. | |
| 41 | + | ``` | |
| 36 | 42 | type | |
| 37 | - | ||
| 38 | - | ||
| 39 | - | ||
| 40 | - | ||
| 41 | - | ||
| 43 | + | mystruct = record | |
| 44 | + | a : DWORD; | |
| 45 | + | b : DWORD; | |
| 46 | + | end; | |
| 47 | + | pmystruct = ^mystruct; | |
| 42 | 48 | ``` | |
| 43 | 49 | ||
| 44 | - | ||
| 50 | + | There are a few options on how the additional elements can be accessed: | |
| 45 | 51 | ||
| 46 | 52 | ### Direct Addressing | |
| 47 | 53 | ||
| 48 | - | ||
| 49 | - | ||
| 50 | - | ||
| 54 | + | By explicitly getting the address that goes after the header struct | |
| 55 | + | ``` | |
| 51 | 56 | var | |
| 52 | - | ||
| 53 | - | ||
| 54 | - | ||
| 55 | - | ||
| 56 | - | ||
| 57 | - | ||
| 58 | - | ||
| 59 | - | ||
| 60 | - | ||
| 61 | - | ||
| 57 | + | i : integer; | |
| 58 | + | buf : Pmystruct; | |
| 59 | + | c : PDword; | |
| 60 | + | begin | |
| 61 | + | GetMem(buf, sizeof(mystruct)+ extrasize); | |
| 62 | + | ... | |
| 63 | + | c:=Pointer(buf)+sizeof(mystruct); | |
| 64 | + | ||
| 65 | + | for i := 0 to buf.b-1 do | |
| 66 | + | c[i] := ...; | |
| 62 | 67 | ``` | |
| 63 | 68 | ||
| 64 | 69 | ### Using Additional Record | |
| 65 | 70 | ||
| 66 | - | ||
| 67 | - | ||
| 68 | - | ||
| 71 | + | Depending on the use, the additional record could overlap with the original record using [absolute](</index.php?title=absolute&action=edit&redlink=1> "absolute \(page does not exist\)"). | |
| 72 | + | ``` | |
| 69 | 73 | type | |
| 70 | - | ||
| 71 | - | ||
| 72 | - | ||
| 73 | - | ||
| 74 | - | ||
| 75 | - | ||
| 76 | - | ||
| 77 | - | ||
| 78 | - | ||
| 79 | - | ||
| 80 | - | ||
| 81 | - | ||
| 82 | - | ||
| 83 | - | ||
| 74 | + | mystructEx = record | |
| 75 | + | hdr : mystruct; | |
| 76 | + | c : PDword; | |
| 77 | + | end; | |
| 78 | + | pmystructEx = ^mystructEx; | |
| 79 | + | ||
| 80 | + | ||
| 81 | + | var | |
| 82 | + | i : integer; | |
| 83 | + | buf : PmystructEx; | |
| 84 | + | begin | |
| 85 | + | GetMem(buf, sizeof(mystruct)+ extrasize); | |
| 86 | + | ||
| 87 | + | for i := 0 to buf.hdr.b-1 do | |
| 88 | + | buf.c[i] := ...; | |
| 84 | 89 | ``` | |
| 85 | 90 | ||
| 86 | 91 | ### Using Advanced Record | |
| 87 | 92 | ||
| 88 | - | ||
| 89 | - | ||
| 90 | - | ||
| 93 | + | Advanced records allow to methods and properties for records. Thus the field could be emulated through a method/property | |
| 94 | + | ``` | |
| 91 | 95 | {$modeswitch advancedrecords} | |
| 92 | - | ||
| 93 | - | ||
| 94 | - | ||
| 95 | - | ||
| 96 | - | ||
| 97 | - | ||
| 98 | - | ||
| 99 | - | ||
| 100 | - | ||
| 101 | - | ||
| 102 | - | ||
| 96 | + | ||
| 97 | + | type | |
| 98 | + | mystruct = packed record | |
| 99 | + | a,b: DWORD; | |
| 100 | + | function c: PDWORD; inline; | |
| 101 | + | end; | |
| 102 | + | ||
| 103 | + | function mystruct.c: PDWORD; | |
| 104 | + | begin | |
| 105 | + | Result := PDWORD(pbyte(@self.b)+sizeOf(self.b)); | |
| 106 | + | end; | |
| 103 | 107 | ``` | |
| 104 | 108 | ||
| 105 | - | ||
| 106 | - | ||
| 107 | - | ||
| 109 | + | OR using an empty record. (An empty record has not struct, yet allows to get an address, which should save some time on address calculation) | |
| 110 | + | ``` | |
| 108 | 111 | {$modeswitch advancedrecords} | |
| 109 | - | ||
| 110 | - | ||
| 111 | - | ||
| 112 | - | ||
| 113 | - | ||
| 114 | - | ||
| 115 | - | ||
| 116 | - | ||
| 117 | - | ||
| 118 | - | ||
| 119 | - | ||
| 120 | - | ||
| 121 | - | ||
| 112 | + | ||
| 113 | + | type | |
| 114 | + | mystruct = packed record | |
| 115 | + | a,b: DWORD; | |
| 116 | + | function c: PDWORD; inline; | |
| 117 | + | strict private | |
| 118 | + | cStart: record end; | |
| 119 | + | end; | |
| 120 | + | ||
| 121 | + | function mystruct.c: PDWORD; | |
| 122 | + | begin | |
| 123 | + | Result := PDWORD(@cStart); | |
| 124 | + | end; | |
| 122 | 125 | ``` | |
| 123 | 126 | ||
| 124 | 127 | ## See Also | |
| 125 | 128 | ||
| 126 | - | ||
| 127 | - | ||
| 128 | - | ||
| 129 | + | * <https://forum.lazarus.freepascal.org/index.php/topic,45998.0.html> \- original thread that started the discussion | |
| 130 | + | * <https://en.wikipedia.org/wiki/Flexible_array_member> \- wikipedia on Flexible Array Member | |
| 131 | + | * [[Common problems when converting C header files]] | |
| 129 | 132 | ||
| 130 | 133 | --- | |
| 131 | - | ||
| 134 | + | *From the Free Pascal wiki, page "Flexible Array Member", as the Internet Archive kept it on 2022-10-01. Copyright stays with the author who wrote it, according to the old wiki's [[Licensing]] page. If you have concerns about this material, [message the admin](/wiki-contact).* |
