> The main reason (other than stylistic and religious) for not using memcmp on structs is because structs are often not perfectly aligned and so contain padding bytes. The padding bytes are uninitialized, unpredictable and inaccessible unless you also guarantee each struct has first been memset(&s,0,sizeof(s)).
>
> In general, memcmp is highly optimized for the architecture and is in fact faster than a bespoke operator. However you can't use memcmp with structs unless you guarantee they're always fully initialized with memset to the full sizeof(yourstruct).
Thanks Daniel - I didn't know that, so I'll keep it in mind (and probably
just implement operator==)
On Thu, 15 Dec 2011, Daniel van Vugt wrote:
> The main reason (other than stylistic and religious) for not using memcmp on structs is because structs are often not perfectly aligned and so contain padding bytes. The padding bytes are uninitialized, unpredictable and inaccessible unless you also guarantee each struct has first been memset( &s,0,sizeof( s)).
>
> In general, memcmp is highly optimized for the architecture and is in fact faster than a bespoke operator. However you can't use memcmp with structs unless you guarantee they're always fully initialized with memset to the full sizeof(yourstruct).
Thanks Daniel - I didn't know that, so I'll keep it in mind (and probably
just implement operator==)
> -- /code.launchpad .net/~smspillaz /compiz- core/fix_ 894688/ +merge/ 84058
> https:/
> You are the owner of lp:~smspillaz/compiz-core/fix_894688.
>