Skip to contentSkip to conversation

size_t vs "unsigned long"

DISCUSSION Junio C Hamano 5 participants 8 messages Jun 15, 2016

lore
Share & tools
lore
#0 lore raw

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?

#1 lore raw
Quoted 11 lines · Junio C Hamano

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.

Signature · 3 lines
·O·  Pierre Habouzit
··O                                                [email protected]
OOO                                                http://www.madism.org
#2 lore raw
Quoted 13 lines · Pierre Habouzit
Quoted 11 lines · Junio C Hamano

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.

Quoted 5 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.

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
#3 lore raw
Quoted 20 lines · Jan Wielemaker
Quoted 13 lines · Pierre Habouzit
Quoted 11 lines · Junio C Hamano

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.

Quoted 5 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.

Quoted 3 lines

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 :)

Signature · 3 lines
·O·  Pierre Habouzit
··O                                                [email protected]
OOO                                                http://www.madism.org
#4 lore raw
Quoted 25 lines · Pierre Habouzit
Quoted 20 lines · Jan Wielemaker
Quoted 13 lines · Pierre Habouzit
Quoted 11 lines · Junio C Hamano

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.

Quoted 5 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.

Quoted 3 lines

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

Quoted 7 lines

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
#5 lore raw
In reply to Pierre Habouzit
Quoted 5 lines · Pierre Habouzit

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".

#6 lore raw
In reply to Pierre Habouzit

* Pierre Habouzit:

Quoted 2 lines

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

#7 lore raw
Quoted 4 lines · Florian Weimer

* Pierre Habouzit:

Quoted 1 line

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