473,785 Members | 2,391 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Clearing Padding in Structure

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(A CTION *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
3 1772
Martin <martindotunder scoreobr...@whi ch.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(A CTION *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(A CTION 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
On Tue, 25 Mar 2008 00:18:27 -0000, user923005 <dc*****@connx. comwrote:
I guess that your function is exactly equivalent to:
memset(&mystruc t, 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
On Sat, 29 Mar 2008 03:45:19 -0000, pete <pf*****@mindsp ring.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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

13
3894
by: Amarendra | last post by:
Folks, This structure padding issue is bothering me now, could not locate a satisfactory answer on clc, so here it goes... I have a structure, given below: typedef struct { int flag; char keys; char padding;
28
20803
by: ramu | last post by:
Hi all, I understand that some compilers pad some bytes to the aggregate data types in order to access the members of the aggregate data types(i.e. structure) fast. This depends on the architectures. Some architectures cannot access the data which will be stored on the odd addresses or they may find difficult to access it. This is the reason for padding extra bytes. Now consider the following structure:
20
2323
by: Lalatendu Das | last post by:
hi let's say i have a structure struct test { int A; char B; int C; }; this above structure defination always going to take 16 byte in memeory in whatever manner we align the member variables while declaring a variable to it . because variable 'A' going to take 4 byte then four charachter of
1
4160
by: pmm | last post by:
hi I am repeating my post here plz excuse i am trying out a UDP packet transfer between a windows machine and a linux I created a structure on both sides (ie on linux and on windows) and I sent using UDP but on the receiving side when I am trying to reform the packet back into structure tis giving me different outputs I assume it is the problem of structure padding I even used pragma pack (1) but still it is not working out for me...
24
2127
by: karthikbalaguru | last post by:
Hi, I find that the structure padding is not being taken into account while using 'new' operator. Is there a way to enable it ? struct Dataunit { char dataid; int keyid;
10
3293
by: Rohit kumar Chandel | last post by:
Hi All, Please let me know how to find in a structure whether compiler has used padding or not. Regards Rohit
12
4807
by: Kislay | last post by:
case 1 : struct s { char c1; // 8 double d; // 8 int i1; // 4 char c2; // 4 int i2; // 4 };
3
3542
by: vikas talwar | last post by:
Hi All, Can you please explain me how the 'C' compiler allocate memory to 'struct'. Please go thu the example below and pls suggest me the solution for my problem. Here is my structure definition struct my_dev {
0
9480
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10148
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9950
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8972
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7499
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5381
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5511
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4053
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3646
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.