3 ms·
I'm try to not sound rude, but I don't think I can. Every C++ developer with even some significant amount of experience will identify this as bad code with one
by dxuh 6y ago
I'm try to not sound rude, but I don't think I can. Every C++ developer with even some significant amount of experience will identify this as bad code with one eye closed. Of course this looks wrong to someone that doesn't know the language. People freak out because it crashes your program, but in the end it's just a bug and producing bugs that are not obvious to someone that doesn't use the language much is very easy with every language.
I remember people prophesizing the end of C++ because of the introduction of string_views. When writing a text editor over a couple of weeks (a lot of text manipulation) I used string_views extensively and purposely over-used them (returned them a lot or kept them for a long time), because I wanted to see how dangerous they could be and I never got a bug/crash that was related to them. Of course you have to think about what you are doing, but you should be doing that anyway.
What I am trying to say is that while it's never "free" to not write bad code, if you get used to it, you don't have to think as hard about it, as you might think.
There are way more common examples of easy ways to break your code, like storing references/pointers to elements in vectors, which then resize or capturing objects in lambdas that get destroyed before the lambda is executed and stuff like that. Even out-of-bounds access to arrays/vectors is a lot more common than that, I think personally.
- mlindner 6y ago> People freak out because it crashes your program People freak out because it _doesn't_ crash your program (until it's running in production of course). On my system without optimizations it prints "xyz" just fine, with optimizations it prints nothing, even though I'm accessing to the middle of memory. I think on most systems this will not actually crash unless you have explicit out of bounds memory tracking inserted into code by the compiler every access slowing down your code. The point is in Rust this wouldn't even compile. I'd be curious if you ran your text editor through a memory sanitizer like valgrind and how many issues it points out.
- pjmlp 6y agoIn VC++ also not, as I do static analysis as part of the build.