# size_t vs "unsigned long"

8 messages from 2007-10-03 to 2007-10-05. Participants: Junio C Hamano, Pierre Habouzit, Jan Wielemaker, Florian Weimer, Kyle Moffett.
Thread: https://gitlist.dev/t/10131

## Junio C Hamano, 2007-10-03 20:30

Subject: size_t vs "unsigned long"
Message-ID: <7vabr0djqr.fsf@gitster.siamese.dyndns.org>
URL: https://gitlist.dev/e/7vabr0djqr.fsf%40gitster.siamese.dyndns.org

```
Traditionally, inside git, we have used the length of things
with "unsigned long" for pretty much anything, except where we
wanted the length exactly sized we used int32_t, uint64_t and
friends.

A few places pass pointer to unsigned long as the second
parameter to strbuf_detach(), triggering type mismatch warnings.
An easy way out is to change strbuf_detach() to take a pointer
to ulong but I think it is going backwards.  Most places that
use "unsigned long" can safely be converted (and made more
correct) to use size_t.

Any opinions?

```

## Pierre Habouzit, 2007-10-03 20:48

Subject: Re: size_t vs "unsigned long"
Message-ID: <20071003204801.GC28188@artemis.corp>
URL: https://gitlist.dev/e/20071003204801.GC28188%40artemis.corp
In-Reply-To: <7vabr0djqr.fsf@gitster.siamese.dyndns.org>

```
On Wed, Oct 03, 2007 at 08:30:04PM +0000, Junio C Hamano wrote:
> Traditionally, inside git, we have used the length of things
> with "unsigned long" for pretty much anything, except where we
> wanted the length exactly sized we used int32_t, uint64_t and
> friends.
> 
> A few places pass pointer to unsigned long as the second
> parameter to strbuf_detach(), triggering type mismatch warnings.
> An easy way out is to change strbuf_detach() to take a pointer
> to ulong but I think it is going backwards.  Most places that
> use "unsigned long" can safely be converted (and made more
> correct) to use size_t.

  Well, afaict, on every linux archs I know of, unsigned longs and
size_t are the same. Though, I don't know if that holds for the msys
port, and if that does not holds, then a s/unsigned long/size_t/ would
help them. Else, for consistency sake, I believe the change is a good
one.

-- 
·O·  Pierre Habouzit
··O                                                madcoder@debian.org
OOO                                                http://www.madism.org

```

## Junio C Hamano, 2007-10-03 21:05

Subject: Re: size_t vs "unsigned long"
Message-ID: <7v641newom.fsf@gitster.siamese.dyndns.org>
URL: https://gitlist.dev/e/7v641newom.fsf%40gitster.siamese.dyndns.org
In-Reply-To: <20071003204801.GC28188@artemis.corp>

```
Pierre Habouzit <madcoder@debian.org> writes:

>   Well, afaict, on every linux archs I know of, unsigned longs and
> size_t are the same. Though, I don't know if that holds for the msys
> port, and if that does not holds, then a s/unsigned long/size_t/ would
> help them. Else, for consistency sake, I believe the change is a good
> one.

FWIW, I am already getting bitten on a FC box with gcc 4.1.1
20060525 that warns about the wrong type being passed, as I
usually build things with -Werror; the issue is not just "they
are of the same underlying type".

```

## Jan Wielemaker, 2007-10-03 21:19

Subject: Re: size_t vs "unsigned long"
Message-ID: <200710032320.00263.wielemak@science.uva.nl>
URL: https://gitlist.dev/e/200710032320.00263.wielemak%40science.uva.nl
In-Reply-To: <20071003204801.GC28188@artemis.corp>

```
On Wednesday 03 October 2007 22:48, Pierre Habouzit wrote:
> On Wed, Oct 03, 2007 at 08:30:04PM +0000, Junio C Hamano wrote:
> > Traditionally, inside git, we have used the length of things
> > with "unsigned long" for pretty much anything, except where we
> > wanted the length exactly sized we used int32_t, uint64_t and
> > friends.
> >
> > A few places pass pointer to unsigned long as the second
> > parameter to strbuf_detach(), triggering type mismatch warnings.
> > An easy way out is to change strbuf_detach() to take a pointer
> > to ulong but I think it is going backwards.  Most places that
> > use "unsigned long" can safely be converted (and made more
> > correct) to use size_t.
>
>   Well, afaict, on every linux archs I know of, unsigned longs and
> size_t are the same. Though, I don't know if that holds for the msys
> port, and if that does not holds, then a s/unsigned long/size_t/ would
> help them. Else, for consistency sake, I believe the change is a good
> one.

Surely on the Microsoft 64-bit compilers size_t is 64-bits and long is
32-bits.  Don't blame me, I'm just the messenger that learned the hard
way ...

	--- Jan

```

## Pierre Habouzit, 2007-10-03 21:36

Subject: Re: size_t vs "unsigned long"
Message-ID: <20071003213601.GD28188@artemis.corp>
URL: https://gitlist.dev/e/20071003213601.GD28188%40artemis.corp
In-Reply-To: <200710032320.00263.wielemak@science.uva.nl>

```
On Wed, Oct 03, 2007 at 09:19:59PM +0000, Jan Wielemaker wrote:
> On Wednesday 03 October 2007 22:48, Pierre Habouzit wrote:
> > On Wed, Oct 03, 2007 at 08:30:04PM +0000, Junio C Hamano wrote:
> > > Traditionally, inside git, we have used the length of things
> > > with "unsigned long" for pretty much anything, except where we
> > > wanted the length exactly sized we used int32_t, uint64_t and
> > > friends.
> > >
> > > A few places pass pointer to unsigned long as the second
> > > parameter to strbuf_detach(), triggering type mismatch warnings.
> > > An easy way out is to change strbuf_detach() to take a pointer
> > > to ulong but I think it is going backwards.  Most places that
> > > use "unsigned long" can safely be converted (and made more
> > > correct) to use size_t.
> >
> >   Well, afaict, on every linux archs I know of, unsigned longs and
> > size_t are the same. Though, I don't know if that holds for the msys
> > port, and if that does not holds, then a s/unsigned long/size_t/ would
> > help them. Else, for consistency sake, I believe the change is a good
> > one.
> 
> Surely on the Microsoft 64-bit compilers size_t is 64-bits and long is
> 32-bits.  Don't blame me, I'm just the messenger that learned the hard
> way ...

  Yeah, I've been wondering, and it's the information I had. well, the
information I had is that sizeof(size_t) is 4 on win32, and 8 on win64,
OTOH (and this one I'm sure), on windows, longs are 32bits on both (32
and 64 bits ABIs).

  So replacing unsigned long with size_t's will help the msys port,
hence I had some insight that this could prove useful, now I'm sure :)

-- 
·O·  Pierre Habouzit
··O                                                madcoder@debian.org
OOO                                                http://www.madism.org

```

## Florian Weimer, 2007-10-03 21:36

Subject: Re: size_t vs "unsigned long"
Message-ID: <877im3khj0.fsf@mid.deneb.enyo.de>
URL: https://gitlist.dev/e/877im3khj0.fsf%40mid.deneb.enyo.de
In-Reply-To: <20071003204801.GC28188@artemis.corp>

```
* Pierre Habouzit:

>   Well, afaict, on every linux archs I know of, unsigned longs and
> size_t are the same.

IIRC, 64-bit Windows uses 64-bit points (duh) and hence a 64-bit
size_t, but still has got 32-bit longs.  Documentation is a bit sparse
on this matter (because you are supposed to use LONG, DWORD and
friends anyway).

```

## Jan Wielemaker, 2007-10-04 07:16

Subject: Re: size_t vs "unsigned long"
Message-ID: <200710040916.55998.wielemak@science.uva.nl>
URL: https://gitlist.dev/e/200710040916.55998.wielemak%40science.uva.nl
In-Reply-To: <20071003213601.GD28188@artemis.corp>

```
On Wednesday 03 October 2007 23:36, Pierre Habouzit wrote:
> On Wed, Oct 03, 2007 at 09:19:59PM +0000, Jan Wielemaker wrote:
> > On Wednesday 03 October 2007 22:48, Pierre Habouzit wrote:
> > > On Wed, Oct 03, 2007 at 08:30:04PM +0000, Junio C Hamano wrote:
> > > > Traditionally, inside git, we have used the length of things
> > > > with "unsigned long" for pretty much anything, except where we
> > > > wanted the length exactly sized we used int32_t, uint64_t and
> > > > friends.
> > > >
> > > > A few places pass pointer to unsigned long as the second
> > > > parameter to strbuf_detach(), triggering type mismatch warnings.
> > > > An easy way out is to change strbuf_detach() to take a pointer
> > > > to ulong but I think it is going backwards.  Most places that
> > > > use "unsigned long" can safely be converted (and made more
> > > > correct) to use size_t.
> > >
> > >   Well, afaict, on every linux archs I know of, unsigned longs and
> > > size_t are the same. Though, I don't know if that holds for the msys
> > > port, and if that does not holds, then a s/unsigned long/size_t/ would
> > > help them. Else, for consistency sake, I believe the change is a good
> > > one.
> >
> > Surely on the Microsoft 64-bit compilers size_t is 64-bits and long is
> > 32-bits.  Don't blame me, I'm just the messenger that learned the hard
> > way ...
>
>   Yeah, I've been wondering, and it's the information I had. well, the
> information I had is that sizeof(size_t) is 4 on win32, and 8 on win64,
> OTOH (and this one I'm sure), on windows, longs are 32bits on both (32
> and 64 bits ABIs).
>
>   So replacing unsigned long with size_t's will help the msys port,
> hence I had some insight that this could prove useful, now I'm sure :)

The other types that are useful are intptr_t and uintptr_t, integers
that are guaranteed to be able to hold a pointer. They are defined by
recent versions of the Microsoft compilers and are in <inttypes.h> in
most POSIX systems (at least I didn't have complaints since I started
using them).  I use them for integers holding mangled pointers.  Of
course most clean C programs should not need that.

	--- Jan

```

## Kyle Moffett, 2007-10-05 06:27

Subject: Re: size_t vs "unsigned long"
Message-ID: <620B5C1C-CFE8-401B-8BC7-D89C8740D63B@mac.com>
URL: https://gitlist.dev/e/620B5C1C-CFE8-401B-8BC7-D89C8740D63B%40mac.com
In-Reply-To: <877im3khj0.fsf@mid.deneb.enyo.de>

```
On Oct 03, 2007, at 17:36:03, Florian Weimer wrote:
> * Pierre Habouzit:
>> Well, afaict, on every linux archs I know of, unsigned longs and  
>> size_t are the same.
>
> IIRC, 64-bit Windows uses 64-bit points (duh) and hence a 64-bit  
> size_t, but still has got 32-bit longs.  Documentation is a bit  
> sparse on this matter (because you are supposed to use LONG, DWORD  
> and friends anyway).

For reference, Linux is always an LP32 (long-and-pointer-are-32-bit)  
or LP64 (long-and-pointer-are-64-bit) platform.  On the other hand,  
for crappy backwards-compat reasons, Windows is either LP32 or LLP64  
(long-long-and-pointer-are-64-bit).  I think most of the remaining  
UNIXes fall into the LP32/LP64 category, the LLP64 platforms are  
fairly rare (thankfully).

Cheers,
Kyle Moffett

```
