[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.c

Are struct members are also padded?

Tech ID

6/12/2011 9:09:00 PM

struct pad1 {
char c1;

};

struct pad2 {
char c1;
int x;

};

struct pad3 {
char c1;
char c2;
int x;

};

struct pad4 {
char c1;
char c2;
int x;
struct pad1 {
char c1;
};

};

What will be the sizeof for the structs above? (for 32-bit and 64-bit
machines).
5 Answers

Ian Collins

6/12/2011 9:13:00 PM

0

On 06/13/11 09:09 AM, Tech ID wrote:
> struct pad1 {
> char c1;
>
> };
>
> struct pad2 {
> char c1;
> int x;
>
> };
>
> struct pad3 {
> char c1;
> char c2;
> int x;
>
> };
>
> struct pad4 {
> char c1;
> char c2;
> int x;
> struct pad1 {
> char c1;
> };
>
> };
>
> What will be the sizeof for the structs above? (for 32-bit and 64-bit
> machines).

That depends on the alignment rules and sizeof(int) for that machine.
There isn't a universal 32 or 64 bit machine.

--
Ian Collins

Shao Miller

6/12/2011 9:14:00 PM

0

On 6/12/2011 4:09 PM, Tech ID wrote:
> struct pad1 {
> char c1;
>
> };
>
> struct pad2 {
> char c1;
> int x;
>
> };
>
> struct pad3 {
> char c1;
> char c2;
> int x;
>
> };
>
> struct pad4 {
> char c1;
> char c2;
> int x;
> struct pad1 {
> char c1;
> };
>
> };
>
> What will be the sizeof for the structs above? (for 32-bit and 64-bit
> machines).

The sizes are implementation-defined. The answer to the subject-line's
question is implementation-defined.

Angel

6/12/2011 9:27:00 PM

0

On 2011-06-12, Tech ID <tech@spamtrap.invalid> wrote:
> struct pad1 {
> char c1;
>
> };
>
> struct pad2 {
> char c1;
> int x;
>
> };
>
> struct pad3 {
> char c1;
> char c2;
> int x;
>
> };
>
> struct pad4 {
> char c1;
> char c2;
> int x;
> struct pad1 {
> char c1;
> };
>
> };
>
> What will be the sizeof for the structs above? (for 32-bit and 64-bit
> machines).

As given, the definition of struct pad4 is incorrect, as it redefines
struct pad1. With that corrected, the sizes are as follows on the
systems I tested on:

x86_64, 64-bit Linux:
1, 8, 8, 12

sparc64, 64-bit Linux with 32-bit user land:
1, 8, 8, 12


As others pointed out though, there is no standard for this and the
sizes will likely vary on different hardware and operating systems.


--
"C provides a programmer with more than enough rope to hang himself.
C++ provides a firing squad, blindfold and last cigarette."
- seen in comp.lang.c

Stephen Sprunk

6/12/2011 9:32:00 PM

0

On 12-Jun-11 16:09, Tech ID wrote:
> struct pad1 {
> char c1;
> };
>
> struct pad2 {
> char c1;
> int x;
> };
>
> struct pad3 {
> char c1;
> char c2;
> int x;
> };
>
> struct pad4 {
> char c1;
> char c2;
> int x;
> struct pad1 {
> char c1;
> };
> };
>
> What will be the sizeof for the structs above? (for 32-bit and 64-bit
> machines).

The sizes will vary from one implementation to another, sometimes even
on the same machine, so there are no universal answers.

If you need to worry about this, it would be more effective to address
the portability problems with your code so that the size is irrelevant,
rather than try to predict what machines it will run on and figure out
how to handle each case.

S

--
Stephen Sprunk "God does not play dice." --Albert Einstein
CCIE #3723 "God is an inveterate gambler, and He throws the
K5SSS dice at every possible opportunity." --Stephen Hawking

Keith Thompson

6/12/2011 9:39:00 PM

0

Tech ID <tech@spamtrap.invalid> writes:
> struct pad1 {
> char c1;
>
> };
>
> struct pad2 {
> char c1;
> int x;
>
> };
>
> struct pad3 {
> char c1;
> char c2;
> int x;
>
> };
>
> struct pad4 {
> char c1;
> char c2;
> int x;
> struct pad1 {
> char c1;
> };

This should be something like:

struct pad4 {
char c1;
char c2;
int x;
struct pad1 p1;
};

> What will be the sizeof for the structs above? (for 32-bit and 64-bit
> machines).

It's not clear to me how the question in your subject header and
the question in the body of your article are related.

The best (in some sense) answer to your second question (once
you correct the error in the declaration of struct pad4) is:
sizeof (struct pad1), sizeof (struct pad2), sizeof (struct pad3),
and sizeof (struct pad4), respectively. I know that's not the answer
you were looking for, but in most contexts it's all you need to know.

As for the first question, "Are struct members ... also padded?",
what exactly do you mean by that?

A struct has a size and layout determined by the compiler.
The compiler may add padding after any member, including the last
one. This is normally done to satisfy alignment requirements,
but in theory a compiler could add padding for any reason.

Your struct pad4 has a member of type struct pad1, which I've
named p1. p1 has whatever size struct pad1 has, which will include
any padding after c1. There may or may not be additional padding
after p1; this padding would be part of struct pad4.

Does that answer your questions?

--
Keith Thompson (The_Other_Keith) kst-u@mib.org <http://www.ghoti.ne...
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"