I've done something vaguely like that (only safeish, with placement new) one time when I needed to create a bunch of objects and I needed to allocate everything and then construct it. |
If everyone just wrote code perfectly, we wouldn't need any kind of checking at compile time. |
It doesn't matter. The compiler will put those initializations in the constructor, so if the constructor doesn't get called, they will not run. |
Rust doesn't let you, due to the way the borrow checker works. |
Its off topic but WHY? I have yet to run into this one -- where you can't just allocate a container of the objects and let the ctors run? Its the same number of bytes, the same memory behaviors, so ... it it because the ctors painted you into a corner where you got the data to construct them late but needed it preallocated for performance or something like that? |
I don't see how one can mistakenly fall into the pits of UB when safe code is usually what you'll find learning C++. |
|
|
|
|
Most mainstream languages I don't see race condition protection being enforced. |
he main motivation was because if allocation was going to fail, it was preferable to fail before any of the constructors had run. |
std::vector<foo> a(10);
not meet your requirements? The vector will allocate the memory before calling constructors I believe.I guess go write more C++ until you understand why people are asking for memory safety features |
In real code where you get multiple people sticking their grubby little fingers over multiple months, it's easy for stuff like this to show up |
void foo() |
Would this: std::vector<foo> a(10); not meet your requirements? |
.join() is the preferred (safer) method of handling threads. |
If you write "good" code, you won't use dangerous features. You have to search for the dangerous features to learn them. |
unsafe
and doesn't call out to external functions will never, ever, ever corrupt memory.A dangerous mountain path has guard rails, but no one is stopping you from climbing over them and hopping around. |
They're for different things. You wouldn't use join() where you would've used detach() |
Conversely, a Rust (or C#, or Java, etc.) program that doesn't contain any unsafe and doesn't call out to external functions will never, ever, ever corrupt memory. |
|
|
C++ has guardrails that at many points sink into the ground |
to check system security to such threats/attacks, to teach the inner workings of code, etc. |
Yes you can use "unsafe" code in other languages, but I don't think/know if it's really the same. |
You've not spent enough time debugging broken code. |
Those are not legitimate reasons. |
I've spent plenty of time debugging broken code. |
Rarely has it been the case that the issue was UB. |
?? of course they are. |
If there were no legitimate reasons for "dangerous" code, languages like Rust wouldn't have a need for unsafe blocks. |
|
|
|
|
"Plenty of time" and "enough time" are distinct conditions. |
Unsafe blocks in Rust and casts in C++ are not there so you can write broken code on purpose, they're there because the language designers know the type system prohibits certain correct programs from being written |
There's a thread here somewhere where I "prove" to a guy that a reference is just a pointer by making it point to something else by overflowing a pointer. That was wrong; it doesn't prove anything about C++'s machine model. There's a CPU-compiler combination out there where our nonsense doesn't work. |
I'd still rather drive myself! |
Though I wonder if that means compile times are higher. |
This doesn't mean you're wrong, it just means you're taking advantage of the CPU/compiler to prove your point. Just because it doesn't work on some other compiler doesn't mean the point you made was invalid. |
If you ask me, no one should be writing in C anymore, unless they need extreme portability. |
Like I said, nothing prevents you from wrapping your entire Rust or C# program in an unsafe block |
Since according to C's rules dereferencing a null pointer has undefined behavior, GCC was free to assume that the pointer was non-null, so it was able to optimize the if away. |
Eh. It's a mixed bag from what I've seen. |
|
|
|
|
|
|
That's the point though, right? I don't wanna have to fiddle with the car to pull out the steering wheel and take over. Once you have a safe AI car, taking over control is going to be "wrong". If you're using Rust and you use "unsafe" blocks, that is now the dangerous behavior that is frowned upon. |
That seems ridiculous if true. I never liked GCC/Clang. I knew from the moment I found out they allowed VLA, when C++ standard does not, that these compilers would cause me nothing but trouble. |
I may have to take back what I said about their runtime speed being comparable. |
So writing in C++ is safe, but writing unsafe code in Rust is not |
Any compiler could do something similar. UB is UB. |
Even though you have no idea which sort algorithms are being used? |
|
|
Elapsed time: 7 microseconds |
|
|
Elapsed time: 13 microseconds |
writing it would be considered bad practice |
Rust is based off C++, I assumed they both used quick sort. |
So asking that the language not be made safer because then you'd need to turn those features off is quite irrational. |
I think most C++ library implementations use a hybrid sort that's faster than quicksort when the input is nearly sorted. |
it requires you to be on alert at all times, and when you're not, that's when it bites. |
testing plenty before implementing. This is true whether or not you're using C++. |