Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

c

gcc

memset

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));
like image 747
Lunar Mushrooms Avatar asked Dec 18 '11 04:12

Lunar Mushrooms


1 Answers

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.

like image 143
Gravity Avatar answered Sep 24 '22 10:09

Gravity