Darren New wrote: > Christopher Smith wrote: >> Darren New wrote: >>> The compiler can look at that and know that i never goes out of bounds >>> and not bother to generate array bounds checks. >> >> Yeah... any good optimizer will do this for you. C++ actually makes its >> job easier. > > Easier than what? I can't imagine how the C++ compiler can tell > statically that I'm not running off a dynamically-allocated array.
What does it matter whether the array was dynamically allocated? > Are you telling me that if I had a "smart array" class, the compiler can > optimize away the checks inside the class when I pass it the right > calls? Even with a dumb array class. Optimizers are cool. :-) > I'd think you would at a minimum have to inline the code for that > to work, and it would seem to make the compiler's job a lot harder, > deducing that what you have is essentially what a different language > would specify from the start. Most optimizers don't work from the syntax of a language. Instead, they look at something like a low level SSA structure. So, there isn't a lot of distinction that comes from having built-in bounds checking. What you can do is provide other context that makes it easier to optimize (Java's memory model allows all kinds of clever optimizations that are essentially impossible for an optimizer to consider in C++), and C++'s rather strict rules guaranteeing nobody else but the current thread is mutating the array unless otherwise specified is very helpful in proving when bounds checking is not necessary. --Chris -- [email protected] http://www.kernel-panic.org/cgi-bin/mailman/listinfo/kplug-lpsg
