LabWindows/CVI

cancel
Showing results for 
Search instead for 
Did you mean: 

local var referenced before initialized

Hello all,

in my present CVI 7.1 application I get

     NON-FATAL RUN-TIME ERROR:   "storm_encoder.c", line 862, col 40, thread id 0x00000A80:   Local 'sCmd' was referenced before being initialized.

The function wherein this event happens is

WORD_16 enc_fResetEventLog( enc_sEncoderType *psEncoder, enc_pvfCmdConfirmType  pvfNotifyFunc, void * pvApplicationData )
{
   WORD_16            wRetVal = TRUE ;
   storm_sCommandType sCmd ;

   sCmd.ubyHeader   = 0x10 ;
   sCmd.ubyLB       = 0x01 ;
   sCmd.ubyCmd      = 0x93 ;

    wRetVal = fSendCommand( psEncoder, sCmd, 3, pvfNotifyFunc, pvApplicationData );
    return wRetVal ;
}

So sCmd is a struct declared in the function. Some but not all menbers of the struct are initialized.

Has anybody an idear, why CVI assumes that sCmd is not initialized?

(The "error" does not appear if I add a memcpy after the declaration.)

Kind regards,
ah
0 Kudos
Message 1 of 3
(7,190 Views)

You have answered the question yourself. All the members of the storm_sCommandType  has not been intitialized. There is more members to this structure than the three members  shown in your code.

 

Jan

 

0 Kudos
Message 2 of 3
(7,183 Views)
Hello ah,

If you are intentionally not initializing all the fields in the struct, because you don't need to use all the fields inside your fSendCommand function, and you would like to not have this error, you have 3 choices:

1. You can disable run-time uninitialized error checking (Options>>Build Options>>Detect uninitialized local variables at runtime), although this is really not recommended, since uninitialized variable error checking is a pretty useful feature.

2. You can initialize all the fields anyway, for example by using memset (&sCmd, 0, sizeof (storm_sCommandType)); before you start initializing the fields that you want.

3. Your best choice is to pass the structure to the function by reference, instead of by value. If you only pass a pointer to the function, the CVI debugger doesn't assume that you are using all the values in the structure, and does not report an error. It is also a good idea to pass structures by reference, generally speaking, since it reduces stack overhead, is faster, etc.

Luis
0 Kudos
Message 3 of 3
(7,161 Views)