From: Thomas Richter on
Hicham Mouline wrote:
> Hello,
>
> Are there any articles comparing the cases/reasons when/why to use memset vs
> fill, vs writing a loop by hand?

std::memset and std::fill are two very different beasts. std::memset
only fills memory with byte patterns, regardless of what this pattern
means, and ignoring any type of assignment operator the class defined.
If you're lucky, this might do the right thing; often, you're not that
lucky.

For example, std::memset *typically* works fine for setting int or
character arrays to zero, *maybe* double or float values if your machine
is IEEE based, but that is non-portable. If you use it to initialize an
array of PODs, you might get away. If the array is of non-PODs, the
result is very likely *not* what you want.

Conclusion: Unless you don't care about portability, and you're very
very sure that you know what you're doing, you might try memset.
Otherwise, hands off.

std::fill is type-aware and does always the right thing. If the compiler
is smart, it should be as fast (or almost as fast) as std::memset(), the
latter often using a special compiler built-in to fill the memory.
However, memset as an optimization is rarely ever worth it (or rather,
if your algorithm has to reset large arrays of data, it might be worth
trying to reconsider the algorithm should this really be the bottleneck
of your program).

A manual loop does of course also do the right thing, but is less
generic (i.e. container dependent). Whether that makes it better or
worse is in the eye of the beholder. For a short C-style array, I
personally prefer the manual loop since I consider it more readable, but
others might disagree.

So long,
Thomas


--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

From: Mathias Gaunard on
On May 31, 4:10 pm, "Hicham Mouline" <hic...(a)mouline.org> wrote:
> Hello,
>
> Are there any articles comparing the cases/reasons when/why to use memset vs
> fill

std::fill works for any pair of iterators, memset only works for
contiguous memory.


> vs writing a loop by hand?

Using std::fill or memset makes the code more explicit, and also
potentially more efficient.


--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

From: Alf P. Steinbach on
* Hicham Mouline, on 31.05.2010 17:10:
>
> Are there any articles comparing the cases/reasons when/why to use memset vs
> fill, vs writing a loop by hand?

Others have enumerated reasons why std::fill is generally superior to memset.

However, the most common use of memset seems to be a use case where neither
std::fill nor memset are appropriate, namely like

SomePodStruct foo;
memset( appropriate args here );

which is brittle, verbose and unnecessary.

To zero that struct after initialization, just do

foo = SomePodStruct();

Not that I recommend such "reuse" of a variable (implied by the zeroing), but I
think it's far more clear and much less brittle than a memset if it's needed.

A proper way to zero that struct at initialization is

SomePodStruct foo = {};

I guess the programmers who choose memset for this do that because they're used
to it in C.


Cheers & hth.,

- Alf

--
blog at <url: http://alfps.wordpress.com>

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

From: Andrei Alexandrescu on
On 05/31/2010 10:10 AM, Hicham Mouline wrote:
> Hello,
>
> Are there any articles comparing the cases/reasons when/why to use memset vs
> fill, vs writing a loop by hand?

I wrote an article on exactly that topic a while ago:

http://www.drdobbs.com/web-development/184403799

with a rather scary conclusion, which I quote:

====
There is a very deep, and sad, realization underlying all this. We are
in 2001, the year of the Spatial Odyssey. We've done electronic
computing for more than 50 years now, and we strive to design more and
more complex systems, with unsatisfactory results. Software development
is messy. Could it be because the fundamental tools and means we use are
low-level, inefficient, and not standardized? Just step out of the box
and look at us � after 50 years, we're still not terribly good at
filling and copying memory.
====

I haven't measured in a while; I hope, but I doubt, that we're in a better shape 9 years later.


Andrei


--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

From: gast128 on
> if your algorithm has to reset large arrays of data, it might be worth
> trying to reconsider the algorithm should this really be the bottleneck
> of your program).

I partially agree. Clearing (byte) buffers with memset isn't less
readable and might even be performance intensive (e.g. using images or
bitmaps which have often considerable large pixel buffers).

Also have a look at some memset implementations. VStudio uses even
SSE2 (if present) for memset to get the last percent performance
improvement. Even without this SSE2 stuff, it seems a non trivial
piece of assembly code, which might be hard to reproduce by an
ordinary compiler.

Personal I use memset only in scenarios with buffers of build in
types, like the example above which imho isn't less readable or a c-
style code.

--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]