By passing an object through const and through & to the same function

I have an image function declared as

thresholding( const Image &imgSrc, Image &imgDest );

      

What happens if I do this,

Image img;
tresholding( img, img );

      

Is it well defined? Because in this case it img

changes.

PS: threshold

reads imgSrc

and if `imgSrc [i] lowerThr → imgDest [i] = 255 else imgDest [i] = 0

More precisely:

__m128i _mm_src = _mm_load_si128 ( any_value );
__m128i _mm_src = _mm_load_si128 ( (__m128i*) &imgSrc[0] );
__m128i _mm_dest = _mm_load_si128 ( (__m128i*) &imgDest[0] );


_mm_dest = mm_cmpgt_epi16 (a, thr);

      

+3


source to share


2 answers


Having multiple references with possibly different types to the same object (as well as multiple pointers to the same object) is not problematic. Function behavior can be fine too: an expression like

imgDest[i] = (imgSrc[i] < lowerThr) * 255;

      



clearly defined for imgSrc

and imgDest

referring to the same object. No os sequencing problems occur. You should check the documentation or source for threshold

, although be sure it can be implemented in such a way that it needs to imgSrc

be constant at runtime. You shouldn't make assumptions without knowing the implementation.

+4


source


Whether or not this is good depends on the specification of the function. The language allows multiple references to the same object, even if some are const

-qualified and others are not, but many functions don't. Any changes made with imgDest

will be immediately visible through imgSrc

, and many functions with parameters similar to yours assume that after changes to the imgDest

original image is still available in imgSrc

. If your function makes such an assumption, then it's easy to say that the language allows such a call, but the language is said to allow printf(NULL);

: of course, technically it does, but if you're also considering the function specification, then it's undefined.



+4


source







All Articles