sizeof

From Infogalactic: the planetary knowledge core
Jump to: navigation, search

In the programming languages C and C++, the unary operator sizeof generates the size of a variable or datatype, measured in the number of char size storage units required for the type. As such, the construct sizeof (char) is guaranteed to be 1. The actual number of bits of type char is specified by the preprocessor macro CHAR_BIT, defined in the include file limits.h. On most modern systems this is eight bits. The result has an unsigned integral type that is usually denoted by size_t.

The operator is written preceding its operand, and may be applied either to a variable or any data type specification, including primitive types such as integer and floating-point types, pointer types, or compound datatypes (unions, structs, or C++ classes). When applied to a data type the type must be enclosed in parenthesis.

Need

In many programs, it is useful to know the size of a particular datatype. Though for any given implementation of C or C++ the size of a particular datatype is constant, the sizes of even primitive types in C and C++ are implementation-defined. This is important when allocating a block of memory of the appropriate size.

int *pointer = malloc(10 * sizeof (int));

In this example, malloc allocates memory and returns a pointer to the memory block. The size of the block allocated is equal to the number of bytes for a single object of type int multiplied by 10, ensuring enough space for ten integers.

It is generally not safe to presume to know the size of any datatype. For example, even though most implementations of C and C++ on 32-bit systems define type int to be four octets, this size may change when code is ported to a different system, breaking the code. The exception to this is the char data type, whose size is always 1 in any standards-compliant C implementation. In addition, it is frequently difficult to predict the sizes of compound datatypes such as a struct or union, due to padding. The use of sizeof enhances readability, since it avoids unnamed numeric constants (magic numbers).

Use

The sizeof operator is used to determine the amount of space a designated datatype would occupy in memory. To use sizeof, the keyword "sizeof" is followed by a type name or an expression (which may be merely a variable name). If a type name is used, it must always be enclosed in parentheses, whereas expressions can be specified with or without parentheses. A sizeof expression results in a value equal to the size in bytes of the datatype or expression (with datatypes, sizeof evaluates to the size of the memory representation for an object of the specified datatype; for expressions it evaluates to the representation size for the type that would result from evaluation of the expression, which however is not evaluated). For example, since sizeof(char) is defined to be 1[1] and assuming the integer type is four bytes long, the following code prints 1,4:

/* the following code fragment illustrates the use of sizeof 
 * with variables and expressions (no parentheses needed),
 * and with type names (parentheses needed)
 */

char c;

printf("%zu,%zu\n", sizeof c, sizeof (int));

Because types are not known to the C preprocessor, sizeof cannot be used in #if expressions.

Certain standard headers such as stddef.h define size_t to denote the unsigned integral type of the result of a sizeof expression. The printf width specifier z should be used to format that type.

Application to arrays

<templatestyles src="Module:Hatnote/styles.css"></templatestyles>

When sizeof is applied to the name of an array, the result is the size in bytes of the whole array. This is one of the few exceptions to the rule that the name of an array is converted to a pointer to the first element of the array, and is possible just because the actual array size is fixed and known at compile time, when the sizeof operator is evaluated. The following program uses sizeof to determine the size of a declared array, avoiding a buffer overflow when copying characters:

#include <stdio.h>
#include <string.h>

int main(int argc, char** argv)
{
  char buffer[10]; /* Array of 10 chars */
  
  /* Copy at most 9 characters from argv[1] into buffer.
   *  sizeof (char) is defined to always be 1.
   */
  strncpy(buffer, argv[1], sizeof buffer - sizeof buffer[0]);
  
  /* Ensure that the buffer is null-terminated: */
  buffer[sizeof buffer - 1] = '\0';

  return 0;
}

Here, sizeof buffer is equivalent to 10 * sizeof buffer[0], which evaluates to 10.

C99 adds support for flexible array members to structures. This form of array declaration is allowed as the last element in structures only, and differs from normal arrays in that no length is specified to the compiler. For a structure named s containing a flexible array member named a, sizeof s is therefore equivalent to offsetof(s, a):

#include <stdio.h>

struct flexarray {
    char val;
    int array[];  /* Flexible array member; must be last element of struct */
};

int main(int argc, char** argv)
{
    printf("sizeof (struct flexarray) = %zu\n", sizeof (struct flexarray));
    return 0;
}

Thus, in this case the sizeof operator returns the size of the structure, including any padding, but without any storage allowed for the array. In the above example, the following output will be produced on most platforms:

sizeof (struct flexarray) = 4

C99 also allows variable length arrays where the length is specified at runtime.[2] In such cases, the sizeof operator is evaluated in part at runtime to determine the storage occupied by the array.

#include <stddef.h>

size_t flexsize(int n)
{
   char b[n+3];      /* Variable length array */
   return sizeof b;  /* Execution time sizeof */
}

int main(void)
{
  size_t size = flexsize(10); /* flexsize returns 13 */
  return 0;
}

sizeof can be used to determine the number of elements in an array, by taking the size of the entire array and dividing it by the size of a single element.

#define Elements_in(arrayname) (sizeof arrayname/sizeof *arrayname)

int main(void)
{
   int tab[10];
   cout << "Number of elements in the array: " << Elements_in(tab) << endl; // yields 10
   return 0;
}

Because this works only for the name of a declared array object, non-trivial revision will be necessary when the code is changed to use a pointer instead of an array name.

Incomplete types

sizeof can only be applied to "completely" defined types. With arrays, this means that the dimensions of the array must be present in its declaration, and that the type of the elements must be completely defined. For structs and unions, this means that there must be a member list of completely defined types. For example, consider the following two source files:

/* file1.c */
int arr[10];
struct x {int one; int two;};
/* more code */

/* file2.c */
extern int arr[];
struct x;
/* more code */

Both files are perfectly legal C, and code in file1.c can apply sizeof to arr and struct x. However, it is illegal for code in file2.c to do this, because the definitions in file2.c are not complete. In the case of arr, the code does not specify the dimension of the array; without this information, the compiler has no way of knowing how many elements are in the array, and cannot calculate the array's overall size. Likewise, the compiler cannot calculate the size of struct x because it does not know what members it is made up of, and therefore cannot calculate the sum of the sizes of the structure's members (and padding). If the programmer provided the size of the array in its declaration in file2.c, or completed the definition of struct x by supplying a member list, this would allow the application of sizeof to arr or struct x in that source file.

Object members

C++11 introduced the possibility to apply the sizeof parameter to specific members of a class without the necessity to instantiate the object to achieve this.[3] For example:

#include <iostream>

using namespace std;

struct foo {
  int a;
  int b;
};

int main(void)
{
  cout << sizeof foo::a << endl << sizeof(foo) << endl;
  return 0;
}

This yields in most platforms:

4
8

Variadic template packs

C++11 introduced variadic templates; the keyword sizeof followed by ellipsis returns the number of elements in a parameter pack.


template <typename... Args>
void print_size(Args... args) 
{
  cout << sizeof...(args) << endl;
}

int main(void) 
{
  print_size(); // outputs 0
  print_size("Is the answer", 42, true); // outputs 3
}

Implementation

When applied to a fixed-length datatype or variable, expressions with the operator sizeof are evaluated during program compilation; they are replaced by constant result-values. The C99 standard introduced introduced variable-length arrays (VLAs), which required evaluation for such expressions during program execution. In many cases, the implementation specifics may be documented in an application binary interface (ABI) document for the platform, specifying formats, padding, and alignment for the data types, to which the compiler must conform.

Structure padding

When calculating the size of any object type, the compiler must take into account any required data structure alignment to meet efficiency or architectural constraints. Many computer architectures do not support multiple-byte access starting at any byte address that is not a multiple of the word size, and even when the architecture allows it, usually the processor can fetch a word-aligned object faster than it can fetch an object that straddles multiple words in memory.[4] Therefore, compilers usually align data structures to at least a word boundary, and also align individual members to their respective boundaries. In the following example, the structure student is likely to be aligned on a word boundary, which is also where the member grade begins, and the member age is likely to start at the next word address. The compiler accomplishes the latter by inserting padding bytes between members as needed to satisfy the alignment requirements. There may also be padding at the end of a structure to ensure proper alignment in case the structure is used as an element of an array.

Thus, the aggregate size of a structure in C can be greater than the sum of the sizes of its individual members. For example, on many systems the following code prints 8:

struct student {
  char grade; /* char is 1 byte long */
  int age; /* int is 4 bytes long */
};

printf("%zu", sizeof (struct student));

See also

References

  1. Lua error in package.lua at line 80: module 'strict' not found.
  2. Lua error in package.lua at line 80: module 'strict' not found.
  3. http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2007/n2253.html
  4. Lua error in package.lua at line 80: module 'strict' not found.