Post has multiple issues.
Following does not have balanced ()
. Unclear what OP's true code may be.
memset(large_float_array, 0, sizeof(large_float_array);
Assuming the above simple used another )
, then code was only zero filling a few bytes with zero as sizeof(large_float_array)
is the size of a pointer.
memset(large_float_array, 0, sizeof(large_float_array));
OP likely wanted something like which would zero fill the allocated space with zero bits. This will bring the allocated memory into the same state as calloc()
.
memset(large_float_array, 0, NUM *sizeof *large_float_array);
Code should use the following. A good compiler will optimize this trivial loop into fast code.
for (size_t i=0; i<NUM; i++) {
large_float_array[i] = 0.0f;
}
A way to use the fast memcpy()
is to copy the float
into the first element of the array, then the first element to the 2nd element, then the first 2 elements to elements 3 & 4, then the first 4 elements to elements 4,5,6,7 ...
void *memset_size(void *dest, size_t n, const char *src, size_t element) {
if (n > 0 && element > 0) {
memcpy(dest, src, element); // Copy first element
n *= element;
while (n > element) {
size_t remaining = n - element;
size_t n_this_time = remaining > element ? element : remaining;
memcpy((char*)dest + element, dest, n_this_time);
element += n_this_time;
}
}
return dest;
}
float pi = M_PI;
float a[10000];
memset_size(a, 10000, &pi, sizeof pi);
As with all optimizations, consider clearly written code first and then employ candidates such as the above in the select cases that warrant it.