I have an existing 1D array, is memset the fastest way to zero it?
I have an existing 1D array, is memset the fastest way to zero it?
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Fastest … probably yes.
Buggy almost sure!
It mostly depends on the implementation, platform and … what type the array contains.
In C++ when a variable is defined its constructor is called. When an array is defined, all the array’s elements’ constructors are called.
Wiping out the memory can be considered “good” only for the cases when the array type is know to have an initial state that can be represented by all zero and for which the default constructor doesn’t perform any action.
This is in general true for built-in types, but also false for other types.
The safest way is to assign the elements with a default initialized temporary.
Note that, if T is
char, the function instantiates and translates exactly asmemset. So it is the same speed, no more no less.