sizeof(7)

๐Ÿ“ฆ version 15.1, in freebsd 14.5, freebsd 15.1, nixpkgs unstable

๐Ÿ’… sizeof(7) โ€” the ultimate glow-up for your data ๐Ÿ’…

okay so bestie, listen up. this is the sizeof operator manual page, and honestly? it's giving main character energy in the C world. it's from the Miscellaneous Information Manual, and it's about to teach you how to measure your... data. ๐ŸŽ€


โœจ NAME โœจ

sizeof operator โ€” yield the storage size of the given operand

like, it tells you how much room your stuff takes up. periodt. ๐Ÿ’–


๐ŸŒธ SYNTAX ๐ŸŒธ

sizeof (type)
sizeof expression

two ways to use it, both iconic. ๐Ÿฆ‹


๐Ÿ’œ DESCRIPTION ๐Ÿ’œ

okay so the sizeof operator yields the size of its operand. but bestie, it's not a free-for-all โ€” you cannot apply it to:

  • incomplete types and expressions with incomplete types (e.g. void, or forward-defined struct foo)
  • function types

no cap, it just won't work. ๐Ÿ’€

the size of primitive (non-derived) data types in C may differ across hardware platforms and implementations. they're defined by corresponding Application Binary Interface (ABI) specifications โ€” see arch(7) for details about ABI used by FreeBSD. so like, 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. iykyk. ๐ŸŒˆ

the unary sizeof operator yields the storage size of an expression or data type in char sized units (C language bytes). as a result, sizeof(char) is always guaranteed to be 1. literally always. (the number of bits per char is given by the CHAR_BIT definition in the <limits.h> header; many systems also provide the "number of bits per byte" definition as NBBY in the <sys/param.h> header.)

so yeah, sizeof(char) ate and left no crumbs. ๐Ÿ’…


๐Ÿ”ฅ EXAMPLES ๐Ÿ”ฅ

different 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 sizeof on an ILP32 vs. an LP64 system:

when applied to a simple variable or data type, sizeof returns the storage size of the data type of the object:

Object or type         Result (ILP32)    Result (LP64)
sizeof(char)           1                 1
sizeof(int)            4                 4
sizeof(long)           4                 8
sizeof(float)          4                 4
sizeof(double)         8                 8
sizeof(char *)         4                 8

look at sizeof(long) doing the absolute most on LP64. she's different. ๐Ÿ‘‘

for initialized data or uninitialized arrays of a fixed size known at compile time, sizeof will return the correct storage size:

#define DATA "1234567890"
char buf1[] = "abc";
char buf2[1024];
char buf3[1024] = { 'a', 'b', 'c' };
Object or type         Result
sizeof(DATA)           11
sizeof(buf1)           4
sizeof(buf2)           1024
sizeof(buf3)           1024

the examples above are the same for ILP32 and LP64 platforms, as they are based on character units. so like, consistent queen behavior. ๐Ÿ’–

when applied to a struct or union, sizeof returns the total number of bytes in the object, including any internal or trailing padding used to align the object in memory. this result may thus be larger than if the storage size of each individual member had been added. padding is that extra glitter you didn't ask for but honestly needed. โœจ

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;
};
Object or type         Result (ILP32)    Result (LP64)
sizeof(struct s1)      1                 1
sizeof(struct s2)      8                 16
sizeof(struct s3)      12                16
sizeof(struct s4)      12                16
sizeof(struct s5)      36                56

sizeof(struct s5) really said "i contain multitudes" and honestly? same. ๐Ÿ˜ญ

when applied to a struct containing a flexible array member, sizeof returns the size of the struct without the array, although again possibly including any padding the compiler deemed appropriate:

struct flex {
        char c;
        long b;
        char array[];
}
Object or type         Result (ILP32)    Result (LP64)
sizeof(struct flex)    8                 16

the array is like, the bonus content. not counted. ๐Ÿ™ˆ

one of the more common uses of the sizeof operator is to determine the correct amount of memory to allocate:

int *nums = calloc(512, sizeof(int));

like, allocate responsibly bestie. ๐Ÿซถ

the sizeof operator can be used to calculate the number of elements in an array by dividing the size of the array by the size of one of its elements:

int nums[] = { 1, 2, 3, 4, 5 };
const int howmany = sizeof(nums) / sizeof(nums[0]);

math but make it fashion. ๐Ÿงฎโœจ

many systems provide this shortcut as the macro ntimes() via the <sys/param.h> header file. so you don't even have to do the division yourself. slay. ๐ŸŽ€


๐ŸŒˆ RESULT ๐ŸŒˆ

the result of the sizeof operator is an unsigned integer type, defined in the stddef.h header as a size_t. it's giving "i'm a size_t and i'm proud". ๐Ÿ‘‘


โญ NOTES โญ

it is a common mistake to apply sizeof to a dynamically allocated array. like, don't do this bestie:

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 (sizeof(char *)), not the allocated memory. it's giving "i measured the wrong thing" energy. ๐Ÿ’€

sizeof determines the size of the result of the expression given, but does not evaluate the expression:

int a = 42;
printf("%ld - %d\n", sizeof(a = 10), a); /* Result: "4 - 42" */

see? a is still 42. sizeof said "i'm just looking, not touching". ๐Ÿ™ˆโœจ

since it is evaluated by the compiler and not the preprocessor, the sizeof operator cannot be used in a preprocessor expression. so like, don't try it. it won't work. no cap. ๐Ÿšซ


๐Ÿฆ‹ SEE ALSO ๐Ÿฆ‹

arch(7), operator(7)

go read those too, bestie. knowledge is power. ๐Ÿ’œ


๐Ÿ’– STANDARDS ๐Ÿ’–

the sizeof operator conforms to ANSI X3.159-1989 ("ANSI C89").

handling of flexible array members in structures conforms to ISO/IEC 9899:1999 ("ISO C99").

standards queen. periodt. ๐Ÿ‘‘


๐ŸŒธ AUTHORS ๐ŸŒธ

this manual page was written by Jan Schaumann <jschauma@netmeister.org>. iconic behavior. ๐Ÿซถ


and that's the tea, bestie. this page was brought to you by the Debian crew on December 12, 2022 โ€” like, a little vintage but still slaying. now go measure your data responsibly. ily. ๐Ÿ’–โœจ๐ŸŽ€๐Ÿฆ‹๐Ÿ’…

๐Ÿ“ฆ where this page comes from