tags:

views:

495

answers:

6

Hi,

I am getting confused with size_t in C. I know that it is returned by the sizeof operator. But what exactly it is? Is it a datatype?

Let's say I have a for loop

int i; or size_t i; //which one should i use?

for(i = 0; i < some_size; i++)
A: 

Read more about it here!

stanigator
+2  A: 

Best answered here: http://en.wikipedia.org/wiki/Size_t

Zac
+1  A: 

The manpage for types.h says:

size_t shall be an unsigned integer type

codaddict
+10  A: 

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 in stddef.h.1 It can be further imported by inclusion of stdlib.h as this file internally sub includes stddef.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 of size_t. Further, the most frequently used compiler-based operator sizeof should evaluate to a constant value that is compatible with size_t.

sblom
you are winning this question by a landslide
Carson Myers
As an implication, size_t is a type guaranteed to hold any array index. Just thought I'd throw it out there..
Michael Foukarakis
"Library functions that take or return sizes expect them to be of type ... size_t" Except that stat() uses off_t for the size of a file
Draemon
A: 

sblom already answered. Just to add advantage of using size_t is in terms of portability.

Adil
+2  A: 

size_t is an unsigned type. So, it can represent non-negative values. You use it when you are counting something, and are sure that it cannot be negative. For example, strlen() returns a size_t because the length of a string has to be at least 0.

In your example, if your loop index is going to be always greater than 0, it might make sense to use size_t, or any other unsigned data type.

When you use a size_t object, you have to make sure that in all the contexts it is used, including arithmetic, you want non-negative values. For example, let's say you have:

size_t s1 = strlen(str1);
size_t s2 = strlen(str2);

and you want to find the difference of the lengths of str2 and str1. You cannot do:

int diff = s2 - s1; /* bad */

This is because s2 - s1 is always going to be a positive number. In this case, depending upon what your use case is, you might be better off using int (or long long) for s1 and s2.

There are some functions in C/POSIX that could/should use size_t, but don't because of historical reasons. For example, the second parameter to fgets should ideally be size_t, but is int.

Alok
@Alok: Two questions: 1) what is the size of [`size_t`](http://codepad.org/osT7Ex1q#output)? 2) why should I prefer `size_t` over something like `unsigned int`?
Lazer
`size_t` isn't guaranteed to be the same thing as `unsigned int` (you seem to be implying that they're the same).
Brendan Long
@Lazer: the size of `size_t` is `sizeof(size_t)`. The C standard guarantees that `SIZE_MAX` will be at least 65535. `size_t` is the type returned by `sizeof` operator, and is used in the standard library (for example `strlen` returns `size_t`). As Brendan said, `size_t` need not be the same as `unsigned int`.
Alok
@Alok, @Brendan Long: When you say `size_t` is not the same as `unsigned int`, I guess you mean that it is not guaranteed to be an `int`, though it actually is `unsigned`. Is that true?
Lazer
@Lazer - yes, `size_t` is guaranteed to be an unsigned type.
Alok
@Alok : thanks!
Lazer