Questions tagged [size-t]

In C and C++, size_t is the unsigned integer type of the result of the sizeof operator

size_t can store the maximum size of a theoretically possible object of any type (including array).

When indexing C++ containers, such as std::string, std::vector... the appropriate type is the member typedef size_type provided by such containers (it's usually defined as a synonym for size_t).

Further details:

333 questions
802
votes
15 answers

What is size_t in C?

I am getting confused with size_t in C. I know that it is returned by the sizeof operator. But what exactly is it? Is it a data type? Let's say I have a for loop: for(i = 0; i < some_size; i++) Should I use int i; or size_t i;?
Vijay
  • 65,327
  • 90
  • 227
  • 319
575
votes
8 answers

unsigned int vs. size_t

I notice that modern C and C++ code seems to use size_t instead of int/unsigned int pretty much everywhere - from parameters for C string functions to the STL. I am curious as to the reason for this and the benefits it brings.
Rob
  • 76,700
  • 56
  • 158
  • 197
270
votes
8 answers

size_t vs. uintptr_t

The C standard guarantees that size_t is a type that can hold any array index. This means that, logically, size_t should be able to hold any pointer type. I've read on some sites that I found on the Googles that this is legal and/or should always…
Chris Lutz
  • 73,191
  • 16
  • 130
  • 183
267
votes
15 answers

When to use std::size_t?

I'm just wondering should I use std::size_t for loops and stuff instead of int? For instance: #include int main() { for (std::size_t i = 0; i < 10; ++i) { // std::size_t OK here? Or should I use, say, unsigned int instead? …
nhaa123
  • 9,570
  • 11
  • 42
  • 63
183
votes
3 answers

Difference between size_t and std::size_t

What are the differences between size_t and std::size_t in terms of where they are declared, when they should be used and any other differentiating features?
Mankarse
  • 39,818
  • 11
  • 97
  • 141
116
votes
2 answers

Should I use size_t or ssize_t?

At my code, I do not use int or unsigned int. I only use size_t or ssize_t for portable. For example: typedef size_t intc; // (instead of unsigned int) typedef ssize_t uintc; // (instead of int) Because strlen, string, vector... all use…
hgyxbll
  • 1,317
  • 2
  • 9
  • 9
108
votes
9 answers

Clean code to printf size_t in C++ (or: Nearest equivalent of C99's %z in C++)

I have some C++ code that prints a size_t: size_t a; printf("%lu", a); I'd like this to compile without warnings on both 32- and 64-bit architectures. If this were C99, I could use printf("%z", a);. But AFAICT %z doesn't exist in any standard C++…
Justin L.
  • 3,957
  • 3
  • 31
  • 29
105
votes
3 answers

Platform independent size_t Format specifiers in c?

I want to print out a variable of type size_t in C but it appears that size_t is aliased to different variable types on different architectures. For example, on one machine (64-bit) the following code does not throw any warnings: size_t size =…
Ethan Heilman
  • 16,347
  • 11
  • 61
  • 88
103
votes
5 answers

What is a portable method to find the maximum value of size_t?

I'd like to know the maximum value of size_t on the system my program is running. My first instinct was to use negative 1, like so: size_t max_size = (size_t)-1; But I'm guessing there's a better way, or a constant defined somewhere.
Justicle
  • 14,761
  • 17
  • 70
  • 94
74
votes
5 answers

How to cast the size_t to double or int C++

My question is that I have a size_t data, but now I want to convert it to double or int. If I do something like size_t data = 99999999; int convertdata = data; the compiler will report warning. because it maybe overflow. Do you have some method…
user2701639
  • 861
  • 2
  • 8
  • 13
70
votes
4 answers

Why is size_t unsigned?

Bjarne Stroustrup wrote in The C++ Programming Language: The unsigned integer types are ideal for uses that treat storage as a bit array. Using an unsigned instead of an int to gain one more bit to represent positive integers is almost never a good…
Jon
  • 5,275
  • 5
  • 39
  • 51
66
votes
8 answers

Does "std::size_t" make sense in C++?

In some code I've inherited, I see frequent use of size_t with the std namespace qualifier. For example: std::size_t n = sizeof( long ); It compiles and runs fine, of course. But it seems like bad practice to me (perhaps carried over from…
jwfearn
  • 28,781
  • 28
  • 95
  • 122
64
votes
4 answers

What's sizeof(size_t) on 32-bit vs the various 64-bit data models?

On a 64-bit system, sizeof(unsigned long) depends on the data model implemented by the system, for example, it is 4 bytes on LLP64 (Windows), 8 bytes on LP64 (Linux, etc.). What's sizeof(size_t) supposed to be? Does it vary with data model like…
anonymous
60
votes
3 answers

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 …
Coder777
  • 985
  • 2
  • 9
  • 15
59
votes
4 answers

Why is the maximum size of an array "too large"?

I'm under the same impression as this answer, that size_t is always guaranteed by the standard to be large enough to hold the largest possible type of a given system. However, this code fails to compile on gcc/Mingw: #include #include…
Lundin
  • 195,001
  • 40
  • 254
  • 396
1
2 3
22 23