C++ Beginner

cpp_011: Understanding the sizeof Operator in C++

codeaddict 2025. 2. 1. 15:07

In this lesson, we’ll explore the sizeof operator in C++. This operator is used to determine the size (in bytes) of a variable, data type, or entire array. It's important to understand that sizeof does not directly give the number of elements in an array but rather the total memory size. To calculate the number of elements, you need to divide the total size of the array by the size of a single element. Let’s dive into the syntax and an example to understand how it works.


1. Syntax of sizeof Operator

The sizeof operator can be used in two ways:

  1. For Variables or Data Types:
sizeof(variableName);
sizeof(dataType);

 

2. For Arrays:

sizeof(arrayName) / sizeof(arrayName[0]);
  • sizeof(arrayName): Returns the total size of the array in bytes.
  • sizeof(arrayName[0]): Returns the size of a single element in the array.

2. Example: Using sizeof with Arrays

Let’s write a program to calculate the size of an array and the number of elements it contains.

Problem Statement:

Write a C++ program to:

  1. Declare an array of integers.
  2. Use the sizeof operator to determine the total size of the array and the size of a single element.
  3. Calculate and display the number of elements in the array.

Solution Code

#include <iostream>
using namespace std;

int main() {
    int numbers[] = {10, 20, 30, 40, 50};
    // Calculate the total size of the array in bytes
    int totalSize = sizeof(numbers);
    // Calculate the size of a single element in bytes
    int elementSize = sizeof(numbers[0]);
    // Calculate the number of elements in the array
    int numElements = totalSize / elementSize;
    cout << "Total size of the array: " << totalSize << " bytes" << endl;
    cout << "Size of a single element: " << elementSize << " bytes" << endl;
    cout << "Number of elements in the array: " << numElements << endl;
    return 0;
}

Explanation

  1. Total Size of the Array:
  • sizeof(numbers) returns the total size of the array in bytes. For example, if the array has 5 integers and each integer occupies 4 bytes, the total size will be 20 bytes.

2. Size of a Single Element:

  • sizeof(numbers[0]) returns the size of the first element in the array. For an int, this is typically 4 bytes.

3. Number of Elements:

  • Dividing the total size of the array by the size of a single element gives the number of elements in the array.

Output

Total size of the array: 20 bytes  
Size of a single element: 4 bytes  
Number of elements in the array: 5

Key Takeaway

The sizeof operator is a powerful tool for understanding memory allocation and calculating the size of arrays or variables. Use it to make your programs more dynamic and efficient!


 

'C++ Beginner' 카테고리의 다른 글

cpp_012: Introduction to Vectors in C++  (0) 2025.02.17
cpp_012: Multidimensional Arrays in C++  (0) 2025.02.08
cpp_010: Arrays in C++  (0) 2025.01.26
cpp_009: While Loops and Break Statement in C++  (0) 2025.01.15
cpp_008: for Loops in C++  (0) 2025.01.12