threads / discuss / 10131

size_t vs "unsigned long"

Subject: size_t vs "unsigned long"

## tl;dr

8 messages between Oct 3, 2007 and Oct 5, 2007.

replies: 7people: 5as markdown or json

Junio C Hamano· Oct 3, 2007, 20:30 UTC · lore

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· Oct 3, 2007, 20:48 UTC · re: Junio C Hamano · lore

Re: size_t vs "unsigned long"

On Wed, Oct 03, 2007 at 08:30:04PM +0000, Junio C Hamano wrote:
Show 11 quoted lines
> 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· Oct 3, 2007, 21:05 UTC · re: Pierre Habouzit · lore

Re: size_t vs "unsigned long"

Pierre Habouzit <madcoder@debian.org> writes:
Show 5 quoted lines
>   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· Oct 3, 2007, 21:19 UTC · re: Pierre Habouzit · lore

Re: size_t vs "unsigned long"

On Wednesday 03 October 2007 22:48, Pierre Habouzit wrote:
Show 18 quoted lines
> 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· Oct 3, 2007, 21:36 UTC · re: Jan Wielemaker · lore

Re: size_t vs "unsigned long"

On Wed, Oct 03, 2007 at 09:19:59PM +0000, Jan Wielemaker wrote:
Show 23 quoted lines
> 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
Jan Wielemaker· Oct 4, 2007, 07:16 UTC · re: Pierre Habouzit · lore

Re: size_t vs "unsigned long"

On Wednesday 03 October 2007 23:36, Pierre Habouzit wrote:
Show 32 quoted lines
> 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
Florian Weimer· Oct 3, 2007, 21:36 UTC · re: Pierre Habouzit · lore

Re: size_t vs "unsigned long"

* 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).

Kyle Moffett· Oct 5, 2007, 06:27 UTC · re: Florian Weimer · lore

Re: size_t vs "unsigned long"

On Oct 03, 2007, at 17:36:03, Florian Weimer wrote:
Show 8 quoted lines
> * 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

← back to recent threads