4 ms·
2nd question I got CV_IMPL IplImage * icvCreateIsometricImage(....) { .... if (!dst || dst->depth != desired_depth || dst->nChannels != de
by xedarius 12y ago
2nd question I got
CV_IMPL IplImage * icvCreateIsometricImage(....)
{
....
if (!dst || dst->depth != desired_depth ||
dst->nChannels != desired_num_channels ||
dst_size.width != src_size.width ||
dst_size.height != dst_size.height)
....
}
The website said the error was 'height was compared with itself', fair enough ... but there is a much much bigger crime here.
I clicked on the (!dst ||) as the code checks for a null pointer and then follows that with a dereference of that pointer which will get executed because of the || operator.
If the pointer is null the code will still crash.
The website said I was wrong .... I didn't bother with any more questions after that.
- JoeAltmaier 12y agoYou're thinking of the '|' operator, which evaluates both arguments. The '||' operator is defined to never evaluate the 2nd if the first is true; its traditionally used exactly for this purpose e.g. if (!p || p->...) or more likely if (p && p->IsGood(...)) {edit: false -> true}
- xedarius 12y agoI have to put my hand up here and admit I got over excited and I was wrong. Thanks for correcting me.
- physPop 12y agoThe website was right. http://en.wikipedia.org/wiki/Short-circuit_evaluation http://en.wikipedia.org/wiki/Short-circuit_evaluation
- RogerL 12y agoAs others have pointed out, || is short circuit. I'm chiming in not to pile on, but to point out this is a canonical code pattern in C and C++. Check the pointer, then deference it, all in the same if expression. if (ptr && ptr->foo... or if (!ptr || !ptr->foo...