Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c > #5917 > unrolled thread

Are struct members are also padded?

Started byTech ID <tech@spamtrap.invalid>
First post2011-06-12 21:09 +0000
Last post2011-06-12 14:39 -0700
Articles 6 — 6 participants

Back to article view | Back to comp.lang.c


Contents

  Are struct members are also padded? Tech ID <tech@spamtrap.invalid> - 2011-06-12 21:09 +0000
    Re: Are struct members are also padded? Ian Collins <ian-news@hotmail.com> - 2011-06-13 09:13 +1200
    Re: Are struct members are also padded? Shao Miller <sha0.miller@gmail.com> - 2011-06-12 16:13 -0500
    Re: Are struct members are also padded? Angel <angel+news@spamcop.net> - 2011-06-12 21:26 +0000
    Re: Are struct members are also padded? Stephen Sprunk <stephen@sprunk.org> - 2011-06-12 16:32 -0500
    Re: Are struct members are also padded? Keith Thompson <kst-u@mib.org> - 2011-06-12 14:39 -0700

#5917 — Are struct members are also padded?

FromTech ID <tech@spamtrap.invalid>
Date2011-06-12 21:09 +0000
SubjectAre struct members are also padded?
Message-ID<it39te$if3$1@speranza.aioe.org>
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). 

[toc] | [next] | [standalone]


#5919

FromIan Collins <ian-news@hotmail.com>
Date2011-06-13 09:13 +1200
Message-ID<95kofbFrhmU2@mid.individual.net>
In reply to#5917
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

[toc] | [prev] | [next] | [standalone]


#5920

FromShao Miller <sha0.miller@gmail.com>
Date2011-06-12 16:13 -0500
Message-ID<it3a65$9f8$2@dont-email.me>
In reply to#5917
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.

[toc] | [prev] | [next] | [standalone]


#5922

FromAngel <angel+news@spamcop.net>
Date2011-06-12 21:26 +0000
Message-ID<slrnivaboe.3nn.angel+news@pearlgates.net>
In reply to#5917
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

[toc] | [prev] | [next] | [standalone]


#5924

FromStephen Sprunk <stephen@sprunk.org>
Date2011-06-12 16:32 -0500
Message-ID<it3b9b$ha1$1@dont-email.me>
In reply to#5917
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

[toc] | [prev] | [next] | [standalone]


#5925

FromKeith Thompson <kst-u@mib.org>
Date2011-06-12 14:39 -0700
Message-ID<lnk4cq7mr8.fsf@nuthaus.mib.org>
In reply to#5917
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.net/~kst>
Nokia
"We must do something.  This is something.  Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.c


csiph-web