comp.lang.c
[Top] [All Lists]

Re: Clearing Padding in Structure

Subject: Re: Clearing Padding in Structure
From: Martin
Date: Wed, 26 Mar 2008 11:26:23 -0000
Newsgroups: comp.lang.c


On Tue, 25 Mar 2008 01:20:44 -0000, Peter Nilsson <airia@xxxxxxxxxxx> wrote:
What about aggregates thereof?

The structure contains, in order, an 8-bit char, two sixteen-bit unsigned ints, and an 8-bit char. No aggregates.

You are right to be concerned [about padding], but in thisspecific case, all bits zero must represent zero for all
integral types in both C90 and C99.

True, but what about the padding areas?


This can be simplified to a single memset(acts, 0, ...).

But if you want robustness against one day having float or
pointer types, then you can do something like...

  void clear_actions(ACTION act[], size_t N)
  {
    static const ACTION zero;
    size_t i;
    for (i = 0; i < N; i++)
      act[i] = zero;
  }

I like that, thanks.

--
Martin


<Prev in Thread] Current Thread [Next in Thread>
Privacy Policy