January 17, 2003 Re: Let's not allow [] with pointers | ||||
---|---|---|---|---|
| ||||
Posted in reply to Ilya Minkov | I say we keep array indexing on pointers and add real rectangular arrays like you say. Rectangular (or multidimensional box) dynamic arrays would be a little pain to implement, but static arrays (dimensions known at compile time) would be easy. Without that, you end up needing little helper or proxy classes for your rows. Sean "Ilya Minkov" <midiclub@8ung.at> wrote in message news:b09a6e$30j4$1@digitaldaemon.com... > Russ Lewis wrote: > > It's a very good syntax, for that purpose. > > > > The problem is, that I have already had some bugs where I used [] on pointers, > > thinking I was accessing an array. When you have a pointer-to-array or array-of-pointers, it can get problematic. If we didn't allow [] on pointers, > > it would immediately show up those sorts of bugs. > > > If you're getting trouble it's not yet a reason to get rid of the > feature in the language, unless it is misused very often or without > purpose. I guess that if the structs and such are kept, it also should be. > > The example: I want a pretty large array of a size which is not constant. I allocate a dynamic array of the requiered raw length, rows*colums. Then i set a pointer to pointer aray denoting rows, and initialize it. The result: i can access it with 'array[y][x]'. It becomes obvious that it should be that instead of [x][y] after some thinking, but it was a source of a strange behaviour in my early C code. Why the heck are my sprites not rectangular? Because i was drawing screen columns thinking of them as of rows, and since it is not square... > > My point is, there better be a syntax like array[x,y] which would translate into array[y][x]. And multi-dimensional contunuous arrays, with their current dimensions written in their header and used to calculate pointers. Hm. I'm still not sure what is faster for huge 2D arays of unknown size: access through indirection, or access through multiplication? Second can obviously be optimised out by the compiler, by bringing it out of the loops and such. Hm. > > Another thing is, for example, i define a class for a 2D array. I overload the [] oerator, and make it return *something* containing a row, which has to be another class, so that [] can be overloaded on it, right? > > It would be good, if array[x,y,z] gets implemented, that it can be overloaded all at once, to make it all easier. The overloading function would then get, for example, an array of indices. > > -i. > > > -- > > The Villagers are Online! http://villagersonline.com > > > > .[ (the fox.(quick,brown)) jumped.over(the dog.lazy) ] > > .[ (a version.of(English).(precise.more)) is(possible) ] > > ?[ you want.to(help(develop(it))) ] > > > > > |
Copyright © 1999-2021 by the D Language Foundation