I was curious as to whether or not there was any advantage in regards to efficiency to utilizing memset() in a situation similar to the one below.
Given the followin
Your variable p
is only required for the initialisation loop. The code for the memset should be simply
memset( my_buffer, 0, sizeof(my_buffer));
which is simpler and less error prone. The point of a void*
parameter is exactly that it will accept any pointer type, the explicit cast is unnecessary, and the assignment to a pointer of an different type is pointless.
So one benefit of using memset()
in this case is to avoid a unnecessary intermediate variable.
Another benefit is that memset() on any particular platform is likely to be optimised for the target platform, whereas your loop efficiency is dependent on the compiler and compiler settings.