Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Inconsistency for size_t and sizeof

Tags:

c++

sizeof

Isn't it bothersome that the result of a native operator cannot be defined without including a header file?

According to this page, size_t is defined in headers cstddef, cstdio, cstring, ctime, and cstdlib. Thus, if neither of those headers is included then size_t should be undefined. However, the following program compiles without any warning (using MSVC 2015RC).

int main() {     auto d_Size = sizeof( int );     return 0; } 

It seems that size_t is somewhat of a bastard between a native type and a typedef. What does the standard say?

like image 282
Hector Avatar asked May 13 '15 05:05

Hector


People also ask

Does sizeof return Size_t?

size_t is an unsigned integer type, which is defined in the stddef. h header. It can hold any value returned by the sizeof operator, and has a min value of 0 , and a max value of the largest value, that can be returned by the sizeof operator.

What is difference between size Size_t?

The type's size is chosen so that it could store the maximum size of a theoretically possible array of any type. On a 32-bit system size_t will take 32 bits, on a 64- bit one 64 bits. In other words, a variable of size_t type can safely store a pointer.

Is it better to use Size_t or int?

When writing C code you should always use size_t whenever dealing with memory ranges. The int type on the other hand is basically defined as the size of the (signed) integer value that the host machine can use to most efficiently perform integer arithmetic.

Is Size_t guaranteed to be unsigned?

Yes, size_t is guaranteed to be an unsigned type.


2 Answers

5.3.3 Sizeof [expr.sizeof]

1) The sizeof operator yields the number of bytes in the object representation of its operand. The operand is either an expression, which is an unevaluated operand (Clause 5), or a parenthesized type-id. The sizeof operator shall not be applied to an expression that has function or incomplete type, to the parenthesized name of such types, or to a glvalue that designates a bit-field. sizeof(char), sizeof(signed char) and sizeof(unsigned char) are 1. The result of sizeof applied to any other fundamental type (3.9.1) is implementation-defined. [ Note: in particular, sizeof(bool), sizeof(char16_t), sizeof(char32_t), and sizeof(wchar_t) are implementation-defined.75 — end note ] [ Note: See 1.7 for the definition of byte and 3.9 for the definition of object representation. — end note ]

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 <cstddef> (18.2). — end note ]

However, std::size_t is just a type alias. The sizeof operator can return its result without any need of "accessing" the type alias; the result of sizeof is some fundamental type (implementation defined), which is then aliased as std::size_t in <cstddef>.

Note also that in C++ typedef or using do not define a new type (i.e. a strong type), but only an alias (i.e. their typeid are the same). Hence, in your case, auto just deduces the fundamental type returned by the sizeof operator, which is the same as the type alias std::size_t. No problem for the compiler.

like image 136
vsoftco Avatar answered Sep 21 '22 05:09

vsoftco


According to the C++ standard, std::size_t is defined in <cstddef>.

5.3.3 Sizeof

...

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 <cstddef> (18.2). — end note ]

like image 20
R Sahu Avatar answered Sep 23 '22 05:09

R Sahu