Path: ...!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail From: Keith Thompson Newsgroups: comp.lang.c Subject: Re: technology discussion =?utf-8?Q?=E2=86=92?= does the world need a "new" C ? Date: Fri, 05 Jul 2024 11:46:38 -0700 Organization: None to speak of Lines: 89 Message-ID: <87wmlzvfqp.fsf@nosuchdomain.example.com> References: <871q48w98e.fsf@nosuchdomain.example.com> MIME-Version: 1.0 Content-Type: text/plain Injection-Date: Fri, 05 Jul 2024 20:46:39 +0200 (CEST) Injection-Info: dont-email.me; posting-host="4654bd1604bef5db972e5af80bad421d"; logging-data="3570936"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18/KwKRlW1KVGJph5vpnY1y" User-Agent: Gnus/5.13 (Gnus v5.13) Cancel-Lock: sha1:TfjTe4CilzzmzrHFu+ql/7rGBtQ= sha1:Itqs1nW3GGBaqbC+OFPAUvj8ZRg= Bytes: 4837 BGB writes: > On 7/5/2024 3:09 AM, Keith Thompson wrote: >> BGB writes: [...] >>> *1: While not exactly that rare, and can be useful, it is debatable if >>> they add enough to really justify their complexity and relative >>> semantic fragility. If using pointers, one almost invariably needs to >>> fall back to doing "arr[y*N+x]" or similar anyways, so it is arguable >>> that it could make sense to drop them and have people always do their >>> multidimensional indexing manually. >>> >>> Note that multidimensional indexing via multiple levels of pointer >>> indirection would not be effected by this. >> [...] >> Multidimensional arrays in C are not a distinct language feature. >> They are simply arrays of arrays, and all operations on them follow >> from operations on ordinary arrays and pointers. >> Are you proposing (in this hypothetical new language) to add >> an arbitrary restriction, so that arrays can have elements of >> arithmetic, pointer, struct, etc. type, but not of array type? >> I'm not sure I see the point. >> > > As-is, the multidimensional arrays require the compiler to realize > that it needs to multiply one index by the product of all following > indices. > > So, say: > int a[4][4]; > int j, k, l; > l=a[j][k]; > > Essentially needs to be internally translated to, say: > l=a[j*4+k]; > > Eliminating multidimensional arrays eliminates the need for this > translation logic, and the need to be able to represent this case in > the typesystem handling logic (which is, as I see it, in some ways > very different from what one needs for a struct). The C standard has just one normative paragraph that discusses multidimensional arrays. That paragraph is provided for clarity, but could be removed without changing the language. Multidimensional arrays require no work by the compiler that's not already required for any other array type, particularly if you disallow VLAs. Every indexing operation requires multiplying the the index by the size of the element type. As Ben suggests, if you add a special case rule to disallow arrays of arrays, programmers will simply wrap arrays in structures, achieving the same generated code with more obfuscated source code. Removing multidimensional arrays from a C-like language would require adding an arbitrary restriction, not removing a feature. There are probably some optimizations a compiler could perform for multidimensional array indexing -- but if you want to simplify the compiler, just don't do those optimizations. [...] >> I personally would hope that this language would *not* inherit C's >> odd treatment of arrays and pointers. If so, and if it supports >> multidimensional arrays, they'd have to be defined differently than >> the way they're defined in C. >> > > The idea in this case was to make it so that: > int[16]; > Can be functionally identical to: > int* > As far as most of the compiler's type-system handling is concerned. In > this case, one only needs to care about the size when reserving space > for the array. No, arrays are not pointers. As I'm sure you know, an expression of array type is "converted" to a pointer to its initial element in most but not all contexts; the exceptions are sizeof, unary "&", and a string literal used to initialize an array object. There are also special rules about parameters that appear to be of array type. Aside from those rules, array types and pointer types are as distinct as, say, structs and integers. [...] -- Keith Thompson (The_Other_Keith) Keith.S.Thompson+u@gmail.com void Void(void) { Void(); } /* The recursive call of the void */