Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

> C code would also have to do.

Well that's the point: C does not have to do it.

RAII protects you from, e.g., leaking memory. But C can just leak memory, and there isn't really anything substantially wrong with that (you can't get UB from leaking memory).

Checking error values, releasing all resources, doing bound checks, and all the other things that both C++ and Rust do by default are more expensive than doing nothing. Sure, they are zero-cost, in the sense that if you were to write C code that does those, the C code wouldn't be faster. But as mentioned, C code is not required to do these.

With C, you have to manually write the code for doing those things. With C++ and Rust, you have to write the code to opt-out of doing those things (with Rust, removing a bound check is a one liner).

It's a matter of language defaults. I know what the better default for me is.



You can write zero ops destructor as well. So yes, RAII is zero-cost abstraction.


I literally said so.


You wrote that in C++ and Rust it is necessary to "opt out" of properly freeing memory. That is a false statement. It is not just incidentally false, it is fundamentally false.


If your constructor news memory, then if you don't write a destructor, then nothing deletes the memory. You don't need to "opt out". It is hard to guess where you get this idea.

It is bad form to code memory leaks, so normally one doesn't. Instead, one normally allocates in such a way that the automatically-generated destructor frees the memory.

But neither case requires writing more code to "opt in" or to "opt out". It is the same in Rust.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: