Timeline for Why do negative array indices make sense?
Current License: CC BY-SA 3.0
9 events
when toggle format | what | by | license | comment | |
---|---|---|---|---|---|
Mar 28, 2013 at 4:59 | comment | added | Kyle Jones | @Raphael Using negative array indices is natural for some data representations. Consider a SAT solver using two-watched-literals for unit propagation. You'll need an array of watched literal lists, indexed by the literal to be watched. The DIMACS CNF spec uses integers to specify literals, negative integers indicate negated literals. You can use these integers to directly index the watched literals array. Because the internal and external representations are the same, debugging is much more pleasant, believe me. You'll have to do bounds checking in any event. | |
Mar 27, 2013 at 15:46 | comment | added | vonbrand |
@Raphael, a[-1] makes perfect sense for some cases of a , in this particular case it is plain illegal (but not caught by the compiler)
|
|
Mar 27, 2013 at 15:23 | comment | added | Raphael | @vonbrand According to Dave, in some environments the behaviour is well-defined. | |
Mar 27, 2013 at 14:25 | comment | added | vonbrand | @Raphael, if your name is Donald Knuth you'd never even consider invoking undefined behaviour, no matter it is s(h)aves 17 instructions. | |
Mar 27, 2013 at 13:41 | comment | added | Dave Clarke | @DeZéroToxin: An array is really just a location in memory, with some locations next to it that are logically part of the array. But really, an array is just a pointer. | |
Mar 27, 2013 at 13:40 | comment | added | Dave Clarke | @Raphael: The implementation of the cola object model uses the -1 position to store the vtable: piumarta.com/software/cola/objmodel2.pdf. Thus the fields are stored in the positive part of the object and the vtable in the negative. I cannot remember the details, but I think it is to do with consistency. | |
Mar 27, 2013 at 13:36 | comment | added | Mohammed Fawzan | Thanks for the reply, But I didn't get the idea. BTW I'll read it again and again until I understand.. :) | |
Mar 27, 2013 at 13:34 | comment | added | Raphael | In other words, it should probably not be used. Period. What, your name is Donald Knuth and you try to save another 17 instructions? By all means, go ahead. | |
Mar 27, 2013 at 13:27 | history | answered | Dave Clarke | CC BY-SA 3.0 |