1 The `ffi.sizeof()` and `ffi.alignof()` functions query type sizes and alignments. 2 3 Size of basic types and structs. 4 5 -- Testcase -- 6 {% 7 import * as ffi from 'ffi'; 8 9 ffi.cdef(` 10 typedef char c; 11 typedef short s; 12 typedef int i; 13 typedef struct { int x; } st; 14 `); 15 16 print("sizeof(char): ", ffi.sizeof('c'), "\n"); 17 print("sizeof(int): ", ffi.sizeof('i'), "\n"); 18 print("sizeof(struct): ", ffi.sizeof('st'), "\n"); 19 -- End -- 20 21 -- Expect stdout -- 22 sizeof(char): 1 23 sizeof(int): 4 24 sizeof(struct): 4 25 -- End -- 26 27 28 Size from expression and alignment query. 29 30 -- Testcase -- 31 {% 32 import * as ffi from 'ffi'; 33 34 ffi.cdef(`typedef char c; typedef int i;`); 35 36 let arr = ffi.ctype('int[10]'); 37 print("sizeof(array): ", ffi.sizeof(arr), "\n"); 38 print("alignof(int): ", ffi.alignof('i'), "\n"); 39 print("alignof(struct with char+int): ", ffi.alignof('struct { char c; int i; }'), "\n"); 40 -- End -- 41 42 -- Expect stdout -- 43 sizeof(array): 40 44 alignof(int): 4 45 alignof(struct with char+int): 4 46 -- End -- 47
This page was automatically generated by LXR 0.3.1. • OpenWrt