4 ms·
Written properly, C++ is not unsafe. Certain language own certain domains, for better or worse, this means that most developers interested in low level program
by d0lph 11y ago
Written properly, C++ is not unsafe.
Certain language own certain domains, for better or worse, this means that most developers interested in low level programming will know C/C++, and that the will be a substantial amount of tooling already in place.
- cmrx64 11y ago> Written properly, C++ is not unsafe. This is abusing what "unsafe" means. As a language, C++ is inherently unsafe. However, some C++ programs ("written properly") are correct. If written properly C++ is not unsafe, then written properly C is not unsafe.
- SCHiM 11y agoNo I think he meant that if you adhere to certain language constructs the language is just as memory safe as Rust/others. Eg. Don't use C++ as C with vectors, use C++ with the more modern language features: It's the difference between: // where f is of type fstream of a file containging "this is a string\r\n"No I think he meant that if you adhere to certain language constructs the language is just as memory safe as Rust/others. Eg. Don't use C++ as C with vectors, use C++ with the more modern language features: It's the difference between: std::string A = "ab" + std::string("c"); // And: char Buffer[512] = {}; strcat(Buffer, "ab"); strcat(Buffer, "c"); Or alternatvily: std::vector<uint8_t> SomeData = { 1,2,3 } std::vector<uint8_t> OtherData(SomeData.begin(), SomeData.end()); // and: std::vector<uint8_t> SomeData = { 1,2,3 } std::vector<uint8_t> OtherData; OtherData.reserve(3); memcpy(...) In the first one you're 'protected' by the stl code, which is arguably the same level of protection as afforded by any memory safe language’s library code. std::string A = "ab" + std::string("c"); // And: char Buffer[512] = {}; strcat(Buffer, "ab"); strcat(Buffer, "c"); Or alternatvily: std::vector<uint8_t> SomeData = { 1,2,3 } std::vector<uint8_t> OtherData(SomeData.begin(), SomeData.end()); // and: std::vector<uint8_t> SomeData = { 1,2,3 } std::vector<uint8_t> OtherData; OtherData.reserve(3); memcpy(...) In the first one you're 'protected' by the stl code, which is arguably the same level of protection as afforded by any memory safe language’s library code.
- dbaupp 11y ago> No I think he meant that if you adhere to certain language constructs the language is just as memory safe as Rust/others. It's not; as soon as code uses (for instance) references, you're running the memory-unsafety gantlet. E.g. even something super-modern like for (auto& thing: vector) is risky: what if the loop body happens to somehow call `vector.push_back(x)`?
- tomjakubowski 11y ago> is risky: what if the loop body happens to somehow call `vector.push_back(x)`? Some people like to say that these bugs are trivial to avoid ("who would write code like that?!"), but they miss the insidious (and common) case: when code several layers down the call stack (in some arbitrary callback assigned at runtime, say) grows the vector/map from under you.
- kccqzy 11y agoIt's still easy to avoid though. Just take a const reference to the vector and use that in the loop and pass that to the callback. It just requires discipline, and in fact less discipline than writing C.
- gpderetta 11y agoI don't subscribe to the "properly written Modern C++ is safe" meme, but still it must be recognized that there are degrees of unsafety [1] between languages. [1] not talking about memory safety, but as a general "probability of having a defect".
- cmrx64 11y agoI really dislike the "safe"/"unsafe" terminology, there isn't a commonly-agreed upon definition of "safe". But I think it's safe to say that it's a combination of type and memory safety, not "low probability of having a defect". There certainly is a spectrum.