![]() |
![]()
| ![]() |
![]()
NAME
SYNTAX
DESCRIPTIONThe The size of primitive (non-derived) data types in C may differ across hardware platforms and implementations. They are defined by corresponding Application Binary Interface (ABI) specifications, see arch(7) for details about ABI used by FreeBSD. It may be necessary or useful for a program to be able to determine the storage size of a data type or object to account for the platform specifics. The unary EXAMPLESDifferent platforms may use different data models. For example, systems on which integers, longs, and pointers are using 32 bits (e.g., i386) are referred to as using the "ILP32" data model, systems using 64 bit longs and pointers (e.g., amd64 / x86_64) as the "LP64" data model. The following examples illustrate the possible results of calling
When applied to a simple variable or data type,
For initialized data or uninitialized arrays of a fixed size known
at compile time, #define DATA "1234567890" char buf1[] = "abc"; char buf2[1024]; char buf3[1024] = { 'a', 'b', 'c' };
The examples above are the same for ILP32 and LP64 platforms, as they are based on character units. When applied to a struct or union, struct s1 { char c; }; struct s2 { char *s; int i; }; struct s3 { char *s; int i; int j; }; struct s4 { int i; uint64_t i64; }; struct s5 { struct s1 a; struct s2 b; struct s3 c; struct s4 d; };
When applied to a struct containing a flexible array
member, struct flex { char c; long b; char array[]; }
One of the more common uses of the int *nums = calloc(512, sizeof(int)); The int nums[] = { 1, 2, 3, 4, 5 }; const int howmany = sizeof(nums) / sizeof(nums[0]); Many systems provide this shortcut as the macro
RESULTThe result of the NOTESIt is a common mistake to apply char *buf; if ((buf = malloc(BUFSIZ)) == NULL) { perror("malloc"); } /* Warning: wrong! */ (void)strncat(buf, input, sizeof(buf) - 1); In that case, the operator will return the storage size of the
pointer ( ‘
int a = 42; printf("%ld - %d\n", sizeof(a = 10), a); /* Result: "4 - 42" */ Since it is evaluated by the compiler and not the preprocessor,
the SEE ALSOSTANDARDSThe Handling of flexible array members in structures conforms to ISO/IEC 9899:1999 (“ISO C99”). AUTHORSThis manual page was written by Jan Schaumann <jschauma@netmeister.org>.
|