std::fill()-ing a volatile array (via pointers)

G

Gennaro Prota

Hi,

I have the following function template

template< typename T, std::size_t n >
void
secure_fill( volatile T ( &arr )[ n ], const T & value = T() )
{
for( std::size_t i( 0 ); i < n; ++i ) {
arr[ i ] = value;
}
}

which I use where I want to be sure that the referenced array is
always written to and the compiler doesn't optimize away the call
"just because" the array itself isn't touched thereafter.

Now, if I want to avoid the hand-coded loop and take advantage of the
standard library (debug mode and anything else it might provide) I
change it to:

template< typename T, std::size_t n >
void
secure_fill( volatile T ( &arr )[ n ], const T & value = T() )
{
std::fill( arr, arr + n, value );
}

Is this guaranteed to work? I'm inclined to think that the answer is
"yes", because I pass regular pointers as iterators and
std::iterator_traits< T * >::value_type is T, which in my case is
volatile-qualified.

Other opinions? The standard says:

[std::fill] assigns value through all the iterators in the
range [first ,last ).

so I guess it all depends on how "assign through an iterator" in
interpreted and whether it provides for anything else than

*iter = value

or the obvious variants "*iter++ = value", etc.
 
A

AnonMail2005

Hi,

I have the following function template

    template< typename T, std::size_t n >
    void
    secure_fill( volatile T ( &arr )[ n ], const T & value = T() )
    {
        for( std::size_t i( 0 ); i < n; ++i ) {
            arr[ i ] = value;
        }
    }

which I use where I want to be sure that the referenced array is
always written to and the compiler doesn't optimize away the call
"just because" the array itself isn't touched thereafter.

Now, if I want to avoid the hand-coded loop and take advantage of the
standard library (debug mode and anything else it might provide) I
change it to:

    template< typename T, std::size_t n >
    void
    secure_fill( volatile T ( &arr )[ n ], const T & value = T() )
    {
        std::fill( arr, arr + n, value );
    }

Is this guaranteed to work? I'm inclined to think that the answer is
"yes", because I pass regular pointers as iterators and
std::iterator_traits< T * >::value_type is T, which in my case is
volatile-qualified.

Other opinions? The standard says:

    [std::fill] assigns value through all the iterators in the
    range [first ,last ).

so I guess it all depends on how "assign through an iterator" in
interpreted and whether it provides for anything else than

    *iter = value

or the obvious variants "*iter++ = value", etc.

The fill will work fine.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,053
Latest member
BrodieSola

Latest Threads

Top