Compared to the cost of lost productivity and potential security defects it seems like a fair trade-off, but...
...the cost trade-off is not entirely true. In a large number of cases, especially for local variables and field initialization, the optimizer can determine whether the initial value is used or not. A lot of the actual zero initialization will not be done in the final machine code.
In the rare case where such a cost did matter there really isn't much of a problem to provide a keyword that says it shouldn't be initialized. Like other unsafe keywords it should be an opt-in though, as it isn't safe.
In that case, there is no way for the compiler/optimizer to know if a is really initialized by sys_cfn. Only whole-program optimizers will know but few toolchains provide them.
Even with a new keyword, you still have to think about variable initialization. And if you have to think about it, you can as well remember that primitives are not initialized and not need a keyword at all. ;-)
C and C++ are designed to with higher regard to efficiency than fool-proofness, much as your sharp kitchen knife. If you do not like that design approach, why not use another language like Java, i.e. your butter knife? ;-)
Yes, it's easy to find situations where the optimizer cannot optimize code. This doesn't discount the fact that in many cases it can.
I can't imagine a situation where the initialization cost in this type of code would be significant though. The overhead of calling the function, and the sub-function are probably more. And if there's any actual memory access involved, the pipelining of the CPU may render the init negligable.
That code also has the problem of a person being unable to determine whether it is correct. Without looking at the documentation for sys_fcn, you cannot tell if you should have initalized that variable or not.
As I said, in the cases where this is truly a cost problem (and they do exist), you could annotate it:
int a = undefined;
Or something like that.
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
Force-Initializing a variable would increase the cost of a variable definition from 0 instructions to 1 instruction.
Compared to the cost of lost productivity and potential security defects it seems like a fair trade-off, but...
...the cost trade-off is not entirely true. In a large number of cases, especially for local variables and field initialization, the optimizer can determine whether the initial value is used or not. A lot of the actual zero initialization will not be done in the final machine code.
In the rare case where such a cost did matter there really isn't much of a problem to provide a keyword that says it shouldn't be initialized. Like other
unsafe
keywords it should be an opt-in though, as it isn't safe.In that case, there is no way for the compiler/optimizer to know if a is really initialized by sys_cfn. Only whole-program optimizers will know but few toolchains provide them.
Even with a new keyword, you still have to think about variable initialization. And if you have to think about it, you can as well remember that primitives are not initialized and not need a keyword at all. ;-)
C and C++ are designed to with higher regard to efficiency than fool-proofness, much as your sharp kitchen knife. If you do not like that design approach, why not use another language like Java, i.e. your butter knife? ;-)
Yes, it's easy to find situations where the optimizer cannot optimize code. This doesn't discount the fact that in many cases it can.
I can't imagine a situation where the initialization cost in this type of code would be significant though. The overhead of calling the function, and the sub-function are probably more. And if there's any actual memory access involved, the pipelining of the CPU may render the init negligable.
That code also has the problem of a person being unable to determine whether it is correct. Without looking at the documentation for
sys_fcn
, you cannot tell if you should have initalized that variable or not.As I said, in the cases where this is truly a cost problem (and they do exist), you could annotate it:
Or something like that.