By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,687 Members | 2,046 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,687 IT Pros & Developers. It's quick & easy.

Clearing Padding in Structure

P: n/a
Is clearing a structure the following way well defined in C89? The
structure ACTION contains no floating point or pointer members. Only
integral types. My thoughts concern the padding - can and should it be
altered?

typedef unsigned char Byte;
#define MAX_ACTIONS 10

/* ... */

void clear_actions(ACTION *acts)
{
Byte *pByte = (Byte *)acts;
Byte *limit;

limit = pByte + sizeof(ACTION) * MAX_ACTIONS; /* 1 beyond array*/

for ( ; pByte < limit; pByte++)
*pByte = 0;

} /* clear_actions */
--
Martin

Mar 24 '08 #1
Share this Question
Share on Google+
3 Replies


P: n/a
Martin <martindotunderscoreobr...@which.tnetwrote:
Is clearing a structure the following way well defined
in C89? The structure ACTION contains no floating point
or pointer members.
What about aggregates thereof?
Only integral types. My thoughts concern the padding -
can and should it be altered?
You are right to be concerned, but in this specific case,
all bits zero must represent zero for all integral types
in both C90 and C99.
* * typedef * unsigned char * Byte;
* * #define * MAX_ACTIONS * * 10

* * /* ... */

* * void clear_actions(ACTION *acts)
* * {
* * * *Byte *pByte = (Byte *)acts;
* * * *Byte *limit;

* * * *limit = pByte + sizeof(ACTION) * MAX_ACTIONS;
/* 1 beyond array */

* * * *for ( ; pByte < limit; pByte++)
* * * * * *pByte = 0;

* * } /* clear_actions */
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;
}

--
Peter
Mar 25 '08 #2

P: n/a
On Tue, 25 Mar 2008 00:18:27 -0000, user923005 <dc*****@connx.comwrote:
I guess that your function is exactly equivalent to:
memset(&mystruct, 0, sizeof mystruct);
Not quite, my function is looping through the address space occupied by an
array of structures of the same type, and clearing it byte by byte.

--
Martin

Mar 26 '08 #3

P: n/a
On Sat, 29 Mar 2008 03:45:19 -0000, pete <pf*****@mindspring.comwrote:
That's a very good question.
What about them?
Why are you clearing the structure?
The structures need to be cleared occasionally. Upthread you will see I
said:

"My concern was about the validity of clearing out the padding areas of
the structures."

My original post shows that the function clear_actions() walks through the
entire storage area of the array of structures and sets each byte to zero.
My question regarding the padding areas was referring to the validity of
changing those. The code posted by Peter Nilsson avoids that problem.

--
Martin

Mar 29 '08 #4

This discussion thread is closed

Replies have been disabled for this discussion.