Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Where to draw the line between size_t and unsigned int? [duplicate]

Tags:

c++

c

conventions

I'm currently in the process of converting some uses of unsigned int to size_t in my a code base that I have been developing over the years. I understand the difference between the two and that for example unsigned int could be 32-bit while pointers and size_t could be 64-bit. My question is more about where I should use either one and what kind of convention people use for picking between the two.

It's quite clear that memory allocation should take size_t instead of unsigned int as an argument, or that container classes should use size_t for size and indexing like in STL. These are the common cases referred when reading about the benefits of size_t vs unsigned int. However, while working on the code base conversion I stumbled upon quite a few cases in gray areas where I'm not sure which one to use. For example if 4x4 matrix row/column index should be size_t for consistency regardless the index being in range [0, 3], or if screen/texture resolution should use size_t despite of being in range of few thousand, or in general if the reasonable number of objects is expected to be in the range of tens I should still use size_t for consistency.

What kind of coding conventions you use for picking between unsigned int and size_t? Should everything that's representing size (in bytes or objects), or index be always size_t regardless of the reasonably expected range? Is there some widely accepted size_t convention used in well-established libraries that I could follow?

like image 701
JarkkoL Avatar asked Jun 15 '14 04:06

JarkkoL


People also ask

Why size_t is not the same as unsigned int?

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 and unsigned int are interchangeable or not and if not then why?

What is size_t data type in C++?

This data type may be smaller (in number of bits), bigger or exactly the same as unsigned int. size_t type is a base unsigned integer type of C/C++ language. It is the type of the result returned by sizeof operator.

Can size_t be negative in C?

The size_t data type is never negative. Therefore many C library functions like malloc, memcpy and strlen declare their arguments and return type as size_t. For instance, size_t or any unsigned type might be seen used as loop variable as loop variables are typically greater than or equal to 0.

Is size_t used to represent only non-negative values?

I have searched on the internet and everywhere mentioned that size_t is an unsigned type so, it can represent only non-negative values. My first question is: if it is used to represent only non-negative values, why don't we use unsigned int instead of size_t? My second question is: are size_t and unsigned int interchangeable or not?


2 Answers

I think it's simple, although I welcome the slings and arrows.

size_t should be used if it describes something that has a size. (A count. A number of things)

like image 74
Drew Dormann Avatar answered Oct 03 '22 18:10

Drew Dormann


With a 32- to 64-bit port of some legacy code recently on my mind, the key characteristic of size_t in my mind is that it is always big enough to represent your whole address space.

Any other type you can name (including unsigned long) has the potential to put an artificial limit on your data structures at some point in the future. size_t (and its cousin ptrdiff_t) should be the default basis for data structure construction when you can't define a hard a priori upper bound on the domain.

like image 33
Drew Hall Avatar answered Oct 03 '22 18:10

Drew Hall