11-28-2016 07:31 AM
CVI 2015 and CVI 2015 SP1 always give compilation warning "variable is uninitialized" for structs which contain unions.
As an example, the following struct
typedef struct { UA_UInt16 namespaceIndex; enum UA_NodeIdType identifierType; union { UA_UInt32 numeric; UA_String string; UA_Guid guid; UA_ByteString byteString; } identifier; } UA_NodeId;
has 3 members: an integer, an enum and a union.
The following code
UA_NodeId id; id.namespaceIndex = nsIndex; id.identifierType = UA_NODEIDTYPE_NUMERIC; id.identifier.numeric = identifier; return id;
sets all the 3 members of the struct.
But CVI complains that id is uninitialized when returned, that is not the case.
If the parameter Options >> Build Options >> Debugging Options >> Detect uninitialized local variables at runtime is set, CVI shows an annoying popup every time id is returned.
Could you provide a fix for this wrong warning, please?
Solved! Go to Solution.
01-21-2017 10:20 AM
Hello,
If you use variable watch to examine the structure during execution do you find that identifier.numeric is indeed set?
Could you also provide the definitions for the data types? I suspect that it could be that the data types may be of dynamic size (such as string) which is causing the compiler to get confused as to how much memory to allocate to the union.
Without knowing more I'm struggling to decipher what could be happening within the compiler. What C extension are you using? For instance, C99?
Best regards,
Ed
01-23-2017 01:14 AM
Hello Ed,
I investigated a little bit deeper in this problem and I think I found the reason of this warning (that has a sense).
The 4 members of the union
union { UA_UInt32 numeric; UA_String string; UA_Guid guid; UA_ByteString byteString; } identifier;
have not the same size in bytes:
The member identifierType is used to set how to interpret the union identifier.
The instructions
id.identifierType = UA_NODEIDTYPE_NUMERIC; id.identifier.numeric = identifier;
set the member numeric (and so 4 bytes only), but the union itself is 16 bytes wide.
And so the warning is right.
If I set all the 16 bytes of the union I don't see the warning anymore.