Search code examples
cinitializationmemset

What is the reason for memsetting initialized buffer


While traversing Wikipedia following some links, I stumbled across the following code example that initializes a char buffer to 0, but then memsets it to 0 before use. Is this necessary? If so, why? The reason I ask is that I am no expert, and the example clearly states that this was the coder's intention with the comment "/* Really initialized to zeroes */" on the memset, as opposed to "/* initialized to zeroes */" on the initialization.

EDIT: Note, I've rolled back the edit on the wikipedia page that caused this, so it is no longer visible in the link.


Solution

  • char buffer[5] = {0};  /* initialized to zeroes */
    
    /* some declaration / statements, but no access to buffer object */
    
    memset ( buffer, 0, sizeof buffer); /* Really initialized to zeroes */
    

    in the above code, the call to memset is totally useless. buffer is already guaranteed to be initialized to 0.