You have already learned that the sizeof( ) operator returns the physical size, in bytes, of the data object to which it is applied. You can use it with any type of data object except bit-fields. A frequent use of sizeof( ) is to determine the physical size of a variable when the size of the variable’s data type can vary from machine to machine. You have already seen how an integer can be either 2 or 4 bytes, depending on the machine being used. If an additional amount of memory to hold seven integers will be requested from the operating system, some way is needed to determine whether 14 bytes (7 2 bytes/integer) or 28 bytes (7 4 bytes/integer) are needed. The following program automatically takes this into consideration (and prints a value of 28 for systems allocating 4 bytes per integer cell):
/* * sizeof.c * A C program applying sizeof( ) to determine an array’s size * Copyright (c) Chris H. Pappas and William H. Murray, 1998 */
#include <stdio.h>
#define iDAYS_OF_WEEK 7
main( ) { int ivideo_library[iDAYS_OF_WEEK]={1,2,3,4,5,6,7};
printf(“There are %d number of bytes in the array” “ ivideo_library.\n”,(int)sizeof(ivideo_library));
return(0); }
This concept becomes essential when the program must be portable and independent of any particular hardware. If you are wondering why there is an int type cast on the result returned by sizeof( ), in the ANSI C standard sizeof( ) does not return an int type. Instead, sizeof( ) returns a data type, size_t, that is large enough to hold the return value. The ANSI C standard added this to C because on certain computers an integer is not big enough to represent the size of all data items. In the example, casting the return value to an integer allows it to match the %d conversion character of the printf( ) function. Otherwise, if the returned value had been larger than an integer, the printf( ) function would not have worked properly.
By changing iarray‘s data type in the following program, you can explore how various data types are stored internally:
/* * array.c * A C program illustrating contiguous array storage * Copyright (c) Chris H. Pappas and William H. Murray, 1998 */
#include <stdio.h>
#define iDAYS 7
main( ) { int index, iarray[iDAYS];
printf(“sizeof(int) is %d\n\n”, (int)sizeof(int));
Notice how the & (address) operator can be applied to any variable, including an array element. An array element can be treated like any other variable; its value can form an expression, it can be assigned a value, and it can be passed as an argument (or parameter) to a function. In this example you can see how the array elements’ addresses are exactly 2 bytes apart. You will see the importance of this contiguous storage when you use arrays in conjunction with pointer variables.
The following C++ listing is similar in structure to the program just discussed:
// // array.cpp // A C++ program illustrating contiguous array storage // Copyright (c) Chris H. Pappas and William H. Murray, 1998 //
#include <iostream.h>
#define iMAX 10
main( ) { int index, iarray[iMAX];
cout << “sizeof(int) is ” << (int)sizeof(int) << “\n\n”;
for(index = 0; index < iMAX; index++) cout << “&iarray[” << index << “] = ” << index << &iarray[index] << endl;