4

So there's a proposal for C++ to zero initialize pretty much everything that lands on the stack.

I think this is a good thing, but I also think malloc and the likes should zero out the memory they give you so I'm quite biased.

What's devrants opinion on this?

https://isocpp.org/files/papers/...

Comments
  • 7
    waste of cycles to zero initialize something I know I will initialize properly myself soon.

    If it's optional using a compile flag, that might be fine though
  • 6
    @Hazarth there's a different proposal referenced in that one to add a [[uninitialized]] attribute

    that would also stop the compiler from warning about it, since you told it "I know what I'm doing"

    I guess that would be a fine optimization when you work with output arguments

    For comparison, Rust has std::mem::MaybeUninit that allows you to avoid initialization on a variable, though I've only seen this used when interacting with C libraries that use the output argument thingy.
  • 4
    @Hazarth I'd rather have a flag that disables zero init and have zero init by default
  • 0
    @LotsOfCaffeine attribute is good too.
  • 1
    @iiii The attribute is too much work because you'd need to annotate every variable. A compiler flag doesn't have that problem and also allows much easier benchmarking to see what the performance impact is (if any).
  • 2
    @Fast-Nop I'd say, both should be present: a flag if you need everything to not be initialized, and an attribute for special cases
  • 0
    @Fast-Nop every other variable? I guess you work with a lot of C-style libraries

    With C++ I would barely use this
  • 1
    @LotsOfCaffeine You don't use local variables? Putting everything in the heap looks even slower. More overhead and less cache friendly.
  • 2
    I don't see any reason to do that. Compiler should warn you when something is not set.
  • 1
    In keeping with C++ motto of don't pay for what you don't use, I'm fine with it if it's strictly opt-in.
  • 0
    @Fast-Nop of course I use variables but when you initialize them right way this proposal wouldnt change anything.

    And no, I do not declare all variables I need at the top of the function, we're not in the stone ages after all
  • 0
    @happygimp0 imo it should be an error and it should be everywhere, so like fields in structs/classes, not just with local variables in functions
  • 2
    Having malloc zero initialise stuff wouldn't add anything, there is already calloc which _does_ zero initialise heap allocations
  • 0
    @ess3sq Yes. And when you use C you should use calloc() per default and only use malloc() where you need the performance and made sure it is save.

    The heartbeat disaster wouldn't exist when the programmers had used calloc().
  • 1
    @happygimp0 Are you sure with Heartbleed? IIRC, the issue was an out of bounds copy from the source, not sending out old malloc'ed data from the target buffer:

    https://theregister.com/2014/04/...
  • 0
    @ess3sq malloc is C stuff. Not C++.
  • 0
  • 0
    @ess3sq the original post was about C++ and stack allocation. Malloc is neither of those 😉
  • 0
    @iiii malloc and friends and the broader libc are available to C++ code.
  • 0
    @ess3sq yes, but those are C legacy and not used in idiomatic C++
Add Comment