2

I know uintptr follow same rules for its size with uint in Go; I guess uint and uintptr are the same and only they are different in the use case. so why uintptr is not an alias for uint in go built-ins like how byte is an alias for uint8 and rune for int32?

abgr
  • 63
  • 9

1 Answers1

3

From the Go spec, here is how the sizes of the "unsized" integer data types are defined:

uint either 32 or 64 bits

int same size as uint

uintptr an unsigned integer large enough to store the uninterpreted bits of a pointer value

As you can see, both types have a distinct size definition. Even if they are coincidentally the same size on most (?) platforms, there's no language-level reason why these types should be conflated.

There's also the benefit of having a distinct type for pointer integers: as Go has strict typing, you cannot use a uintptr as any other int type without converting it first. This is important because uintptr is primarily used for unsafe purposes in Go, so having a distinct type gives strong indications or errors when you might be misusing unsafe features (or at least, it will make you think twice about it).

Hymns For Disco
  • 7,530
  • 2
  • 17
  • 33