N
Noob
Hello everyone,
Typically when I want to initialize an auto struct to
"all-0" values, I write
struct foo bar = { 0 };
and let the compiler correctly set arithmetic values
to the appropriate 0 or 0.0 and pointers to NULL.
But when I have a malloced struct, I am often tempted
to write
struct foo *bar = malloc(sizeof *bar);
memset(bar, 0, sizeof *bar);
even though I know this is not guaranteed to set
floating-point fields to 0.0 and pointers to NULL.
What are my options then? I suppose I should write
struct foo zero = { 0 };
struct foo *bar = malloc(sizeof *bar);
*bar = zero;
to get the behavior I want, safely?
Regards.
Typically when I want to initialize an auto struct to
"all-0" values, I write
struct foo bar = { 0 };
and let the compiler correctly set arithmetic values
to the appropriate 0 or 0.0 and pointers to NULL.
But when I have a malloced struct, I am often tempted
to write
struct foo *bar = malloc(sizeof *bar);
memset(bar, 0, sizeof *bar);
even though I know this is not guaranteed to set
floating-point fields to 0.0 and pointers to NULL.
What are my options then? I suppose I should write
struct foo zero = { 0 };
struct foo *bar = malloc(sizeof *bar);
*bar = zero;
to get the behavior I want, safely?
Regards.