LabWindows/CVI

cancel
Showing results for 
Search instead for 
Did you mean: 

Global initialized variable is seen as uninitialized local

Hello to all,
here is my problem:

when I launch my executable in Debbug mode, it happens that CVI stops on some global variables and display the following message: "NON FATAL RUNTIME ERROR: Local "var" was referenced before being initialized.

For example, the code is as follow:

DisableBreakOnLibraryErrors();

for(i=0; i<9; i++)
{
if(i==4)
{
DebugPrintf("switching(%d): %f", i, switching_spectrum[i]);
}
else
{
results[i] = (switching_spectrum[i] > StdSwSpecLim[band_index][power_level][i])?1:0;
}
}

EnableBreakOnLibraryErrors();

The problem is that the Disable or Enable BreakOnLibraryErrors() as no effects

CVI always stops on "StdSwSpecLim" which is global and
initialized. Its declaration is made in another .C, this part of code only see the extern declaration throught another .h.

Thanks to all.
0 Kudos
Message 1 of 4
(3,666 Views)
Hello Florent!

"florent.d" writes:
> when I launch my executable in Debbug mode, it happens that CVI stops
> on some global variables and display the following message: "NON FATAL
> RUNTIME ERROR: Local "var" was referenced before being initialized.
> [...]
> CVI always stops on "StdSwSpecLim" which is global and initialized.
> Its declaration is made in another .C, this part of code only see the
> extern declaration throught another .h.

CVI shouldn't look for uninitialized global variables.
Global variables are always initialized to zero if you
don't initialize them yourself.

So I'm wondering whether the error message gives you the
wrong variable name. Are any of the other variables
(results, switching_spectrum, band_index, power_lev
el) in
the offending statement local variables that might not have
been initialized?

If all of these variables are globals or initialized locals,
or if the variable name in the error message turns out to be
wrong, could you send me your project so that I can look at
the problem here? I'm unable to reproduce the problem with
my own projects.

> The problem is that the Disable or Enable BreakOnLibraryErrors() as no
> effects

Disable and EnableBreakOnLibraryErrors only work on
return values of library functions. You can use
SetBreakOnProtectionErrors (utility.h) instead:

int oldValue = SetBreakOnProtectionErrors (0);

/* your code here */

SetBreakOnProtectionErrors (oldValue);

Hope this helps,

Peter

-- Peter Ilberg
Message 2 of 4
(3,666 Views)
"Hi Peter and thank you.
I found "a" solution (1) to my problem, and later (with your advices) "the" solution (2); here are the results of my tests, I really do believe they will interest you:

1 - I put a "memcpy" before the "for " statement, as follow:


memcpy (dTrunkTab, StdSwSpecLim, 2*10*9*sizeof(double));


matching = 0;


for(i=0; i<9; i++)
{
if(i==4)
{
DebugPrintf("switching(%d): %f", i, switching_spectrum[i]);
}else
{
results[i] = (switching_spectrum[i] > StdSwSpecLim[band_index][power_level][i])?1:0;
if(results[i])
matching = 1;
}
}


and it worked just fine, the debugger does not stop on
"StdSwSpecLim" anymore. (?? Don't ask me!)


2 - Now "the" solution (the one you're not going to like).
Here is the whole function:
(parameters given to the function)
switching_spectrum = -38.88
type_bande = 880 (EGSM)
power_level = 5


(function)
int RF_Switching_Matching( double *switching_spectrum, int type_bande, int power_level, int *results )
{
int i, band_index, matching=0;
double dTrunkTab[2][10][9];


switch(type_bande)
{
case 900:
case EGSM:
band_index = 0;
power_level -= 2;
if(power_level<0) power_level=0;
if(power_level>9) power_level=9;
break;


case 1800:
case 1900:
band_index = 1;
power_level += 3;
if(power_level<0) power_level=0;
if(power_level>8) power_level=8;
break;


default:
Assert(0);
}


// memcpy (dTrunkTab, StdSwSpecLim, 2*10*9*sizeof(double));


matching = 0;


for(i=0; i<9; i++)
{
if(i==4)
{
DebugPrintf("switching(%d): %f", i, switching_spectrum[i]);
}else
{
results[i] = (switching_spectrum[i] > StdSwSpecLim[band_index][power_level][i])?1:0;
if(results[i])
matching = 1;
}
}


return matching;
}


you will notice the "band_index" declaration and utilisation.


With:


int i, band_index, matching;


the debugger stops on:
results[i] = (switching_spectrum[i] > StdSwSpecLim[band_index][power_level][i])?1:0;
with the following message:
"NON FATAL RUNTIME ERROR: Local "StdSwSpecLim" was referenced before being initialized.
(With band_index = 0, according to the type_bande value).




With:


int i, band_index=0, matching;


it works perfectly !!???????


So as you suspected it seems to turns out that the variable name in the error message is wrong.


I Thank you again for your time and if you need more informations about my project feel free to ask, I'll be glad to oblige.
Bye, Florent


PS: One more thing if it can help, I'm using CVI 6.0."
0 Kudos
Message 3 of 4
(3,666 Views)
Hello Florent!

I've finally managed to look into the problem and it has
turned out to be a bug in the CVI compiler. I've reported
and fixed it for the next version of CVI. Thanks for finding
the problem!

The compiler incorrectly propagated information about
uninitialized local variables from band_index to
StdSwSpecLim. If StdSwSpecLim was a local variable this
would be correct, because it allows CVI to detect when
you're accessing an uninitialized array element. But it's
incorrect to do this for global variables, and
misinterpreting the information later on causes the
incorrect runtime error.

I don't want to go into too much detail here, but this bug
also explains the odd behavior of CVI when calling memcpy().
As you've already noted,
the best way to work around the
problem is to initialize band_index at its declaration.

Thanks again for finding the problem!

Peter

-- Peter Ilberg
0 Kudos
Message 4 of 4
(3,666 Views)