是否保证memset将结构中的填充位清零? [英] Is it guaranteed that memset will zero out the padding bits in a structure?

查看:77
本文介绍了是否保证memset将结构中的填充位清零?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

通常,按照C标准,是否保证memset()为0会使C结构中的填充位清零?

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

那gcc呢?

例如,类似:

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

struct MyStruct ms;

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

推荐答案

也许值得一提的是,memset对您的结构(或数组,基元或任何内存块)一无所知),因此即使它希望保留填充位完整无缺,它甚至都不知道它们在哪里.

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.

这篇关于是否保证memset将结构中的填充位清零?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆