Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How is the sizeof operator implemented in c++?

Tags:

c++

sizeof

Can someone point me the to the implementation of sizeof operator in C++ and also some description about its implementation.

sizeof is one of the operator that cannot be overloaded.

So it means we cannot change its default behavior?

like image 834
anand Avatar asked Apr 05 '09 12:04

anand


People also ask

How does sizeof operator work in C?

It is a compile-time unary operator and used to compute the size of its operand. It returns the size of a variable. It can be applied to any data type, float type, pointer type variables. When sizeof() is used with the data types, it simply returns the amount of memory allocated to that data type.

What is sizeof () in C with example?

Need of sizeof() operator Mainly, programs know the storage size of the primitive data types. Though the storage size of the data type is constant, it varies when implemented in different platforms. For example, we dynamically allocate the array space by using sizeof() operator: int *ptr=malloc(10*sizeof(int));

How can you define your own sizeof operator?

The operator sizeof() is a unary operator and is used to calculate the size of any type of data. We can use #define directive to implement our own sizeof() operator which will work exactly same as sizeof() operator. Here, Any_name − The name you want to give to your own sizeof() operator.

Why sizeof is used in C?

The sizeof() operator allows a user to avoid the specification of the machine-dependent type of data sizes in any program. We generally use the sizeof() operator in the C language so that we can determine the total size of the data type or the expression that is specified in the storage units of char-size.


1 Answers

No, you can't change it. What do you hope to learn from seeing an implementation of it?

What sizeof does can't be written in C++ using more basic operations. It's not a function, or part of a library header like e.g. printf or malloc. It's inside the compiler.

Edit: If the compiler is itself written in C or C++, then you can think of the implementation being something like this:

size_t calculate_sizeof(expression_or_type)
{
   if (is_type(expression_or_type))
   {
       if (is_array_type(expression_or_type))
       {
           return array_size(exprssoin_or_type) * 
             calculate_sizeof(underlying_type_of_array(expression_or_type));
       }
       else
       {
           switch (expression_or_type)
           {
                case int_type:
                case unsigned_int_type:
                     return 4; //for example
                case char_type:
                case unsigned_char_type:
                case signed_char_type:
                     return 1;
                case pointer_type:
                     return 4; //for example

                //etc., for all the built-in types
                case class_or_struct_type:
                {
                     int base_size = compiler_overhead(expression_or_type);
                     for (/*loop over each class member*/)
                     {
                          base_size += calculate_sizeof(class_member) +
                              padding(class_member);
                     }
                     return round_up_to_multiple(base_size,
                              alignment_of_type(expression_or_type));
                }
                case union_type:
                {
                     int max_size = 0;
                     for (/*loop over each class member*/)
                     {
                          max_size = max(max_size, 
                             calculate_sizeof(class_member));
                     }
                     return round_up_to_multiple(max_size,
                            alignment_of_type(expression_or_type));
                }
           }
       }
   }
   else
   {
       return calculate_sizeof(type_of(expression_or_type));
   }
}

Note that is is very much pseudo-code. There's lots of things I haven't included, but this is the general idea. The compiler probably doesn't actually do this. It probably calculates the size of a type (including a class) and stores it, instead of recalculating every time you write sizeof(X). It is also allowed to e.g. have pointers being different sizes depending on what they point to.

like image 170
Doug Avatar answered Oct 12 '22 11:10

Doug