473,395 Members | 2,436 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

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.
Jul 17 '08 #1
1 2728
On Jul 17, 1:36*pm, Gennaro Prota <inva...@yahoo.comwrote:
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.
Jul 17 '08 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: ma740988 | last post by:
Consider: # include <iostream> using std::cout; using std::cin; using std::endl; # include <list> using std::list;
1
by: Saeed Amrollahi | last post by:
Dear All C++ Programmers Hello I am Saeed Amrollahi. I am a software engineer in Tehran Sewerage Company. I try to use std::map and map::find member function. I use Visual Studio .NET. my...
12
by: Steven T. Hatton | last post by:
I know of a least one person who believes std::ifstream::read() and std::ofstream::write() are "mistakes". They seem to do the job I want done. What's wrong with them. This is the code I...
2
by: ma740988 | last post by:
The hierachy for standard exception lists: bad_alloc, bad_cast, bad_typeid, logic_error, ios_base::failure, runtime_error and bad_exception runtime_error and logic_error has subsets. The...
6
by: Geoffrey S. Knauth | last post by:
It's been a while since I programmed in C++, and the language sure has changed. Usually I can figure out why something no longer compiles, but this time I'm stumped. A friend has a problem he...
12
by: ma740988 | last post by:
I've allocated 4K memory and I'd like to use std::fill to fill each 1K with a different value. Note: I could easily use a vector/deque but I'm interested in a C style array. int main() { int...
18
by: ma740988 | last post by:
Trying to get more acclimated with the use of function objects. As part of my test, consider: # include <vector> # include <iostream> # include <algorithm> #include <stdexcept> #include...
6
by: ma740988 | last post by:
I've been perusing for some time now - I suppose some of the algorithms in Josuttis. 'Today', I do alot of funny math on data resident within memory. As part of this. I find myself using...
2
by: eb | last post by:
I have this working code : foo.h /* nothing */ foo.cpp ... std::vector<std::vector<T * my_T(board_size,board_size) ; for (i=0; i<board_size; i++)
16
by: JDT | last post by:
Hi, Can someone show me how to set any integer (or float) in an std::vector as zero in a way other than using a for loop? Can we apply memset() or ZeroMemory() to the vector? Thanks for your...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.