Questions that make me cringe
I spend a fair amount of time reading (and sometimes responding to) questions in the C++ newsgroups. Every once in a while, someone asks a question that makes me cringe.What makes a question cringe-worthy? Usually it is a question that implies that the person asking it is trying to do something inappropriate. For example, I just saw one such question: Are the elements of a std::vector contiguous? Here is why that question made me cringe.
Every C++ container is part of an abstraction that includes several companion iterators. The normal way of accessing a container's elements is through such an iterator.
I can think of only one reason why one should care whether the elements of a vector are in continuous memory, and that is if you intend to use pointers, rather than iterators, to access those elements. Doing so, of course, violates the abstraction.
There is nothing wrong per se with violating abstractions: As Robert Dewar told me more years ago than I care to remember, some programs are poorly designed on purpose. However, there is something wrong with violating abstractions when you know so little of the data structures used to implement those abstractions that you have to ask strangers on Usenet about your proposed violation. To put it more bluntly: If you have to ask whether vector elements are contiguous, you probably should not be trying to make use of that knowledge.
An analogy comes to mind that may be interesting. Engines for light airplanes are designed with limited lifetimes; after an engine has been used for a stipulated number of hours, it must be overhauled or replaced. Such replacements are expensive, so aviation magazines talk about ways of reducing that cost.
I once read an article that talked about a novel way of reducing the cost of a new engine: Buy a complete kit of engine parts and hire an appropriately certified mechanic to put them together. It turns out that one person can assemble an engine in a small shop for less money than the factory charges to do so--at least as of when this article was published.
Here's the interesting bit. The article said that the factory did not publish an assembly manual for the engine. The rationale is that part of becoming certified as an aircraft mechanic is that you should know enough about how such engines are put together that you can do it correctly without the aid of a manual. In other words, if you have to look up how to assemble an airplane engine, you have no business trying to assemble it in the first place.
Asking how to violate programming-language abstractions is similar: If you have to ask, you probably shouldn't be doing it.