8.7 sizeof Operator

The compile time unary operator sizeof determines the size in bytes of a built-in array or of any other data type, variable or constant during program compilation. When applied to a built-in array’s name, as in Fig. 8.13 (line 12), sizeof returns the total number of bytes in the built-in array as a value of type size_t. The computer we used to compile this program stores variables of type double in 8 bytes of memory, and numbers is declared to have 20 elements (line 10), so it uses 160 bytes in memory. When applied to a pointer parameter (line 20) in a function that receives a built-in array as an argument, the sizeof operator returns the size of the pointer in bytes (4 on the system we used)—not the built-in array’s size.

Common Programming Error 8.3

Using the sizeof operator in a function to find the size in bytes of a built-in array parameter results in the size in bytes of a pointer, not the size in bytes of the built-in array.

Fig. 8.13 sizeof operator when applied to a built-in array’s name returns the number of bytes in the built-in array.

Alternate View

 1   // Fig. 8.13: fig08_13.cpp
 2   // Sizeof operator when used on a built-in array's name
 3   // returns the number of bytes in the built-in array.
 4   #include <iostream>
 5   using namespace std;
 6
 7   size_t getSize(double*); // prototype
 8
 9   int main() {
10      double numbers[20]; // 20 doubles; occupies 160 bytes on our system  
11
12      cout << "The number of bytes in the array is " << sizeof(numbers);
13
14      cout << "
The number of bytes returned by getSize is "
15         << getSize(numbers) << endl;
16   }
17
18   // return size of ptr        
19   size_t getSize(double* ptr) {
20      return sizeof(ptr);       
21   }                            

The number of bytes in the array is 160
The number of bytes returned by getSize is 4

The number of elements in a built-in array can be determined using the results of two sizeof operations. For example, to determine the number of elements in the built-in array numbers, use the following expression (which is evaluated at compile time):


sizeof numbers / sizeof(numbers[0])

The expression divides the number of bytes in numbers (160, assuming 8-byte doubles) by the number of bytes in the built-in array’s zeroth element (8)—resulting in the number of elements in numbers (20).

Determining the Sizes of the Fundamental Types, a Built-In Array and a Pointer

Figure 8.14 uses sizeof to calculate the number of bytes used to store many of the standard data types. The output was produced using the default settings in Xcode 7.2 on Mac OS X. Type sizes are platform dependent. When we run this program on Windows, for example, long is 4 bytes and and long long is 8 bytes, whereas on our Mac, they’re both 8 bytes.

Fig. 8.14 sizeof operator used to determine standard data type sizes.

Alternate View

 1   // Fig. 8.14: fig08_14.cpp
 2   // sizeof operator used to determine standard data type sizes.
 3   #include <iostream>
 4   using namespace std;
 5
 6   int main() {
 7      char c; // variable of type char
 8      short s; // variable of type short
 9      int i; // variable of type int
10      long l; // variable of type long
11      long long ll; // variable of type long long
12      float f; // variable of type float
13      double d; // variable of type double
14      long double ld; // variable of type long double
15      int array[20]; // built-in array of int
16      int* ptr{array}; // variable of type int*
17
18      cout << "sizeof c = " << sizeof c
19         << "	sizeof(char) = " << sizeof(char)
20         << "
sizeof s = " << sizeof s
21         << "	sizeof(short) = " << sizeof(short)
22         << "
sizeof i = " << sizeof i
23         << "	sizeof(int) = " << sizeof(int)
24         << "
sizeof l = " << sizeof l
25         << "	sizeof(long) = " << sizeof(long)
26         << "
sizeof ll = " << sizeof ll
27         << "	sizeof(long long) = " << sizeof(long long)
28         << "
sizeof f = " << sizeof f
29         << "	sizeof(float) = " << sizeof(float)
30         << "
sizeof d = " << sizeof d
31         << "	sizeof(double) = " << sizeof(double)
32         << "
sizeof ld = " << sizeof ld
33         << "	sizeof(long double) = " << sizeof(long double)
34         << "
sizeof array = " << sizeof array
35         << "
sizeof ptr = " << sizeof ptr << endl;
36   }

sizeof c = 1    sizeof(char) = 1
sizeof s = 2    sizeof(short) = 2
sizeof i = 4    sizeof(int) = 4
sizeof l = 8    sizeof(long) = 8
sizeof ll = 8   sizeof(long long) = 8
sizeof f = 4    sizeof(float) = 4
sizeof d = 8    sizeof(double) = 8
sizeof ld = 16  sizeof(long double) = 16
sizeof array = 80
sizeof ptr = 8

Portability Tip 8.2

The number of bytes used to store a particular data type may vary among systems. When writing programs that depend on data type sizes, always use sizeof to determine the number of bytes used to store the data types.

Operator sizeof can be applied to any expression or type name. When sizeof is applied to a variable name (which is not a built-in array’s name) or other expression, the number of bytes used to store the specific type of the expression is returned. The parentheses used with sizeof are required only if a type name (e.g., int) is supplied as its operand. The parentheses used with sizeof are not required when sizeof’s operand is an expression. Remember that sizeof is a compile-time operator, so its operand is not evaluated at runtime.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset