user3535698
user3535698

Reputation: 21

Why does lint complain "Warning 446: side effect in initilizer", lint e446 for the code below?

I need help understanding why lint complains on the code below due to the struct initialization. The code runs without any issues, or at least any known issues that is

struct MsgKey_t {
    int type;
    int index;
    int signal;
};

typedef std::map< int, std::pair< MsgKey_t*, int* > > MyMap_t;

MyMap_t myMap;

MyMap_t::iterator subKey = myMap.find( 11 );

if ( myMap.end() == subKey )
{
    exit(-1);
}

MsgKey_t key = { subKey->second.first->type, subKey->second.first->index, subKey->second.first->signal };

If I change the initialization of the struct to look like:

MsgKey_t key;
memcpy( &key, subKey->second.first, sizeof( key ) );

or :

MsgKey_t* pKey = subKey->second.first;
MsgKey_t key = { pKey->type, pKey->index, pKey->signal };

lint is happy all day.

Upvotes: 1

Views: 1482

Answers (2)

user2591612
user2591612

Reputation:

According to the C++lint documentation, this is the reason:

Auto aggregates (arrays, structures and union) are normally initialized by a collection of constant-valued expressions without side-effects. A compiler could support side-effects in which case you might want to suppress this message.

It is entirely possible that these are not constant and could be subject to side effects:

subKey->second.first->type, subKey->second.first->index, subKey->second.first->signal

Since your memcpy does not technically follow a typical initialization, just moving bytes around, lint does not warn you. It will let you do it.

Upvotes: 1

user3458
user3458

Reputation:

Could lint be referring to operator -> in the initalization? That could possibly have side effects, depending on the implementation of the iterator.

Upvotes: 1

Related Questions