6 ms·
C has many, many problems. Curly braces are really not one of them. If I could wave a magic wand and fix C I'd make arrays and strings know their size. Edit fo
by adrianN 11y ago
C has many, many problems. Curly braces are really not one of them. If I could wave a magic wand and fix C I'd make arrays and strings know their size.
Edit for the nitpickers: Yes, arrays decay to pointers and that's the real problem. Sorry.
- okasaki 11y agoGood news. Arrays have sizeof(arr)/sizeof(arr[0]) elements and strings have strlen(str) letters.
- deleted 11y ago[deleted]
- pjc50 11y ago.. until you pass them as a function argument. And strlen() only works if your null terminator is valid, and doesn't tell you anything about available space in the string buffer for adding to the string. This kind of thing keeps coming up in vulnerability reports.
- ArkyBeagle 11y agoThis happens because people don't use the tools properly.
- adrianN 11y agoThey should be using Ada instead of C, amiright /s
- ArkyBeagle 11y agoI have no way of knowing what they should use.
- noir_lord 11y agoIf different people keep chopping into themselves with a power tool it's probably wise to consider a safety guard.
- ArkyBeagle 11y agoWrong is still wrong, and if someone did it, they did it wrong. This really isn't complicated. It's hardly obscure. There's no excuse.
- exprA 11y agoC arrays “know” their size. (String literals do too.)
- veli_joza 11y agoYou can sizeof them if the declaration is in scope. If you pass them as function argument, this information is not obtainable inside function. You have to pass length as well. This is not very elegant and sometimes you elect to process them without using functions just because you can iterate them easier.
- mfukar 11y agoNow, at one point, you stopped talking about arrays and started talking about pointers.
- veli_joza 11y agoExactly. When array is passed into function, you lose length information and array construct is reduced to pointer. Array syntax is just synthetic sugar for dereferencing pointer with offset.
- mfukar 11y agoThis discussion won't get very far if all you think arrays are is syntactic sugar. The array type is useful - think in terms of a static analyser if it helps - in different ways than pointers.
- cyphar 11y ago"array syntax" != "arrays". He means things like a[i]. Arrays are only really arrays in their local scope, if you pass them to functions they suddenly are pointers (but for some insane reason, C compilers let you specify "the size of the array in a function argument" which doesn't actually compile to any code).
- 11y ago
- asddubs 11y agoI would make it impossible for a simple addition of two integers to result in undefined behaviour