Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Rule 11.6.1 - Variables initialized via output-parameter?
#1
Hi,

Consider the following example:

Code:
// external_c_api.h
enum error_t
{
  // ...
};

enum foo_t
{
  foo_A = 1,
  foo_B = 2
};

error_t externalCApi(foo_t*);

// main.cpp
void f()
{
  foo_t x;
  error_t error = externalCApi(&x);
}

How would one apply 11.6.1 in this case? It's not possible to avoid the two-step initialization process. There may not even be a "sensible default" to give to `x` before giving it the "real" value (and MISRA explicitly discourages this pattern). In particular, doing "foo_t x{};" leads to zero initialization, but there is no enumerator with the value 0.

Thanks!
Reply
#2
11.6.1 is only advisory for precisely this reason, there are cases when it its not possible to initialise to a 'sensible' state.

Note that this rule only cares about variables being initialised, it doesn't care if the value is overwritten before being read, though another rule may fire on that.
Posted by and on behalf of
the MISRA C++ Working Group
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)