Are struct members are also padded?

T

Tech ID

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).
 
I

Ian Collins

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.
 
S

Shao Miller

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.
 
A

Angel

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.
 
S

Stephen Sprunk

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
 
K

Keith Thompson

Tech ID said:
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?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,754
Messages
2,569,526
Members
44,997
Latest member
mileyka

Latest Threads

Top