Home

On Sep 20, 6:34 pm, werasm <wer...@gmail.com> wrote:
> James Kanze wrote:
> > Not deprecated. It never officially existed, at least in
> > standard C. It was present in the Berkley distributions of
> > Unix, and is part of Posix, but in the supplementary header
> > <strings.h> (not <string.h>). Given that it's deprecated by
> > Posix, however, and was never part of standard C, I'd definitely
> > replace it by memset (which is standard, and declared in
> > <string.h>---or you can use std::memset, and <cstring>).

> Maybe I'm slightly paranoid, but I would rather use something
> like this...

Not paranoid, just good programming practice.

> #include <cstddef>
> #include <algorithm>

> template <class T, std::size_t N>
> void zero( T(&array)[N] )
> {
> std::fill( array, array+N, T() );
> }

> int main()
> {
> char array[] = "Hallo";
> zero( array );
> }

> ... as I've seen memset being used erroneously by mistake
> one to many times. OTOH I think you would probably too.

Now that you mention it, yes. Memset only works for C style
arrays of integers. std::fill (and std::fill_n) works for
everything. (And in C, it was a common error to see memset used
for arrays of double or of pointer.)

--
James Kanze (GABI Software) email:james.kanze@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

previous
next

Re: Python is overtaking Perl
Re: Python 3K or Python 2.9?
Re: I am new to programming and wanted to know where a good placeto start for C++
Re: What is going on here?
Re: Construct On First Use Idiom example from FAQ
Dzieci Niczyje
Fundacja Sloneczko
Fundacja Iskierka
Pajacyk
Rodzic Po Ludzku