LabWindows/CVI

cancel
Showing results for 
Search instead for 
Did you mean: 

"variable is uninitialized" and unions

Solved!
Go to solution

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?

Vix
-------------------------------------------
In claris non fit interpretatio

-------------------------------------------
Using LV from 7
Using LW/CVI from 6.0
0 Kudos
Message 1 of 3
(4,069 Views)

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

Message 2 of 3
(3,921 Views)
Solution
Accepted by topic author vix

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:

  • numeric                    4 bytes
  • string                        8 bytes
  • guid                          16 bytes
  • byteString                 8 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.

Vix
-------------------------------------------
In claris non fit interpretatio

-------------------------------------------
Using LV from 7
Using LW/CVI from 6.0
0 Kudos
Message 3 of 3
(3,905 Views)