Home > Back-end >  Are stack-allocated arrays in C zero'd out by default in C?
Are stack-allocated arrays in C zero'd out by default in C?

Time:07-20

When I allocate memory on the heap with malloc(), there might be garbage in that memory unless I use calloc() or memset() to zero it out. Is this the same situation for the stack, or can I safely assume anything I allocate on the stack won't have garbage in it? Also, does this differ on different systems or OSes?

CodePudding user response:

It is the same situation on the stack.

Unless you explicitly initialize the values of variables to zero or some other value, variables with automatic storage duration will have indeterminate values.

  • Related