3 ms·
I don't see a mistake here, certainly not a "biggest mistake". This is C, not C++. Keep it simple. Here, the idea is that there is no special type for "pointe
by GuB-42 6y ago
I don't see a mistake here, certainly not a "biggest mistake".
This is C, not C++. Keep it simple.
Here, the idea is that there is no special type for "pointer+size" (what the author proposes as an array). Ok, let's add one and see the implications.
- How do I get the size, the number of elements? A "sizeof" like operator?
- Can I resize the array? If yes, how? If no, why?
- What happens if I overflow? Undefined behavior?
- A memcpy-like would be an obvious function to implement, what happens if sizes differ?
- 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?
- About casting, how can I go from a separate pointer and size to an array and vice versa? If it is possible at all.
- What if I do a bit of pointer magic to access the internal representation of the array? Probably undefined behavior.
It is much more complex than "just add array[..]", I expect more tradeoffs, more undefined behaviors (C wouldn't be C without them). Adding complexity to the language can actually make things worse.
As for zero-terminated strings, they have advantages and drawbacks. They are preferred by the C library, but you can do pointer+size if you want by using mem* instead of str* , or %.*s instead of %s in printf (not sure about this one).
- dnautics 6y agoZiglang 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.
- WalterBright 6y ago> How do I get the size, sizeof(array) > the number of elements? array.length > Can I resize the array? If yes, how? If no, why? In D it would be: T* p = realloc(array.ptr, newLength * sizeof(T)); array = p[0 .. newLength]; A C macro could handle that in C. > What happens if I overflow? 2's complement arithmetic happens. > A memcpy-like would be an obvious function to implement, what happens if sizes differ? That would be up to the implementor of the function. There are many ways. > What is the relationship between static arrays (ex: int a[5]) and "pointer+size" arrays? Are these completely different types? Yes, they are different types. > Is there an implicit cast bet>ween the two? Conversion between a static array and the dynamic array should be implicit. Can't go the other way. > About casting, how can I go from a separate pointer and size to an array and vice versa? If it is possible at all. In D, this would be: a = p[0 .. size]; In C, I expect a macro can do it: a = ToArray(p, size); > What if I do a bit of pointer magic to access the internal representation of the array? Then you'd need to be careful to do it right, as in any systems language when you go under the hood. > using mem* instead of str* That only works for some functions. Not fopen(), for example. > or %.*s I use that a lot, but it's annoying because the length argument must be an int, and so must be cast from size_t.