Search code examples
cgccmemset

Is it guaranteed that memset will zero out the padding bits in a structure?


In general ,as per C standard is it guaranteed that memset() with 0 will zero out the padding bits in a C structure?

What about gcc?

For example , something like:

struct MyStruct
{
    unsigned char   member1;
    unsigned int    member2;
    char        member3;
    unsigned char   member4;
    float       member5;    
};

struct MyStruct ms;

memset(&ms, 0, sizeof( struct MyStruct));

Solution

  • Perhaps worth noting that memset doesn't know anything about your struct (or array, or primitive, or any chunk of memory whatsoever that you happen to unleash it on), so even if it wanted to leave the padding bits intact, it wouldn't even know where they are.