loading...

re: The uninitialized variable anathema: non-deterministic C++ VIEW POST

TOP OF THREAD FULL DISCUSSION
re: 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 entir...
 

extern void sys_fcn(int* handle);

void fcn()
{
  int a;

  sys_fcn(&a)
}

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.

Code of Conduct Report abuse