4 ms·
This is how Go does slices, though they are 3 fields: type slice struct { zerothElement *type len int cap int } Though I suspe
by kortex 6y ago
This is how Go does slices, though they are 3 fields:
type slice struct {
zerothElement *type
len int
cap int
}
Though I suspect under the hood the len and cap are ordered first.
If it's good enough for C greybeard Ken Thompson and unix hacker Rob Pike, its good enough for me.
In fact I've looked for a port of go-style slices to C and haven't found one. Maybe people think sds is good enough?
- LukeShu 6y ago> Though I suspect under the hood the len and cap are ordered first. A slice is stored in memory as a `reflect.SliceHeader` https://golang.org/pkg/reflect/#SliceHeader https://golang.org/pkg/reflect/#SliceHeader ; the pointer does come first.
- jgbaldwinbrown 6y agoI actually wrote a (sort of) Go-style slice library. It's a little heavier than Go slices because it allows for dynamic array resizing and tracking the parentage of slices: https://github.com/jgbaldwinbrown/slice https://github.com/jgbaldwinbrown/slice It's just a proof-of-concept, though, and would need a lot of work to be used in anything serious.
- int_19h 6y agoGo has a rather idiosyncratic take on arrays and how they're used, which is reflected in its slices. I can't think of any other language or framework that did it this way.
- saati 6y agoRust Vec has the same representation.