Home » Community » Newbie corner » [solved]An U++ equivalent of bzero ? (if not a sin) (Can I bzero a flat array of double, or should I use some container template instead?)
[solved]An U++ equivalent of bzero ? (if not a sin) [message #52637] |
Sat, 02 November 2019 19:34  |
xrysf03
Messages: 43 Registered: November 2018 Location: CZ
|
Member |
|
|
Dear gentlemen,
while messing with my toy proggie, I've reached a point where I need to initialize an array of "double" (the double-length floating point type) - as an accumulation buffer of sorts.
Defined roughly as
double my_accu_buf[SOME_PARTICULAR_INTEGER_SIZE];
Can I just use the bzero() function that I know from GNU Libc? The MinGW compiler behind U++ cannot find that function, even if I #include <strings.h> . Ahaa, memset() does work (I don't even need to #include <string.h>). It's true that "man bzero" says "nono, deprecated, use memset() instead". Or is there some U++ equivalent? Or, should I refrain from using the unsafe and ugly, plain old C arrays, and use some container template instead? Such as the Vector... And of course I can just iterate across the array, but that feels so *meh* 
Come to think of that, if I zero-pad the storage allocated behind a "double", do I actually achieve the same as
Recommendations welcome 
Frank
[Updated on: Tue, 12 November 2019 22:18] Report message to a moderator
|
|
|
|
Re: An U++ equivalent of bzero ? (if not a sin) [message #52641 is a reply to message #52639] |
Sat, 02 November 2019 22:26   |
 |
Klugier
Messages: 1099 Registered: September 2012 Location: Poland, Kraków
|
Senior Contributor |
|
|
Hello Frank,
For plain arrays you can use std::fill from standard library:
#include <iostream>
int main() {
double array[10];
std::fill(std::begin(array), std::end(array), 0.0);
for (int i = 0; i < 10; ++i) {
std::cout << array[i] << "\n";
}
return 0;
}
Alternatively, you could use std::array that have compilation time defined size:
#include <iostream>
#include <array>
int main() {
std::array<double, 10> array;
array.fill(0.0);
for (auto d : array) {
std::cout << d << "\n";
}
return 0;
}
I prefer to use std::fill for plain arrays, because it is probable solution and works outside U++, however it requires c++17 standard. However, if your app is designed to work with Upp framework than you can freely use Upp::Zero, which is easy to use (easier than std::fill).
Sincerely,
Klugier
U++ - one framework to rule them all.
[Updated on: Sat, 02 November 2019 22:36] Report message to a moderator
|
|
|
|
|
|
Goto Forum:
Current Time: Tue May 13 10:50:25 CEST 2025
Total time taken to generate the page: 0.02460 seconds
|