Autor: michael Data: A: exim-users Assumpte: [Exim] Using int vs. size_t in Exim
Hello,
while debugging some code, I wondered why out of a sudden I got a bunch
of warnings. The following program demonstrates why:
----------------------------------------------------------------------
/*
Don't use `int' as the type of variables in which you store object
sizes. Rationale: C and C++ define the `size_t' type as an unsigned
integral type used for storing object sizes, and thus sizeof() always
returns `size_t'. For that reason, f(&s) will generate:
warning: passing arg 1 of `f' from incompatible pointer type
*/
#include <stdlib.h>
void f(int *ptr)
{
}
int main(int argc, char *argv[])
{
int i;
size_t s;
Exim always uses int to store the size of objects, whereas my code used
size_t. For now, I changed my code to use int for being compatible with
Exim, but I don't like it.
It turns out that gcc 3.2.2 on different operating systems differs by
needing -pedantic on some systems to get that warning, which explains
why I did not see it before (Exim does not use -pedantic).
Apart from the effort to change this and possibly historical reasons,
are there reasons why int is used? Any objections to start introducing
size_t? It could break code that deals with negative object sizes, e.g.
as a loop pre- or post-condition, so some care is needed.