sizeof Operators

C++ provides the unary operator sizeof to determine the size of an array (or of any other data type, variable or constant) in bytes during program compilation. When applied to the name of an array, as in Fig. 8.16 (line 14), the sizeof operator returns the total number of bytes in the array as a value of type size_t (an alias for unsigned int on most compilers). Note that this is different from the size of a vector< int >, for example, which is the number of integer elements in the vector. The computer we used to compile this program stores variables of type double in 8 bytes of memory, and array is declared to have 20 elements (line 12), so array uses 160 bytes in memory. When applied to a pointer parameter (line 24) in a function that receives an array as an argument, the sizeof operator returns the size of the pointer in bytes (4), not the size of the array.

Figure 8.16. sizeof operator when applied to an array name returns the number of bytes in the array.

(This item is displayed on page 422 in the print version)

 1 // Fig. 8.16: fig08_16.cpp
 2 // Sizeof operator when used on an array name
 3 // returns the number of bytes in the array.
 4 #include 
 5 using std::cout;
 6 using std::endl;
 8 size_t getSize( double * ); // prototype
10 int main()
11 {
12 double array[ 20 ]; // 20 doubles; occupies 160 bytes on our system
14 cout << "The number of bytes in the array is " << sizeof( array );
16 cout << "
The number of bytes returned by getSize is "
17 << getSize( array ) << endl;
18 return 0; // indicates successful termination
19 } // end main
21 // return size of ptr 
22 size_t getSize( double *ptr )
23 { 
24  return sizeof( ptr ); 
25 } // end function getSize 
 The number of bytes in the array is 160
 The number of bytes returned by getSize is 4

Common Programming Error 8.7

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

[Note: When the Borland C++ compiler is used to compile Fig. 8.16, the compiler generates the warning message "Parameter 'ptr' is never used in function get-Size(double *)." This warning occurs because sizeof is actually a compile-time operator; thus, variable ptr is not used in the function's body at execution time. Many compilers issue warnings like this to let you know that a variable is not being used so that you can either remove it from your code or modify your code to use the variable properly. Similar messages occur in Fig. 8.17 with various compilers.]

Figure 8.17. sizeof operator used to determine standard data type sizes.

(This item is displayed on page 423 in the print version)

 1 // Fig. 8.17: fig08_17.cpp
 2 // Demonstrating the sizeof operator.
 3 #include 
 4 using std::cout;
 5 using std::endl;
 7 int main()
 8 {
 9 char c; // variable of type char
10 short s; // variable of type short
11 int i; // variable of type int
12 long l; // variable of type long
13 float f; // variable of type float
14 double d; // variable of type double
15 long double ld; // variable of type long double
16 int array[ 20 ]; // array of int
17 int *ptr = array; // variable of type int *
19 cout << "sizeof c = " << sizeof c
20 << "	sizeof(char) = " << sizeof( char )
21 << "
sizeof s = " << sizeof s
22 << "	sizeof(short) = " << sizeof( short )
23 << "
sizeof i = " << sizeof i
24 << "	sizeof(int) = " << sizeof( int )
25 << "
sizeof l = " << sizeof l
26 << "	sizeof(long) = " << sizeof( long )
27 << "
sizeof f = " << sizeof f
28 << "	sizeof(float) = " << sizeof( float )
29 << "
sizeof d = " << sizeof d
30 << "	sizeof(double) = " << sizeof( double )
31 << "
sizeof ld = " << sizeof ld
32 << "	sizeof(long double) = " << sizeof( long double )
33 << "
sizeof array = " << sizeof array
34 << "
sizeof ptr = " << sizeof ptr << endl;
35 return 0; // indicates successful termination
36 } // end main
 sizeof c = 1 sizeof(char) = 1
 sizeof s = 2 sizeof(short) = 2
 sizeof i = 4 sizeof(int) = 4
 sizeof l = 4 sizeof(long) = 4
 sizeof f = 4 sizeof(float) = 4
 sizeof d = 8 sizeof(double) = 8
 sizeof ld = 8 sizeof(long double) = 8
 sizeof array = 80
 sizeof ptr = 4

The number of elements in an array also can be determined using the results of two sizeof operations. For example, consider the following array declaration:

double realArray[ 22 ];

If variables of data type double are stored in eight bytes of memory, array realArray contains a total of 176 bytes. To determine the number of elements in the array, the following expression can be used:

sizeof realArray / sizeof( double ) // calculate number of elements

The expression determines the number of bytes in array realArray (176) and divides that value by the number of bytes used in memory to store a double value (8); the result is the number of elements in realArray (22).

Determining the Sizes of the Fundamental Types, an Array and a Pointer

The program of Fig. 8.17 uses the sizeof operator to calculate the number of bytes used to store most of the standard data types. Notice that, in the output, the types double and long double have the same size. Types may have different sizes based on the system the program is run on. On another system, for example, double and long double may be defined to be of different sizes.

Portability Tip 8.3

The number of bytes used to store a particular data type may vary between systems. When writing programs that depend on data type sizes, and that will run on several computer systems, use sizeof to determine the number of bytes used to store the data types.

Operator sizeof can be applied to any variable name, type name or constant value. When sizeof is applied to a variable name (which is not an array name) or a constant value, the number of bytes used to store the specific type of variable or constant is returned. Note that 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 a variable name or constant. Remember that sizeof is an operator, not a function, and that it has its effect at compile time, not execution time.

Common Programming Error 8.8

Omitting the parentheses in a sizeof operation when the operand is a type name is a compilation error.

Performance Tip 8.2

Because sizeof is a compile-time unary operator, not an execution-time operator, using sizeof does not negatively impact execution performance.

Error-Prevention Tip 8.3

To avoid errors associated with omitting the parentheses around the operand of operator sizeof, many programmers include parentheses around every sizeof operand.

Introduction to Computers, the Internet and World Wide Web

Introduction to C++ Programming

Introduction to Classes and Objects

Control Statements: Part 1

Control Statements: Part 2

Functions and an Introduction to Recursion

Arrays and Vectors

Pointers and Pointer-Based Strings

Classes: A Deeper Look, Part 1

Classes: A Deeper Look, Part 2

Operator Overloading; String and Array Objects

Object-Oriented Programming: Inheritance

Object-Oriented Programming: Polymorphism


Stream Input/Output

Exception Handling

File Processing

Class string and String Stream Processing

Web Programming

Searching and Sorting

Data Structures

Bits, Characters, C-Strings and structs

Standard Template Library (STL)

Other Topics

Appendix A. Operator Precedence and Associativity Chart

Appendix B. ASCII Character Set

Appendix C. Fundamental Types

Appendix D. Number Systems

Appendix E. C Legacy Code Topics

Appendix F. Preprocessor

Appendix G. ATM Case Study Code

Appendix H. UML 2: Additional Diagram Types

Appendix I. C++ Internet and Web Resources

Appendix J. Introduction to XHTML

Appendix K. XHTML Special Characters

Appendix L. Using the Visual Studio .NET Debugger

Appendix M. Using the GNU C++ Debugger


show all menu

C++ How to Program
C++ How to Program (5th Edition)
ISBN: 0131857576
EAN: 2147483647
Year: 2004
Pages: 627
Similar book on Amazon © 2008-2017.
If you may any questions please contact us: