This simplified example code raises C6001 under static analysis:
struct st_facade
{
st_facade &operator=(const int new_value) { m_value = new_value; return *this;}
private:
int m_value;
};
struct foo
{
st_facade my_int;
};
void main()
{
foo f;
f.my_int= 0; // C6001: Using uninitialized memory 'f'.
}
My understanding is that this warning is raised because the overloaded assignment operator hypothetically could be reading uninitialized memory. But in this case, we are not doing that -- we are in fact initializing the memory.
How can I tell the compiler that the assignment operator here is not using any uninitialized memory? I've tried to express this using several different SAL annotations, but nothing has worked.
Zero-initializing the memory (i.e. with foo f{};
or st_facade my_int{};
) is not the correct answer in this case. In the actual code, such initialization could have an unacceptable perf impact
[edit] For similar reasons, constructor-based initialization is not the correct answer either. We need this type to be uninitialized on creation; we need to inform the static analyzer that the assignment operator performs the initialization.
[edit 2] More generally, we need a way to tell the static analyzer "this method initializes the object" without implementing a constructor or zero-initializing every instance of the type.