3 ms·
> The memory is uninitialized so it is a good idea to initialize it to zero bytes. We can do this by calling the memset function with the pointer to our new str
by juunpp 3y ago
> The memory is uninitialized so it is a good idea to initialize it to zero bytes. We can do this by calling the memset function with the pointer to our new struct, the initialization value 0, and the size of our structure:
Or just calloc(), which also takes a count and an element size, convenient when you're allocating arrays.
- jonhohle 3y agoI thought the same thing. Why is calloc not widely used? That said, when initializing structs `= { 0 }` can be used to set all members 0. And at least in more recent compilers you can used the designated initializer with dynamic allocation by casting: Foo* f = malloc(sizeof(Foo)); *f = (Foo) { .bar = 1, .baz = “car” }; If a member is missed, it will be set to zero (the value for uninitialized static storage).