The assignment value is garbage or undefined

I posted a screenshot of my error code. enter image description here

heights conclusion

enter image description here

please can anyone help me?

+4
source share
2 answers

I think the static analyzer does not see how _numberOfColumns can become non-zero and therefore insist that garbage is assigned. You need to verify that you are actually providing some facilities for _numberOfColumns to become non-zero.

Usually, when I write loops that want to find the largest or smallest value, I initialize the size variable to the largest (if I want the smallest) or smallest (if I want the largest) amount, and I think this will solve most of your problems:

 float shortestHeight = FLT_MAX; for (unsigned i = 0; i < _numberOfColumns; i++) { // etc. } 
+2
source

The analyzer is correct. Your code will gain access to garbage memory if _numberOfColumns is 0, thereby allocating 0 bytes for heights , making garbage heights[0] . The analyzer does not know what values _numberOfColumns values ​​can have, but you can say this with assert(_numberOfColumns>0) .

Take this program C, for example:

 int main(int argc, const char * argv[]) { int n = argc-1; int *a = malloc(n*sizeof(int)); for (int i=0; i<n; i++) { a[i] = i; } int foo = a[0]; free(a); return foo; } 

the size of a is determined by the number of arguments. If you have no arguments n == 0 . If you are sure that your program (or only this part of your program) will always assign something more from 0 to a , you can use the statement. Adding assert(n>0) will tell the analyzer exactly.

+1
source

Source: https://habr.com/ru/post/1469189/


All Articles