5 ms·
>This can of course be done without the new syntax, and it seems exceedingly useful. Are such structs commonly used in C projects? There are string libraries t
by coldtea 5y ago
>This can of course be done without the new syntax, and it seems exceedingly useful. Are such structs commonly used in C projects?
There are string libraries that work that way in C.
If you do it manually, without a lib, then you need to check that the length is valid yourself (after every operation), so it's not as useful as a language with first class support for it.
- naasking 5y agoPresumably you can add an inlined array access function or macro in the header to do this for you.
- WalterBright 5y agoIf that worked, people would have done it, and C buffer overflows would be a thing of the past.
- naasking 5y agoThe reason this hasn't been done is mostly because C programmers have an allergy to runtime checks that might slow their programs by even single digit percentages.
- WalterBright 5y agoPeople don't use these other schemes because they are clumsy, inconvenient, look bad, and have never caught on. With it as part of the syntax, it becomes natural to use them. I'm not making this up, it is based on extensive experience. The runtime overflow checks can be turned on and off with a compiler switch, so it becomes trivial to see what performance effect it actually has. Critical loops can be coded with ordinary pointers as necessary. For the rest, the performance effect is not measurable. Again, this is from experience, not supposition.