This is the mail archive of the libstdc++@gcc.gnu.org mailing list for the libstdc++ project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: valarray_copy implementation question


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


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]