[futurebasic] Re: [FB] C array garbage value

Message: < previous - next > : Reply : Subscribe : Cleanse
Home   : February 2017 : Group Archive : Group : All Groups

From: Brian S <fblistserve@...>
Date: Fri, 10 Feb 2017 08:57:57 -0700
> On Feb 10, 2017, at 7:57 AM, Brian S <fblistserve@...> wrote:
> 
> 
>> Bernie wrote:
>> 
>> Clang (analysis) complains of a garbage value in MyFunction1. To remove the warning, I created an NSArray instead (MyFuntion2).
>> 
>> Anyone got a better idea?
>> 
>> // ---------------
>> BeginCFunction
>> void MyFunction1( int count ) {
>>   int dim[count];
>>   for ( int d = 0; d < count; d++ ) {
>>       dim[d] = 15;
>>   }
>>   int d = dim[1];// garbage value
>>   NSLog(@"%d",d);
>> }
> 
> clang analysis allows for all possibilities even if they wouldn’t happen when code runs. In the above code, from clang’s point of view 'count' could arrive to the function as a negative number, which means the 'for' loop doesn’t execute and the 'dim' array isn’t populated. So any use of 'dim' later could potentially access garbage data. Of course, the programmer might take efforts to always pass a positive integer but that is unknown to clang( most of the time ).

Which means:

MyFunction1( -1 );  // will produce garbage values.


Brian