Question

I have an app that is near completion and am getting the following error whe running Analyze:

Assigned value is garbage or undefined

The error is occurring on this line of code:

float flowSourceValue = flowSource[count];

When running debugger, code DOES enter this for loop and floatSourceValue DOES get assigned.

if(gaugeReportsCFS){
    count = 0;
    gaugeCount = [flowKeys count];
    float tally = 0.0;
    float flowSource[gaugeCount];
    for (NSString *key in flowKeys){
        float flowSourceValue = flowSource[count]; //assigned value is garbage or undefined
        if(flowSourceValue < 1){
            gaugeReportsCFS = NO;
            gaugeReportsFeet = YES;
        } else {
            if(!isnan(flowSource[count])){
                flowSource[count] = [[rvrGauge.gaugeFlowList objectForKey:key] integerValue];
                tally += flowSource[count];
            }
        }
        count++;
    }
}

What could be the reason I am getting this warning? Thanks!

Was it helpful?

Solution

The local array

 float flowSource[gaugeCount];

Is not initialized and therefore can contain "garbage". Accessing it is undefined behavior.

OTHER TIPS

The warning is telling you that the analyzer things there there are execution paths where your line

float flowSourceValue = flowSource[count];

will be executed, but the expression on the right side of the equals is uninitialized.

Looking at your code, that is totally true. You declare an array flowSource on the stack, and then start trying to fetch values from that newly defined array. The values you get from that array will be garbage values from the memory assigned to the array.

What makes you think that your flowSource array could possibly contain valid values? Where are your values supposed to be coming from?

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top