473,497 Members | 2,124 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

struct padding ???

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 {
char abc;
} ;

and now when I do
printf("size=%d\n", sizeof(struct my_dev));
output is: 4

But I expect the size of struct = '1'.

Is there any trick to get the struct to occupy memory with pad of 1
byte and not 4 bytes..
--
Thanks
Vikas
May 31 '08 #1
3 3511
vikas talwar <vi**********@gmail.comwrote:
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 {
char abc;
} ;
and now when I do
printf("size=%d\n", sizeof(struct my_dev));
output is: 4
But I expect the size of struct = '1'.
Is there any trick to get the struct to occupy memory with pad of 1
byte and not 4 bytes..
The compiler is obviously putting in 3 padding bytes after the
'abc' element of the structure. It does that to make sure when
you create an array of such structures all the structures of
the array end up on correctly aligned memory positions. If
it wouldn't do that something simple as

my_dev xxx[ 2 ];

xxx.abc = 'c';

could lead to a crash of the program, at least on your machine
(on mine the sizeof for the structure is repoted as 1, so on
my machine it doesn't seem necessary to insert padding bytes).
For the same reason also padding bytes can appear between the
elements of a structure.

There is no C standard compliant way to elimintae padding bytes.
Some compilers have some extra options to avoid the addition of
padding bytes, for e.g. gcc you would have to add

__attribute__((packed))

directly after the structure declaration) but, unless you know
exactky what you're doing and the trouble you can get into with
that I can only recommend not to use it. Perhaps you should
tell what exactly you want to achieve that would require the
elimination of padding bytes and perhaps there's a better way
to do it.
Regards, Jens
--
\ Jens Thoms Toerring ___ jt@toerring.de
\__________________________ http://toerring.de
May 31 '08 #2
Jens Thoms Toerring <jt@toerring.dewrote:
....it wouldn't do that something simple as
my_dev xxx[ 2 ];
xxx.abc = 'c';
Sorry, I meant

xxx[1].abc = 'c'

here, the original version would be a syntax error...

Regards, Jens
--
\ Jens Thoms Toerring ___ jt@toerring.de
\__________________________ http://toerring.de
May 31 '08 #3
jt@toerring.de (Jens Thoms Toerring) writes:
vikas talwar <vi**********@gmail.comwrote:
>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 {
char abc;
} ;
>and now when I do
printf("size=%d\n", sizeof(struct my_dev));
output is: 4
>But I expect the size of struct = '1'.
>Is there any trick to get the struct to occupy memory with pad of 1
byte and not 4 bytes..
See questions 2.12 and 2.13 in the comp.lang.c FAQ,
<http://www.c-faq.com/>.
The compiler is obviously putting in 3 padding bytes after the
'abc' element of the structure. It does that to make sure when
you create an array of such structures all the structures of
the array end up on correctly aligned memory positions. If
it wouldn't do that something simple as

my_dev xxx[ 2 ];

xxx.abc = 'c';

could lead to a crash of the program, at least on your machine
(on mine the sizeof for the structure is repoted as 1, so on
my machine it doesn't seem necessary to insert padding bytes).
For the same reason also padding bytes can appear between the
elements of a structure.
Actually, I can't think of any good reason why the compiler *needs* to
pad "struct my_dev" to 4 bytes. It *could* internally treat a
structure with a single member the same way it would treat an object
of the member type.

There is a requirement for all pointers to structures to have the same
representation; on an implementation where byte pointers and word
pointers are represented differently, that could force 4-byte
alignment for all structures, even small ones. But most systems use
the same representation for all pointers.

Probably the compiler's developers decided that it would be simpler to
force all structures to be word-aligned (where a "word" is probably 4
bytes). They're allowed to do that. The standard allows padding
after members of structures; it doesn't require that padding to be the
minimum necessary. A perverse implementation could add as much
padding as it likes, for no good reason at all.

[...]

For your particular problem, why do you need a structure at all?
Rather than
struct my_dev {
char abc;
};
why not just use type char directly (with a typedef if you like)?
(Using a structure does have the advantage of type safety; "struct
my_dev" is incompatible with other types. But then you have to pay
the price of letting the compiler add extra padding.)

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
May 31 '08 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
17556
by: Roy Hills | last post by:
When I'm reading from or writing to a network socket, I want to use a struct to represent the structured data, but must use an unsigned char buffer for the call to sendto() or recvfrom(). I have...
5
2370
by: Kobu | last post by:
In embedded systems (programmed in C), often times structure declarations are used to group together several status/control/data registers of external hardware (or even internal registers). The...
21
2408
by: hermes_917 | last post by:
I want to use memcpy to copy the contents of one struct to another which is a superset of the original struct (the second struct has extra members at the end). I wrote a small program to test...
8
1667
by: Mike | last post by:
The following struct, DataStruct, is only part of a larger one that contains additional fields and arrays. I need the explicit layout because this struct is really a union, where some of the...
6
1927
by: Christian Kandeler | last post by:
Hi, I'd like to know whether the following code is standard-compliant: struct generic { int x; int a; };
7
1530
by: venkatbo | last post by:
Hi folks, Of TurboGers & Django WAF candidates, which one would be easier to use in an environment where the data/content doesn't come an RDBMS, but from other server-side apps... If these are...
2
2270
by: Michael Yanowitz | last post by:
Hello: I am relatively new to Python and this is my first post on this mailing list. I am confused as to why I am getting size differences in the following cases: >>> print...
5
4014
by: Hallvard B Furuseth | last post by:
Does struct assignment copy padding bytes? Some compilers do, but I couldn't find anything in the standard which says they must. What I need is for any padding bytes to contan initialized values...
11
3611
by: simonp | last post by:
I'm taking an intro course on C++, and our teacher is not being clear on how stuct memory padding is determined. If the memory used by all components defined inside a struct falls between a...
0
7120
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
6991
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...
0
7160
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7196
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...
0
7373
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...
0
5456
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,...
0
3088
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...
0
3078
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1405
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 ...

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.