Objectmix
Tags Register Mark Forums Read

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

This is a discussion on std::fill()-ing a volatile array (via pointers) within the c++ forums in Programming Languages category; 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 ...


Object Mix > Programming Languages > c++ > std::fill()-ing a volatile array (via pointers)

c++ comp.lang.c++ usenet archive

Reply

 

LinkBack Thread Tools
  #1  
Old 07-17-2008, 12:36 PM
Junior Member
 
Join Date: Nov 2009
Posts: 0
Application Development is on a distinguished road
Default std::fill()-ing a volatile array (via pointers)

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.

--
Gennaro Prota | <https://sourceforge.net/projects/breeze/>
Do you need expertise in C++? I'm available.
  #2  
Old 07-17-2008, 01:05 PM
Junior Member
 
Join Date: Nov 2009
Posts: 0
Application Development is on a distinguished road
Default Re: std::fill()-ing a volatile array (via pointers)

On Jul 17, 1:36 pm, Gennaro Prota <inva...@yahoo.com> wrote:
> 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.
>
> --
>   Gennaro Prota  |  <https://sourceforge.net/projects/breeze/>
>     Do you need expertise in C++?   I'm available.


The fill will work fine.
Reply

Thread Tools



All times are GMT -5. The time now is 12:48 AM.

Managed by Infnx Pvt Ltd.