Replies: 3 comments
|
在codeberg上被拒了,哎 |
0 replies
这个咋理解?C 指针怎么区分指向的东西是一个元素还是数组? |
0 replies
能举例说明嘛? |
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
Make
[*]Tan at-least-one many-item pointerA silly thing I found recently in Zig!
I just put a many-item pointer
[*]Tin my function signature, and in the function body I dereferenced this pointer, and then it broke.I found that we need
@ptrCastto cast[*]Tto*T.What the hell? A many-item pointer can naturally be converted to a single-item pointer.
Also, I just get the
ptrfrom[]T, which seems fine, but when the[]Tis empty, it does not returnnull; it gives me an invalid pointer!Reiteration
OK, maybe I'm beating around the bush.
So, as in the title, I'd like to reiterate my position: make
[*]Tmany-item.Why include 0?
Why? C pointers already have the semantics of including 0.
Coercion from
*Tto[*]TWhy? A single-item pointer is not necessarily the same as a many-item pointer.
Why? The C pointer
[*c]Tis unknown-length, and only C pointers are truly unknown.Sentinel pointers
Zig has
[*:0]T, a sentinel-terminated pointer type, which requires the memory pointed to by the pointer to contain at least one element (namely, the sentinel itself).If
[*]Tcan point to empty/zero-length memory, then the semantic distinction between[*:0]Tand[*]Talong the dimension of "whether empty is allowed" becomes unclear.More precisely:
[*:0]Timplicitly guarantees, via the sentinel value, that "at least one element is readable" (the sentinel itself), whereas[*]Tdoes not even have this guarantee.If Zig is willing to pay the type-system cost for sentinel pointers to guarantee "at least one," why does ordinary
[*]Tinstead allow "zero"?std.mem.spanand standard-library consistencystd.mem.spanaccepts[*:0]Tand[*c]T, but rejects[*]Twith@compileErrorbecause it has no sentinel.The standard library's own utilities cannot do anything with a bare
[*]T— it carries neither length nor sentinel nor validity guarantee.This shows that the standard library itself is wary of
[*]T's "no length guarantee."If
[*]Twere changed to mean "at least one," thenstd.mem.spancould at least safely readptr[0], instead of directly erroring out.This is an argument for internal consistency within the standard library.
Empty slices and
[]T.ptrConcerned about
[]T? Good, that's also something I'm concerned about.For an empty slice
[]T, if you access theptr, you get an invalid pointer.This shouldn't occur!
Letting
[]T.ptrreturn?[*]Twould cause fewer bugs, and you would only need to add.?in your trusted code.Proposed semantics:
[*]Tas a superset of*TThe semantics of
[*]Tshould be changed to "at least one element" (at-least-one), rather than the current "unknown length (including 0)."[*]Tshould be changed to be a superset of*T.To repeat,
[*]Tcan do everything*Tcan do (dereference the first element, take its address, pass it as an argument), and it can also do what*Tcannot (pointer arithmetic, indexing, pointing to arbitrary positions).So
[*]Tis a superset of*Tin terms of capabilities.And allow an implicit cast from
[*]Tto*T.Narrowing does not affect correctness.
In other words, converting to
*Tonly strengthens the guarantee, so it is safe to do implicitly.To be clear,
*Tneeds an explicit cast to[*]T.Arbitrary positions and reverse iteration
[*]Tshould not be constrained to point to the start of a memory block;to support reverse iteration and similar operations, it must be able to point to arbitrary positions.
This point is crucial, as it directly dismantles the defense that "
[*]Tis the starting address of a block of memory."In Pointer Reform #770, the original definition of
[*]Tis "pointer to a block of memory of unknown length," a phrasing that implies "starting address" semantics.However, if
[*]Tsupports pointer arithmetic such asptr - 1andptr += n(and it does), then it must be able to point to any position within the block, including the end, the middle, or even the previous position during reverse iteration.A pointer that can point to arbitrary positions no longer has the semantics of "the start of a block"; rather, it is a "many-item pointer to some
T."Under this semantics, "at least one element" is a natural guarantee: the position to which the pointer points must contain a valid
T.One-past-end
Using
p != endfor one-past-end is itself counterintuitive;i < lenis more readable.Pointer comparison is harder to reason about than index comparison, and LLVM bounds-check elimination also favors index forms more.
The concern that "past may overflow" almost never materializes in 64-bit practice, but at the type-system level it is indeed something extra that must be proven, whereas
lendoes not require this.The cost of keeping the current
[*]TIf a type:
[]T(it has no length)[*c]Tto carry C compatibilitythen its only remaining reasons for existing are historical inertia and occasional use in low-level algorithms.
You either remove it, or make it better (at least one element).
Keeping it around as a semantically ambiguous intermediate state imposes a real cognitive cost on language users.
All reactions