Earlier quoted context omitted.
That's exactly the problem. C/C++ has undefined behavior as part of the language spec, so it can never be safe unless you use a compiler that promises to reject programs that invoke undefined behavior.
int get_value(int* pointer) { return *pointer; } How is a compiler supposed to reject this for undefined behavior? It'd be absurd to demand the compiler someone knows all possible usages for get_value to find if any of them pass a nullptr, so what is it supposed to do? And this is why the spec says things like that deref'ing a null pointer is undefined behavior, so that the compiler can take that function and do the…
A Usable C++ Dialect That Is Safe Against Memory Corruption
51–60 of 103 posts
Re: A Usable C++ Dialect That Is Safe Against Memory Corruption
#52Re: A Usable C++ Dialect That Is Safe Against Memory Corruption
#53Earlier quoted context omitted.
It's more complicated than this. C++ compilers can't just "reject" UB code - they don't have enough information at all times to prove that code invokes UB, the languages is not powerful enough to represent this in all cases. When people say "just fix this in compilers, don't compile when they run into UB" it's a fundamental misunderstanding of the problem. Yes, in some cases the compiler can prove UB, and uses it to…
> a fundamental misunderstanding of the problem No kidding! There is one right here: > Yes, in some cases the compiler can prove UB, and uses it to write optimiation Compilers do not prove UB , on the contrary they postulate no-UB and then use this axiom to prove other properties of the code.
Re: A Usable C++ Dialect That Is Safe Against Memory Corruption
#54They have even been used to ship some important code in big project made of tons of legacy code -- so I'm not even sure an interop argument could be made.
Re: A Usable C++ Dialect That Is Safe Against Memory Corruption
#55Earlier quoted context omitted.
Wouldn't you have a problem with reference cycles in C++ smart pointers? Not sure if they do anything special to prevent this.
std::weak_ptr is there for resolving reference cycles.
Re: A Usable C++ Dialect That Is Safe Against Memory Corruption
#56Earlier quoted context omitted.
Then you have a check on every deref, which means you incur a relatively large performance penalty for using a smart pointer. If you want stuff like this there are other languages out there.
Would it be possible to write the check so that the null test overlaps with the rest of the instructions? If the test is anyways assumed to pass, you should only get a 1-instruction overhead, right? (That'll only work for null.)
Re: A Usable C++ Dialect That Is Safe Against Memory Corruption
#57On the other hand, the tagged pointer implementation strategy for "soft pointers" isn't really garbage collection, but it does have much of the same overhead. Pointer reads must check the tag ID and throw, which is like a read barrier [2]. Writes through a pointer must do the same, similar to a write barrier [3]. And that's not getting into the overhead of multithreading; I see no reasonable way to implement this scheme in a multithreaded world. I expect that a fast GC without read barriers will significantly outperform this scheme. As much as everyone complains about the speed of GC, garbage collection is hard to beat!
[1]: http://www.memorymanagement.org/glossary/d.html#term-deferre...
[2]: http://www.memorymanagement.org/glossary/r.html#term-read-ba...
[3]: http://www.memorymanagement.org/glossary/w.html#term-write-b...
Re: A Usable C++ Dialect That Is Safe Against Memory Corruption
#58I have a question about this. Articles like http://blog.llvm.org/2011/05/what-every-c-programmer-should-... have convinced me that even if C or C++ reads logically like it is safe, there is a possibility that the compiler can rewrite your code in an acceptable way according to the standards such that the checks that are clearly visible in your code disappear, opening up the very problems that you thought you were pro…
The article you linked discusses what happens when you do things you should not, like fail to initialize a variable before using it. Modern compilers, when used correctly will let you know when you do this. With the right compiler options, it won't allow you to make such mistakes.
Re: A Usable C++ Dialect That Is Safe Against Memory Corruption
#59Re: A Usable C++ Dialect That Is Safe Against Memory Corruption
#60Earlier quoted context omitted.
Mostly agree, but: > Don't use C API. The STL should provide you with enough API to use the proper C++ types, either std::string or std::string_view in C++17 if possible. Sometimes you're working with C API that gives you back a char * that they've already allocated. AFAIK there isn't a way to create an std::string out of that without a copy. > you can't static_cast a pointer You can static_cast a void * into other k…
> Do you mean std::vector and at()? No, std::array was added in C++11: http://en.cppreference.com/w/cpp/container/array