2 ms·
They're clearly closely related, but it would technically have been possible to represent strings using a header with a length field followed by the characters
by ScottBurson 13y ago
They're clearly closely related, but it would technically have been possible to represent strings using a header with a length field followed by the characters in the string. That would have required people to pass the location of an arbitrary character as two explicit values, the string itself and an offset, which would certainly not have been in the spirit of the way C takes advantage of pointers, but nonetheless would have been possible.
BTW I must take issue slightly with your characterization of the problem. Arrays decay to pointers immediately when referenced, not just when passed to functions. I suppose a C implementation could do bounds checking on non-pointer-mediated accesses to local arrays, but I've never seen an implementation do that (except those that do bounds checking more generally). Did yours?
In fact, the way I put it once was, "C does not have arrays. It has pointers and notations for allocating and initializing memory; it does not have arrays." (Meaning, of course, "array" in the sense of a firstclass object, with bounds checking.)
- WalterBright 13y agoLength-prefixed strings have another disastrous problem - strings cannot be sliced. A substring would always have to be copied. Arrays decay to pointers when dereferenced, but this wouldn't impede array bounds checking. I didn't bother putting in static array bounds checking in Digital Mars C because pretty much all the interesting cases were buffers passed to a function. I know C doesn't have first class arrays. That's what my enhancement suggestion was for.
- kyllo 13y agoAren't strings immutable in most languages? Like in javascript, String.slice() returns a copy, it does not mutate the original string. I don't disagree with you, but I don't see how it's a disastrous problem, either.
- WalterBright 13y agoIn D, which has ptr/length arrays, strings are immutable and sliceable without copying. This can make them very fast.
- ScottBurson 13y ago> Length-prefixed strings have another disastrous problem - strings cannot be sliced. A substring would always have to be copied. Slicing is one common operation that's O(n) with a simple vector representation. I agree it's nice if it can be made O(1), but concatenation is still O(n) with that representation. This is why I'm a fan of ropes or other more exotic sequence data structures that can do all the common operations in log time or better. > I know C doesn't have first class arrays. That's what my enhancement suggestion was for. Fair enough. I think it's a good suggestion, but I'm skeptical that it would see wide use. Here's why. It has been straightforward for a long time to declare a struct type that holds a base and bounds, pass that struct by value, and then do one's own bounds checking. (The K&R-era compilers didn't have struct passing by value, but it was introduced in Unix v7, as I recall. That was a long time ago.) This isn't quite as convenient as what you're proposing, but it's not hard, and I don't think I've ever seen anyone do it. (Not that I spend my time reading other people's C code.) It certainly hasn't become standard practice. Still and all, if you can persuade the committee to put it in the language, and also define new standard library functions that make use of it, I agree it would be a good thing.