Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the return type of sizeof operator?

What is the return type of sizeof operator? cppreference.com & msdn says sizeof returns size_t. Does it really return a size_t? I'm using VS2010 Professional, and targeting for x64.

int main() {     int size   = sizeof(int);     // No warning     int length = strlen("Expo");  //warning C4267: 'initializing' : conversion from 'size_t' to 'int', possible loss of data     return 0; } 

I have this question because first line is not issuing any warning, whereas the second does. Even if I change it to char size, I don't get any warnings.

like image 845
Coder777 Avatar asked Nov 08 '13 23:11

Coder777


People also ask

What is the use of sizeof () operator?

You can use the sizeof operator to determine the size that a data type represents. For example: sizeof(int); The sizeof operator applied to a type name yields the amount of memory that can be used by an object of that type, including any internal or trailing padding.

Does sizeof in C return bytes?

sizeof always returns size as the number of bytes. But according to wikipedia: In the programming languages C and C++, the unary operator sizeof is used to calculate the size of any datatype, measured in the number of bytes required to represent the type.

What is the type of sizeof?

sizeof can be applied to any data-type, including primitive types such as integer and floating-point types, pointer types, or compound datatypes such as Structure, union etc. sizeof() operator is used in different way according to the operand type.


Video Answer


2 Answers

C++11, §5.3.3 ¶6

The result of sizeof and sizeof... is a constant of type std::size_t. [ Note: std::size_t is defined in the standard header (18.2). — end note ]

You can also do a quick check:

#include <iostream> #include <typeinfo> #include <cstdlib>  int main() {     std::cout<<(typeid(sizeof(int))==typeid(std::size_t))<<std::endl;     return 0; } 

which correctly outputs 1 on my machine.

As @Adam D. Ruppe said in the comment, probably the compiler does not complain because, since it already knows the result, it knows that such "conversion" is not dangerous

like image 159
Matteo Italia Avatar answered Oct 03 '22 02:10

Matteo Italia


size_t is an alias of some implementation-defined unsigned integral type. In C++ opposite to C where sizeof operator may be applied to VLA arrays the operand of sizeof operator is not evaluated (at run time). It is a constant. If the value of sizeof operator can be fit into int type the compiler does not issue a warning. In the second example std::strlen is evaluated at run time so its result can do not fit into int so the compiler issues a warning. You could substitute std:;strlen with your own constexpr function (some recursive function). In this case if the result can fit into int I think that the compiler will not issue a warning.

like image 26
Vlad from Moscow Avatar answered Oct 03 '22 02:10

Vlad from Moscow