C++ is a free, general purpose. Case-sensitive programming language that supports object-oriented, generic, and procedural programming. Operators are symbols that run operations on values and variables. They form the foundation of any programming language.
In this article, we will discuss the sizeof the operator in C++. So let’s get started!
What is the sizeof Operator in C++?
The sizeof Operator in C++ is a unary operator that calculates the size of data type, constants, and variables at compile time. The size of char, which on most systems is 1 byte (8 bits), is a multiple of the size returned by the operator.size_t is the type of value returned from the sizeof() operator. It is an integer type defined in the header file<stddef.h>.
The operand can be of the following in the sizeof operator in C++:
Type name: For using the C++ sizeof operator with a type name, it must be enclosed in parentheses
An expression: The sizeof operator in C++ can be used with/ without parentheses when used with an expression; also, the expression is not evaluated.
Syntax
The syntax of the sizeof operator, when the operand is the data type, is as follows:
C++
C++
sizeof(data_type);
You can also try this code with Online C++ Compiler
Here, data_type can be the data type of data, structure, variable, union, constants, or any other data type defined by the user.
How sizeof() Operator works?
The sizeof operator in C++ can be used to return the data type/ expression size and work directly by taking these values as an operand. And this operator works on them by returning their size in bytes.
Here, we use the C++ sizeof operator to evaluate the sizes of four different data types. The output clarifies that the int data type takes up 4 bytes of computer memory, float takes up 4, double takes up 8, and char takes up just 1 byte.
Case 2: When an Operand is of Class Type
C++
C++
#include<iostream>
using namespace std;
class TClass {
// Data attribute of the class.
int x;
};
int main() {
TClass obj;
cout << "Size of class is: " << sizeof(obj) << endl; return 0;
}
You can also try this code with Online C++ Compiler
The result for the above-mentioned program shows that the class object uses 4 bytes of memory. Because the object only has one data attribute—a four-byte integer—it was expected that this would happen. The size of the object will increase to 8 bytes if we add one more integer to the class.
Case 3: When an Operand is of Array Type
C++
C++
#include<iostream>
using namespace std;
int main() {
int arr[] = {10, 20, 30, 40, 50};
cout << "Size of array is: " << sizeof(arr) << endl;
return 0;
}
You can also try this code with Online C++ Compiler
We declare an array with five integer values in the program above. We used the sizeof operation in C++ to assess the array. The sizeof the (array) value should be five times the size of the int, or 5 * 4 = 20 bytes, according to the calculation. Using the sizeof operator, the output gives the same result.
Case 4: When an Operand is an Expression
C++
C++
#include<iostream>
using namespace std;
int main() {
int a;
double b;
cout << "Size of expression is: " << sizeof(a + b) << endl;
return 0;
}
You can also try this code with Online C++ Compiler
We declared two variables in the above example, a of type integer and b of type double. The sum of an integer and a double will be saved as a double, which requires 8 bytes of computer memory. Hence the result of sending the expression to the sizeof operator is 8 bytes.
Case 5: When an Operand is of Pointer Type
C++
C++
#include<iostream>
using namespace std;
int main() {
int * pntr1 = new int(7);
cout << "Size of pntr1 is: " << sizeof(pntr1) << endl;
cout << "Size of *pntr1 is: " << sizeof( * pntr1) << endl;
From the output given, we can observe that the size of both the pointers is the same, measuring 8 bytes. However, the size of the *pointer varies due to the discrepancy in the size of the data type pointed to by the pointer in C++. The first pointer indicates a 4-byte integer; the second pointer indicates an 8-byte.
Advantages of Using sizeof Operator
Advantages of using the sizeof() operator are as follows:
It helps with the system type on which the program is running
It helps to calculate and find the size of an array or the number of elements present in an array
It helps check the memory taken by user-defined data types
The sizeof() returns an unsigned integer value which stands for the amount of memory in bytes occupied by the operand.
What does the sizeof() operator do?
The sizeof the operator in C++ gives the amount of storage in bytes needed to store an object of the operand type. This operator enables us to avoid specifying machine-dependent data sizes in our programs.
What is the difference between size() and sizeof() in C++?
The size () operator returns the number of characters in the string, whereas sizeof() is used to get the actual size of any type of data in bytes.
Conclusion
In this article, we discussed the sizeof operators in C++. We started with the introduction to C++ and operators in C++, then we saw the sizeof operator and finally saw examples of it. We hope this blog has helped you enhance your knowledge of the sizeof Operator in C++. If you want to learn more, then check out our articles: