开发者

What is the 'garbage value' in 'Left operand of '/' is a garbage value' warning generated by "Build & Analyze"?

开发者 https://www.devze.com 2023-01-20 23:28 出处:网络
When I \'Build and Analyze\" this code in Xcode, I get a warning that I don\'t understand. Here\'s the method with the problem:

When I 'Build and Analyze" this code in Xcode, I get a warning that I don't understand. Here's the method with the problem:

-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{
        UITouch * touch = [touches anyObject];
        CGPoint location = [touch locationInView:self];
        CGPoint relativePosition = CGPointMake(1.0-(location.x / self.bounds.size.width),location.y / self.bounds.size.height);
        [[Stage getSharedStage] movePa开发者_如何学GortToLocation:relativePosition];
}

Here's the warning:

 warning: The left operand of '/' is a garbage value
         CGPoint relativePosition = CGPointMake(1.0-(location.x / self.bounds.size.width),location.y / self.bounds.size.height);
                                                     ~~~~~~~~~~ ^
1 warning generated.

Here are the arrows:

What is the 'garbage value' in 'Left operand of '/' is a garbage value' warning generated by "Build & Analyze"?

What is it trying to tell me? The code works fine.


It's hard to tell without seeing the entire function, but I would take that to mean that there exists a path the code could take where location.x is never initialized. It may be that the code never takes that path in your testing, but the possibility is there.

EDIT: I'm going to take a wild guess here and say that it's because [touches anyObject] could conceivably return nil. In which case [touch locationInView:self] will return garbage (remember sending messages to nil is perfectly valid).

Try making the rest of the function conditional on (touch != nil).


If touch is nil, [touch locationInView:] will return the nil struct result, which before some version of llvm would only guarantee that the first [size of pointer] bytes of the struct are zeroed, and the rest would still be garbage.

AFAIK [touches anyObject] can never be nil and the warning is a false positive.


the left operand of '/' is a garbage value ,This suggests tell us that must give an initial value to location.x , because [touch locationInView:self] maybe nil,so Must make a judgment whether the if(touch!=nil)

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号