This blog post feels weirdly superficial (haven't looked at the video/presentation).
> Array new and delete
> When you write new in your applications, you are creating unmanaged objects, and you are then required to call delete later on if you don’t want to risk leaks. So don’t use new and delete at all, as this is considered a C++ bad practice. Better yet, working in modern C++ allows you to use smart pointers and Standard library container classes that make it easier to match every new with exactly one delete.
The text is reasonable, but why is the heading "array new and delete"? There is a difference between new/delete and new[]/delete[], and calling delete[] on something created by new or vice versa is indeed going to cause problems. But that problem (and hence the section title) is more or less orthogonal to what the section body talks about.
Edit: The corresponding slide in the presentation does talk specifically about this kind of mismatch. It also comes to the same conclusion as the text ("just use the STL"). The connection between title and body was apparently lost in translation.
Maybe as more general feedback, I'm getting a strange vibe from this blog. Lots of "top 5 <C++ thing>" posts, links to Patreon-gated articles, ads for blog-owner-written books... Which would all be fine, but that plus an inconsistent summary of someone else's talk raises a lot of warning signs for me.
But why though? I mean, if I would like exactly 26 chars, this seems a nice way to get that. Even understanding that in C++ "char" is just a weirdly archaic way to say "byte" I might actually want that.
Obviously it's bogus for the language to let you say you want an array of 26 chars and then treat that as a reasonable place to write an unknown number of characters, but that's a language fault.
Sure, but that's a bug right? The language isn't able to give you those bells and whistles for its actual array type, so as a work around here's something else instead. Naming it "array" won't fool anybody when your language does have arrays already.
I mean, given what is intended in the examples is a string, obviously you should use an actual string. But if you want an array, you should be able to use an array for that, and it's silly that instead you're asked to use a substitute.
This may be splitting hairs, but `std::array` is how the language provides bells and whistles (via the STL). You can see here (1) it's a transparent wrapper around a raw array.
On the one hand, thanks for the terrifying code. An earlier thread on HN left me wading through Rust internals which are very ugly, but this is reassuringly worse.
But on the other hand, I already am splitting hairs. C++ comes with actual arrays, which lack bells and whistles, adding the bells and whistles to a class named "array" in the STL instead does not add them to actual arrays. If it did that would be a fine fix, but it doesn't.
Rust users who've told the compiler that their function wants, for example, an array of 26 chars can cheerfully assume in the function that the array has 26 chars in it. The compiler won't let anybody call that function with anything else so it'll be fine. It can make sense to do that, although perhaps not with exactly 26 chars.
C++ users can write a pretty similar looking function signature, including that size constraint - but, a C++ compiler just ignores the integer 26 and silently gives them a pointer to what may or may not be some number of chars, even though that's clearly not what the author intended. They need to write something quite different if they want the compiler to look after this constraint, and if they knew to do that they wouldn't be in this mess in the first place.
There's nothing obviously terrifying about this code to me. That's just what the STL internals look like. It has a few conventions that differ from other C++ code, and most of the verbosity of that code is to fulfill requirements of standard collections (implementing your own STL compatible container is not usually quick)
As to your point... C++ doesn't really work that way. The STL can't add methods to "actual arrays" as you call them, and the compiler can't really do it either. You could extend a compiler to do it but then it wouldn't be C++, strictly speaking.
Some of the other issues like type coercion between arrays and pointers is both not that big of a deal, has well known solutions, and changing would break so much could it's not feasible to do.
While there's some verbosity to the STL (the error messages are worse than anything else) that's just what programming in C++ is like. It is not terse.
> You could extend a compiler to do it but then it wouldn't be C++, strictly speaking.
This is tautological. C++ has subsequently added all sorts of features, including new operators and keywords. It chose not to actually fix arrays, either when instituting std:array or since. "You can't do that in C++" would have been an equally reasonable thing to say about the spaceship operator (and then C++20 added that), or a for loop inside a constant function (and then C++14 added that) and it remains today just as reasonable a thing to say about various features we can expect to see in C++23 or beyond.
It's not really tautological - C++ is defined by the specification, adding things outside the specification make it not C++. What you're talking about here is not a trivial or safe alteration to the language, both examples are merely allowing previously disallowed syntax.
What you are suggesting is altering either the semantics of the language (no C++, including the STL, may define methods for foreign types, which include primitive/built-in types like arrays), or altering the contract between the compiler and the language (the implementation does not define any methods for primitive types, with the exception of a handful of things that must be implemented by the compiler, like sizeof).
Now I'm not saying its impossible for raw arrays to have nice features added in the future. Just that it's an uphill battle that has subtleties you're ignoring. It's not as simple as allowing some extra syntax that previously was disallowed.
Other stuff like forbidding coercion between arrays of the same type will never be added to the specification because it does more than allow new programs to be written, it results in old programs being forbidden. The last change I can recall doing this was the removal of auto_ptr and deprecation of some niche features dating to the 80s that no one actually used.
> Now I'm not saying its impossible for raw arrays to have nice features added in the future.
If you agree it's possible to fix this "in the future" then either you imagine some as yet non-existent technology is needed, or you have to agree it was already possible to fix this in the past and they chose not to. As I said.
> Just that it's an uphill battle that has subtleties you're ignoring.
Everything in C++ has subtleties that I'm ignoring. Trivial fixes to the STL result in multi-hour wrangling, the C++ back compatibility story is a black hole nightmare, and so any new feature work comes down to who is willing to wrestle with the bear. But remember, I am not the one who came here to tell people about how "No even vaguely competent C++ programmer" would use a core language primitive.
A powerful desire to ignore these subtleties is why I don't write C++. I was actually starting to worry that I might have to learn (modern) C++ in the last few years, but Rust averts that.
if you didn't want to be using a jury rigged together language with 5 versions of each way of doing something, of which at least 4 are wrong, you wouldn't be writing C++
I've seen that in professional decently built code. In the code that I am thinking of though, instead of 26, it is MAX_PATH, which is 260 and is used to specify the maximum length of a filepath. It is used to interface with specific Microsoft functions that handle file paths by taking in an array of chars and immediately after is converted to a std::string. Sure, it could be dynamically allocated, but if it gets a filepath longer than the maximum length of a filepath, then it is already dealing with some sort of undefined behavior.
It is clearly meant as a bad example, followed by suggestions on how to fix it. Replace “26“ by some #define or const variable and then tell me again you haven't seen code like this. Not to mention that this article is clearly aimed at beginners.
As I understand it this is one purpose of Rust's "Editions" behaviour. They can deprecate something, suggesting what you ought to do instead, and then in a subsequent edition they can actually outlaw it. Your old code still compiles, since that was for the earlier edition, and editions compose in both directions perfectly well so that the ecosystem isn't fragmented, but new projects will be written for a newer edition where the deprecated code now does not compile, and so the deprecated practice actually dies out in the minds of programmers, rather than limping on forever because of doubts about backward compatibility.
Rust is a young language, perhaps in twenty years we'll know that editions weren't powerful enough, but it seems like a good start. This year will see a new edition (Rust 2021) with a handful of such changes, such as requiring you always write 1..=9 not the historical alternative 1...9 when you mean that the range should be exactly 1,2,3,4,5,6,7,8,9.
Various smart pointer types solve the uninitialized pointer problem. You can even have variants that are guaranteed to be non-null (unlike, say, java, where null references can escape during initialization, especially in multithreaded code)
weirdly, this article advises to use unsigneds for array offsets, which is a fine way to overwrite the heap - when you accidentally decrement an array index past 0.
> Array new and delete
> When you write new in your applications, you are creating unmanaged objects, and you are then required to call delete later on if you don’t want to risk leaks. So don’t use new and delete at all, as this is considered a C++ bad practice. Better yet, working in modern C++ allows you to use smart pointers and Standard library container classes that make it easier to match every new with exactly one delete.
The text is reasonable, but why is the heading "array new and delete"? There is a difference between new/delete and new[]/delete[], and calling delete[] on something created by new or vice versa is indeed going to cause problems. But that problem (and hence the section title) is more or less orthogonal to what the section body talks about.
Edit: The corresponding slide in the presentation does talk specifically about this kind of mismatch. It also comes to the same conclusion as the text ("just use the STL"). The connection between title and body was apparently lost in translation.
Maybe as more general feedback, I'm getting a strange vibe from this blog. Lots of "top 5 <C++ thing>" posts, links to Patreon-gated articles, ads for blog-owner-written books... Which would all be fine, but that plus an inconsistent summary of someone else's talk raises a lot of warning signs for me.