sizeof(): Returns the size of a type or object in bytes.size(): Returns the number of elements in containers likestd::vectororstd::string.
What is sizeof() in C++?
Key takeaways:
sizeof()is an operator in C++ that returns the size of its operand in bytes.It can be applied to variables, types, structures, or classes.
The return type of
sizeof()issize_t, an unsigned integer type.Useful for determining the size of arrays, user-defined types, and pointers (size of the pointer itself, not the pointed data).
For arrays, dividing the total size by the element size gives the number of elements.
The sizeof() operator in C++ is used to calculate the size of a parameter passed to it in bytes. The passed parameter can be a variable, structure, class, or a datatype. With the use of sizeof() in programs, developers can determine the amount of memory occupied within the system by different objects, variables, etc.
The sizeof() operator in C++ returns the size of its operand in bytes. The operand can be a variable, a type (like int, double), a user-defined type (like classes or structures), or expressions. sizeof() is not a function but an operator in C++.
Syntax for sizeof()
Here is the syntax of the sizeof() function in C++:
sizeof(parameter)
The sizeof() operator returns a value of type size_t, which is an unsigned integer type that represents the size of any object in bytes.
Use cases for using sizeof()
Here are a few use cases of the sizeof() function:
We can use
sizeof()to find the length of an array.sizeof()can be used to determine the size of user-defined data structures, like structs or classes.sizeof()can be used to determine the size of a pointer itself, but not the size of the memory it points to. In other words,sizeof(ptr)gives the size of the pointer (usually 4 or 8 bytes, depending on the architecture), not the size of the data being pointed to.
Code example
Here is a comprehensive example to understand the sizeof() function:
#include <iostream>using namespace std;struct Employee{int Id;string Name;bool Gender;void size_of_each_variable(){cout<<"Size of Id (int): "<<sizeof(Id)<<endl;cout<<"Size of Name (string): "<<sizeof(Name)<<endl;cout<<"Size of Gender (bool: "<<sizeof(Gender)<<endl;}};int main() {Employee E;size_t e_size;e_size = sizeof(E);cout<<"Size of Employee (User defined datatype): "<<e_size<<endl;E.size_of_each_variable();return 0;}
Code explanation
Let’s try to understand what’s happening in the code:
Lines 4–7: Create a class
Employeewithint,boolandstringvariables.Lines 11–13: Here we call the
sizeof()function.Lines 18–19: Declare
e_sizevariable, a special type of integer(size_t), and storing the size of the objectE.Lines 20–21: Print the total size of the object
Eand the size of each of its member variables by calling its functionsize_of_each_variable().
Conclusion
In conclusion, sizeof() is a powerful tool for memory-related operations in C++, enabling developers to calculate the size of various data types and objects, ensuring efficient memory management and program optimization.
Frequently asked questions
Haven’t found what you were looking for? Contact Us
What is the difference between `sizeof()` and `size()` in C++?
What type does `sizeof()` return in C++?
What is the alternative to `sizeof()` in C++?
Free Resources