Remix.run Logo
▲ William_BB 6 hours ago

If my understanding is correct, C++ built-in types can be declared without initializing them:

  int x;   // Unitialized, garbage value
  int x{}; // Value-initialized (falls back to zero-initialization)
We want classes to have the same behaviour:

  Struct s;   // Default-initializes (presumably we want members uninitialized)
  Struct s{}; // Value-initializes (presumably we want members initialized)
▲ninkendo 6 hours ago | parent [-]

As I replied in a sibling comment to yours, at least one language without constructors (Rust) has the exact same semantics for built-in types vs library types here. You must initialize in typical code, and the escape hatch is std::mem::uninitialized() which lets you work with uninitialized data, and this is true for any type, built-in or otherwise.

▲tialaramex 4 hours ago | parent [-]

For many years now, This function is deprecated. Use MaybeUninit<T> instead.

core::mem::uninitialized is of course unsafe, but what people who designed it hadn't really appreciated was that in most cases it was impossible to use it correctly, it almost might as well have been a call to core::hint::unreachable_unchecked

If your type is a ZST then core::mem::uninitialized was safe to use, because not initializing zero bytes of RAM is the same as initializing all zero of them, this can't possibly matter so we're fine. Otherwise though, we create values which the LLVM backend is sure we cannot know and so it feels entitled to optimise as if any assumptions we make about those values are false, even if the results are contradictory. This causes immediate Undefined Behaviour all over the place.

The MaybeUninit<T> wrapper type is pretty easy to use and won't invoke Undefined Behaviour so long as you do everything in the instructions.

▲ninkendo 4 hours ago | parent [-]

I literally had `MaybeUninit::uninit().assume_init()` first in the comment but rewrote it with `std::mem::uninitialized()` because I thought it would be more readable to a C++ crowd, but perhaps your right, I should have left it as-is.

▲tialaramex 3 hours ago | parent [-]

MaybeUninit::uninit().assume_init() is, exactly like the deprecated† core::mem::uninitialized() immediately UB unless your type is zero size and thus there's no difference between initialized and uninitialized.

You can unsafely ask for an uninitialized value of any type, but you should not ever assert that you've initialized it unless you actually have. Essentially, you can write this in Rust, but you need to be specific about what you're doing and when you are or are not initializing the variable or else things will go badly for you.

It wouldn't be worse in C++, historically, but C++ people are more used to their world being set on fire by an angry compiler because they did something wrong. In Rust you'd be quite surprised by the compiler's wrath.

† Edited to add: Technically today you do not get this if you call core::mem::uninitialized, because it was so dangerous, instead if you call the deprecated function on a non-ZST - ironically you get a a value scribbled with 0x01 bytes. Doing this ensures in most cases LLVM won't set fire to your program immediately but at a price of doing all that extra work of writing 0x01 bytes. But we did tell you (for several years now) how to do better.

▲ninkendo 3 hours ago | parent [-]

I completely agree, but the point was to show Rust code that does the same thing as the (equally unsafe) C++ code, and particularly that it doesn't treat built-ins any different than other types. But nobody should be writing code like this in either language.

To me the whole point of an "uninitialized variable" is to declare it in one place and then assign it in exactly one code path, which is exactly what rust supports without any unsafe code:

  let i: i32;
  if is_tuesday {
      i = 0;
  else {
      i = 1;
  }
  foo(i);
is already perfectly valid, safe rust code, because the compiler knows that all code paths result in assignment before use. And the answer to "what happens on that first line?" is "nothing", because it's just a declaration. In the equivalent C++ you'd be running an arbitrary constructor on the first line, and hoping the compiler optimizes it away. Or if the default constructor doesn't exist or is deleted, you simply can't code that way.