The Size of an Array
As mentioned earlier in this lesson, an array consists of consecutive memory locations. Given an array, like this:
data-type Array-Name[Array-Size];
you can then calculate the total bytes of the array by the following expression:
sizeof(data-type) * Array-Size
Here data-type is the data type of the array; Array-Size specifies the total number of elements the array can take. The result returned by the expression is the total number of bytes the array takes.
Another way to calculate the total bytes of an array is simpler; it uses the following expression:
sizeof(Array-Name)
Here Array-Name is the name of the array.
The program in Listing 12.2 shows how to calculate the memory space taken by an array.
TYPE
Listing 12.2. Calculating the size of an array.1: /* 12L02.c: Total bytes of an array */
2: #include <stdio.h>
3:
4: main()
5: {
6: int total_byte;
7: int list_int[10];
8:
9: total_byte = sizeof (int) * 10;
10: printf( "The size of int is %d-byte long.\n", sizeof (int));
11: printf( "The array of 10 ints has total %d bytes.\n", total_byte);
12: printf( "The address of the first element: 0x%x\n", &list_int[0]);
13: printf( "The address of the last element: 0x%x\n", &list_int[9]);
14: return 0;
15: }
OUTPUT
After running the executable 12L02.exe, I have the following output printed on my screen:
ANALYSIS
C:\app>12L02
The size of int is 2-byte long.
The array of 10 ints has total 20 bytes
The address of the first element: 0x1806
The address of the last element: 0x%1818
C:\app>
Note that you might get different address values when you run the program in Listing 12.2 on your machine. However, the difference between the address of the first element and the address of the last element should be the same as the one obtained from the output on my machine.
In Listing 12.2, there is an integer array, list_int, which is declared in line 7. The total memory space taken by the array is the result of multiplying the size of int and the total number of elements in the array. As declared in this example, there are a total of 10 elements in the array list_int.
The statement in line 10 prints out the size of int on my machine. You can see from the output that each integer element in the array takes 2 bytes. Therefore, the total memory space (in bytes) taken by the array is 10 * 2. In other words, the statement in line 9 assigns the value of 20, returned by the sizeof (int) * 10 expression, to the integer variable total_byte. Line 11 then displays the value contained by the total_byte variable on the screen.
To prove that the array does take the consecutive memory space of 20 bytes, the address of the first element in the array is printed out by the statement in line 12. Note that the ampersand (&), which was introduced as the address-of operator in Hour 11, "An Introduction to Pointers," is used in line 12 to obtain the address of the first element, list_int[0], in the array. Here the address of the first element is the start address of the array. From the output, you can see that the address of the list_int[0] element is 0x1806 on my machine.
Then, the &list_int[9] expression in line 13 returns the address of the last element in the array, which is 0x1818 on my machine. Thus, the distance between the last element and the first element is 0x1818_0x1806, or 18 bytes long.
As mentioned earlier in the book, hexadecimal is a 16-based numbering system. We know that 0x1818 minus 0x1806 produces 0x0012 (that is, 0x12). Then 0x12 in hexadecimal is equal to 1*16 + 2 that yields 18 in decimal.
Because each element takes 2 bytes, the total number of bytes taken by the array list_int is indeed 20 bytes. You can calculate it another way: The distance between the last element and the first element is 18 bytes. The total number of bytes taken by the array should be counted from the very first byte in the first element to the last byte in the last element. Therefore, the total number bytes taken by the array is equal to 18 plus 2, that is 20 bytes.
- 13 views