tail head cat sleep
QR code linking to this page

Manual Pages  — sizeof

NAME

sizeof operator – yield the storage size of the given operand

CONTENTS

SYNTAX

Vt)
Vt

DESCRIPTION

The sizeof operator yields the size of its operand. The sizeof operator cannot be applied to incomplete types and expressions with incomplete types (e.g. void, or forward-defined struct foo ), and function types.

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 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. (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.)

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:
".Li sizeof(struct flex)" ".Sy Result (ILP32)" ".Sy Result (LP64)"
Object or type Ta Sy Result (ILP32)
Ta Sy Result (LP64)
sizeof(char) Ta 1
Ta 1
sizeof(int) Ta 4
Ta 4
sizeof(long) Ta 4
Ta 8
sizeof(float) Ta 4
Ta 4
sizeof(double) Ta 8
Ta 8
sizeof(char *) Ta 4
Ta 8

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' };

".Li sizeof(struct flex)" ".Sy Result"
Object or type Ta Sy Result
sizeof(DATA) Ta 11
sizeof(buf1) Ta 4
sizeof(buf2) Ta 1024
sizeof(buf3) Ta 1024

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, 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:

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; };


".Li sizeof(struct flex)" ".Sy Result (ILP32) " ".Sy Result (LP64)"
Object or type Ta Sy Result (ILP32)
Ta Sy Result (LP64)
sizeof(struct s1) Ta 1
Ta 1
sizeof(struct s2) Ta 8
Ta 16
sizeof(struct s3) Ta 12
Ta 16
sizeof(struct s4) Ta 12
Ta 16
sizeof(struct s5) Ta 36
Ta 56

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[];
}

".Li sizeof(struct flex)" ".Sy Result (ILP32) " ".Sy Result (LP64)"
Object or type Ta Sy Result (ILP32)
Ta Sy Result (LP64)
sizeof(struct flex) Ta 8
Ta 16

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));

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]);

Many systems provide this shortcut as the macro ntimes() via the <sys/param.h> header file.

RESULT

The result of the sizeof operator is an unsigned integer type, defined in the stddef.h header as a size_t.

NOTES

It is a common mistake to apply sizeof to a dynamically allocated array:
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.

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

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

Since it is evaluated by the compiler and not the preprocessor, the sizeof operator cannot be used in a preprocessor expression.

SEE ALSO

arch(7), operator(7)

STANDARDS

The sizeof operator conforms to -ansiC.

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

AUTHORS

This manual page was written by Jan Schaumann <Mt jschauma@netmeister.org>.

sizeof (7) December 12, 2022

tail head cat sleep
QR code linking to this page


Please direct any comments about this manual page service to Ben Bullock. Privacy policy.