Overview
Examples
Screenshots
Comparisons
Applications
Download
Documentation
Tutorials
Bazaar
Status & Roadmap
FAQ
Authors & License
Forums
Funding Ultimate++
Search on this site
Search in forums












SourceForge.net Logo
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 Go to next message
xrysf03 is currently offline  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* Very Happy

Come to think of that, if I zero-pad the storage allocated behind a "double", do I actually achieve the same as
double my_var = 0;  // ?


Recommendations welcome Smile

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 #52639 is a reply to message #52637] Sat, 02 November 2019 21:52 Go to previous messageGo to next message
Oblivion is currently offline  Oblivion
Messages: 689
Registered: August 2007
Contributor
Hello Frank,

And welcome to U++ forums.

Usually, the Upp::Zero() template works fine. Smile

double darray[20];
Zero(darray);


Best regards,
Oblivion


Re: An U++ equivalent of bzero ? (if not a sin) [message #52641 is a reply to message #52639] Sat, 02 November 2019 22:26 Go to previous messageGo to next message
Klugier is currently offline  Klugier
Messages: 660
Registered: September 2012
Location: Poland, Kraków
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


Ultimate++ - one framework to rule them all.

[Updated on: Sat, 02 November 2019 22:36]

Report message to a moderator

Re: An U++ equivalent of bzero ? (if not a sin) [message #52645 is a reply to message #52637] Sun, 03 November 2019 22:19 Go to previous messageGo to next message
xrysf03 is currently offline  xrysf03
Messages: 43
Registered: November 2018
Location: CZ
Member
...even better than I thought. Thanks for your exhaustive answers gentlemen Smile

Frank
Re: An U++ equivalent of bzero ? (if not a sin) [message #52646 is a reply to message #52637] Sun, 03 November 2019 22:34 Go to previous messageGo to next message
xrysf03 is currently offline  xrysf03
Messages: 43
Registered: November 2018
Location: CZ
Member
Actually come to think of that... what if I allocate the buffer dynamically?

double* array_ptr = new double[some_calculated_size];


Will Upp::Zero() know the right size, by any chance? (Talk to the allocator behind the scenes? That would not be very good as a general approach, as in general a pointer needn't point to the beginning of an allocated chunk of memory, it can point to someplace inside an allocated buffer etc.) = I guess I'd better decide whether to use a proper container, or use manual iteration, or stick to memset()...

BTW that floating-point literal zero, written as 0.0, that's a nice trick I didn't know about, thanks Klugier Smile
Re: An U++ equivalent of bzero ? (if not a sin) [message #52647 is a reply to message #52646] Sun, 03 November 2019 23:08 Go to previous message
Oblivion is currently offline  Oblivion
Messages: 689
Registered: August 2007
Contributor
Hello Frank,

If you're working with U++ containers, but want to work with dynamic buffers with C-like characteristics, then you can simply use Buffer (ıt is the closest thing to C arrays (with C++ benefits: lets you avoid new/delete, for one. Also it let's you specifiy the initial value.):

Buffer<double> darray(200, 0.0);

for(int i = 0; i < 200; i++)
  Cout() << darray[i] << "\n";


Best regards,
Oblivion


[Updated on: Sun, 03 November 2019 23:12]

Report message to a moderator

Previous Topic: [SOLVED] Empty values to the "EditInt" widget and the "int" basic type
Next Topic: Upp::Vector FindIndex by value of Object
Goto Forum:
  


Current Time: Thu Jul 09 11:45:52 CEST 2020

Total time taken to generate the page: 0.01189 seconds