2 ms·
Ziglang is basically this plus getting rid of the preprocessor and almost all UB. It's extremely clean, quite safe, and in many ways far far simpler than C. H
by dnautics 6y ago
Ziglang is basically this plus getting rid of the preprocessor and almost all UB. It's extremely clean, quite safe, and in many ways far far simpler than C.
Here are it's answers:
- How do I get the size, the number of elements?
Builtin .len field operator. @sizeOf works too.
- Can I resize the array? If yes, how? If no, why?
No. Array lengths are comptime known; there is something called a slice which is runtime known and bounds checked in safe releases.
- What happens if I overflow? Undefined behavior?
Panic, in safe releases. UB in dangerous releases (small or fast)
- A memcpy-like would be an obvious function to implement, what happens if sizes differ?
Bounds checked at runtime for safe releases.
- What is the relationship between static arrays (ex: int a[5]) and "pointer+size" arrays? Are these completely different types? Is there an implicit cast between the two?
Yes, and yes. Arrays can implicitly be converted to slices at compile time; slicing into a slice with compile-time known index width yields an array.
- About casting, how can I go from a separate pointer and size to an array and vice versa? If it is possible at all.
There is an escape hatch function for this.
- What if I do a bit of pointer magic to access the internal representation of the array? Probably undefined behavior.
It's defined, but unsafe.
Do I haven't really worked too much in zig (it's not my daily driver) but I think it says something that all of these questions have answers to them, they are sensible, and very easy to remember.