valarray_copy implementation question
Christian BRUEL
christian.bruel@st.com
Wed Nov 18 09:52:00 GMT 2009
Hello,
Thanks a lot for this clarification, I understand that the destination
must contain the 'n' selected elements assigned ("compacted", not at
their original slot). Re-reading the 26.3.8.2 chapter with this in mind
become suddenly perfectly clear :-).
But regarding my original out-of-bound src report, I'm still convinced
that another test is needed, or a change in the iterated is needed.
Couldn't we just iterate on the mask and simplify the valarray_copy
loops as something like :
_Tp* __p (__a._M_data);
_Tp* __q (__b._M_data);
for (__ok = __m.M_data; __ok < __ok + __n; ++__ok, ++__p)
{
if (*__ok )
*__q++ = *__p;
}
many thanks,
Christian
Gabriel Dos Reis wrote:
> On Mon, Nov 16, 2009 at 9:26 AM, Christian BRUEL <christian.bruel@st.com> wrote:
>> Hello,
>>
>> The standard interpretation that lead to the implementation of
>> __valarray_copy used for the mask_array assignment in the valarray looks
>> strange.
>>
>> From what I see in the comments and implementation (xsee
>> valarray_array.tcc), there is a big possibility to go out of bounds if there
>> is at least a false value in the boolean mask before the first true ones.
>>
>> The comments are indeed explicit:
>>
>> // Copy n consecutive elements from a into elements of b. Elements
>> // of b are skipped if the corresponding element of m is false. m
>> // must contain at least n true elements. b must have at least as
>> // many elements as the index of the nth true element of m. I.e. if
>> // m has 15 elements with 5 false followed by 10 true, b must have
>> // at least 15 elements.
>>
>> But in the 14882:1998 standard section 26.3.8.2 all I see is that the mask
>> acts as a selector on the source arguments, so their is a constraint that
>> the size of the bool array is smaller or equal to the size of the
>> destination array, regardless of mask values.
>>
>> I think the words "selected elements" means that the elements are selectable
>> and so that me must stop after n elements, not after n true elements. But
>> I'm quite not sure how to read this standard.
>>
>> Thanks for any clarification. if this is a possible bug I'll post to
>> bugzilla.
>>
>> Christian
>
> Christian,
>
> Before we get deep into implementation details (and worry about
> whatever valgrind might say), I believe we should first concentrate
> on the semantics.
>
> The slice_array, gslice_array, mask_array and friends have no other
> purpose but to help implement array expressions. In the specific
> case at hand, the idea is that given an `dst_ary' and a `mask', we
> can select slots to assign to with values from a `src_ary', as in
>
> dst_ary[mask] = src_ary;
>
> In this statement, `dst_ary' and `mask' must have the same length
> (and the valarray component is free to assume they are, without
> having to check). Then the expression `dst_ary[mask]' conceptually
> constructs an array of some length `n'. That `n' is the number
> of selected elements (number of 'true' slots in the mask). The source
> array `src_ary' must be of length `n' -- but the valarray component
> is free to assume that is the case, without having to check.
>
> Does that help?
>
> -- Gaby
>
More information about the Libstdc++
mailing list