Difference between size_t and unsigned int?
if it is use to represent non negative value so why we not using
unsigned int
instead ofsize_t
Because unsigned int
is not the only unsigned integer type. size_t
could be any of unsigned char
, unsigned short
, unsigned int
, unsigned long
or unsigned long long
, depending on the implementation.
Second question is that
size_t
andunsigned int
are interchangeable or not and if not then why?
They aren't interchangeable, for the reason explained above ^^
.
And can anyone give me a good example of size_t and its brief working ?
I don't quite get what you mean by "its brief working". It works like any other unsigned type (in particular, like the type it's typedeffed to). You are encouraged to use size_t
when you are describing the size of an object. In particular, the sizeof
operator and various standard library functions, such as strlen()
, return size_t
.
Bonus: here's a good article about size_t
(and the closely related ptrdiff_t
type). It reasons very well why you should use it.
What's the difference between size_t and int in C++?
From the friendly Wikipedia:
The stdlib.h and stddef.h header files define a datatype called size_t which is used to represent the size of an object. Library functions that take sizes expect them to be of type size_t, and the sizeof operator evaluates to size_t.
The actual type of size_t is platform-dependent; a common mistake is to assume size_t is the same as unsigned int, which can lead to programming errors, particularly as 64-bit architectures become more prevalent.
Also, check Why size_t matters
What is size_t in C?
From Wikipedia:
According to the 1999 ISO C standard
(C99),size_t
is an unsigned integer
type of at least 16 bit (see sections
7.17 and 7.18.3).
size_t
is an unsigned data type
defined by several C/C++ standards,
e.g. the C99 ISO/IEC 9899 standard,
that is defined instddef.h
.1 It can
be further imported by inclusion of
stdlib.h
as this file internally sub
includesstddef.h
.This type is used to represent the
size of an object. Library functions
that take or return sizes expect them
to be of type or have the return type
ofsize_t
. Further, the most
frequently used compiler-based
operator sizeof should evaluate to a
constant value that is compatible with
size_t
.
As an implication, size_t
is a type guaranteed to hold any array index.
Difference between usage of type size_t and int when used as template type?
std::size_t
is an unsigned type that is at least as large as an unsigned int
.
int
is a signed type, whose upper bound is less than that of an unsigned int
.
There are going to be values which cannot be represented by int
that size_t
can represent.
Passing -1
as an int
results in a negative value. Passing -1
as a size_t
results in a large positive value.
Overflow on int
is undefined behavior; undefined behavior at compile time makes expressions non-constexpr
in some contexts.
Overflow on size_t
is defined behavior, it is mathematics modulo 2^n
for some (unspecified) n
.
Many containers in C++ use size_t
for their indexing, and tuple
uses it for the index of its get
.
There are disadvantages to unsigned values, in that they behave strangely (unlike "real integers") "near zero", while int
behaves strangely far from zero, and being far from zero is a rarer case than being near zero.
size_t
cannot be negative, which is seemingly makes sense to use to represent values that cannot be negative, but the wrap-around behavior can sometimes cause big problems. I find this happens less so with compile-time code however.
You could use ptrdiff_t
, which is basically the signed equivalent of size_t
, as another choice.
There are consequences to both choices. Which of these consequences you want to deal with is up to you. Which is better, a matter of opinion.
comparing int with size_t
It's safe provided the int
is zero or positive. If it's negative, and size_t
is of equal or higher rank than int
, then the int
will be converted to size_t
and so its negative value will instead become a positive value. This new positive value is then compared to the size_t
value, which may (in a staggeringly unlikely coincidence) give a false positive. To be truly safe (and perhaps overcautious) check that the int
is nonnegative first:
/* given int i; size_t s; */
if (i>=0 && i == s)
and to suppress compiler warnings:
if (i>=0 && (size_t)i == s)
size_t vs int in C++ and/or C
In general, size_t
should be used whenever you are measuring the size of something. It is really strange that size_t
is only required to represent between 0 and SIZE_MAX
bytes and SIZE_MAX
is only required to be 65,535...
The other interesting constraints from the C++ and C Standards are:
- the return type of
sizeof()
issize_t
and it is an unsigned integer operator new()
takes the number of bytes to allocate as asize_t
parametersize_t
is defined in<cstddef>
SIZE_MAX
is defined in<limits.h>
in C99 but not mentioned in C++98?!size_t
is not included in the list of fundamental integer types so I have always assumed thatsize_t
is a type alias for one of the fundamental types:char
,short int
,int
, andlong int
.
If you are counting bytes, then you should definitely be using size_t
. If you are counting the number of elements, then you should probably use size_t
since this seems to be what C++ has been using. In any case, you don't want to use int
- at the very least use unsigned long
or unsigned long long
if you are using TR1. Or... even better... typedef
whatever you end up using to size_type
or just include <cstddef>
and use std::size_t
.
unsigned int vs. size_t
The size_t
type is the unsigned integer type that is the result of the sizeof
operator (and the offsetof
operator), so it is guaranteed to be big enough to contain the size of the biggest object your system can handle (e.g., a static array of 8Gb).
The size_t
type may be bigger than, equal to, or smaller than an unsigned int
, and your compiler might make assumptions about it for optimization.
You may find more precise information in the C99 standard, section 7.17, a draft of which is available on the Internet in pdf format, or in the C11 standard, section 7.19, also available as a pdf draft.
Why do I need to use `size_t` in C++?
Its main advantage is that it's the right tool for the job.
size_t
is literally defined to be big enough to represent the size of any object on your platform. The others are not. So, when you want to store the size of an object, why would you use anything else?
You can use int
if you like, but you'll be deliberately choosing the inferior option that leads to bugs. I don't quite understand why you'd want to do so, but hey it's your code.
If you choose to use float
, though, please tell us what program you're writing so we can avoid it. :)
Related Topics
How to Parse a Url in C++ Cross Platform
In C/C++ What's the Simplest Way to Reverse the Order of Bits in a Byte
Calculating Normals in a Triangle Mesh
How to Code a Modulo (%) Operator in C/C++/Obj-C That Handles Negative Numbers
How to Get the Error Message from the Error Code Returned by Getlasterror()
C++: What Regex Library Should I Use
Error Lnk2005, Already Defined
What Is a Subnormal Floating Point Number
Destruction Order of Static Objects in C++
Why Is Template Argument Deduction Disabled With Std::Forward
Pointers, Smart Pointers or Shared Pointers
What Does "Default" Mean After a Class' Function Declaration
Magic Number in Boost::Hash_Combine