memset in your code will set all bits to 0, which may or may not be what you want to do. In particular, this does not guarantee that a pointer with all zero bits is a null pointer. In addition, the floating value with all zero bits is zero.
If you want your code to be fully portable, you must initialize each element.
my_struct_t *arr = malloc(N * sizeof arr[0]); const my_struct_t default_my_struct = { 0 }; for (int i=0; i<N; i++) arr[i] = default_my_struct;
Or you can initialize with the C99 literal:
my_struct_t *arr = malloc(N * sizeof arr[0]); for (int i=0; i<N; i++) arr[i] = (my_struct_t) { 0 };
In practice, you will have to work hard to find a C implementation for which the above code will have a different result from your version that uses memset .
David heffernan
source share