The fastest ... maybe yes. Buggy is almost sure!
It basically depends on the implementation, the platform, and ... what type the array contains.
In C ++, when a variable is defined, its constructor is called. When an array is specified, all array element constructors are called.
Clearing the memory can be considered “good” only for cases when it is known that the type of the array has an initial state, which can be represented by all zero and for which the default constructor does not perform any actions.
This is generally true for built-in types, but also for other types.
The safest way is to set the elements to a default initialized temporary.
template<class T, size_t N> void reset(T* v) { for(size_t i=0; i<N; ++i) v[i] = T(); }
Note that if T is char , this function instantiates and converts exactly like memset . Thus, this is the same speed, nothing more.
Emilio garavaglia
source share