Ranter
Join devRant
Do all the things like
++ or -- rants, post your own rants, comment on others' rants and build your customized dev avatar
Sign Up
Pipeless API
From the creators of devRant, Pipeless lets you power real-time personalized recommendations and activity feeds using a simple API
Learn More
Comments
-
Hazarth95212ywaste 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 -
@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. -
@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).
-
@Fast-Nop every other variable? I guess you work with a lot of C-style libraries
With C++ I would barely use this -
@LotsOfCaffeine You don't use local variables? Putting everything in the heap looks even slower. More overhead and less cache friendly.
-
I don't see any reason to do that. Compiler should warn you when something is not set.
-
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.
-
@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 -
@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
-
ess3sq1632yHaving malloc zero initialise stuff wouldn't add anything, there is already calloc which _does_ zero initialise heap allocations
-
@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(). -
@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/...
Related Rants
-
xjose97x19Just saw a variable in C named like this: long time_ago; //in a galaxy far away I laughed no stop.
-
Unskipp24So this happened last night... Gf: my favorite bra is not fitting me anymore Me: get a new one ? Gf: but it ...
-
sam966911Hats off to this lady .... I would have just flipped the machine
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/...
rant
c++
initialization
memory