size of a typedef with arrays of unsigned char

Discussion in 'C Programming' started by Francois Grieu, Dec 19, 2006.

  1. Hello,

    is the size of a type, defined using typedef as a collection of
    arrays of unsigned char, the sum of the size of the arrays ?

    After simplifying my question, it probably is equivalent to:
    does the following program terminates ?

    int main(void) {
    typedef struct t { unsigned char f[1]; } t;
    for(;1!=sizeof t;);
    return 0; }


    TIA,

    Francois Grieu
     
    Francois Grieu, Dec 19, 2006
    #1
    1. Advertising

  2. Francois Grieu

    pete Guest

    Francois Grieu wrote:
    >
    > Hello,
    >
    > is the size of a type, defined using typedef as a collection of
    > arrays of unsigned char, the sum of the size of the arrays ?
    >
    > After simplifying my question, it probably is equivalent to:
    > does the following program terminates ?


    The code example depends on a property of structures
    called padding bytes.
    sizeof t could be equal to 1,
    or sizeof t could be larger than 1.

    > int main(void) {
    > typedef struct t { unsigned char f[1]; } t;
    > for(;1!=sizeof t;);
    > return 0; }


    --
    pete
     
    pete, Dec 19, 2006
    #2
    1. Advertising

  3. Francois Grieu

    iamgodk Guest

    in fact, the size is affected by the different compiler.

    "This is by no means guaranteed"
    refer to Martin Ambuhl <>
    :)

    ÓÚ Tue, 19 Dec 2006 09:42:21 +0100£¬Francois Grieuдµ½£º

    > Hello,
    >
    > is the size of a type, defined using typedef as a collection of
    > arrays of unsigned char, the sum of the size of the arrays ?
    >
    > After simplifying my question, it probably is equivalent to:
    > does the following program terminates ?
    >
    > int main(void) {
    > typedef struct t { unsigned char f[1]; } t;
    > for(;1!=sizeof t;);
    > return 0; }
    >
    >
    > TIA,
    >
    > Francois Grieu
     
    iamgodk, Dec 19, 2006
    #3
  4. Francois Grieu

    iamgodk Guest

    ÓÚ Tue, 19 Dec 2006 08:55:20 +0000£¬peteдµ½£º

    > Francois Grieu wrote:
    >>
    >> Hello,
    >>
    >> is the size of a type, defined using typedef as a collection of
    >> arrays of unsigned char, the sum of the size of the arrays ?
    >>
    >> After simplifying my question, it probably is equivalent to:
    >> does the following program terminates ?

    >
    > The code example depends on a property of structures
    > called padding bytes.
    > sizeof t could be equal to 1,
    > or sizeof t could be larger than 1.
    >
    >> int main(void) {
    >> typedef struct t { unsigned char f[1]; } t;
    >> for(;1!=sizeof t;);
    >> return 0; }

    sometimes the number of the padding bytes can be set by compiler parameter
     
    iamgodk, Dec 19, 2006
    #4
  5. Francois Grieu

    Richard Bos Guest

    Francois Grieu <> wrote:

    > is the size of a type, defined using typedef as a collection of
    > arrays of unsigned char, the sum of the size of the arrays ?


    Not necessarily.

    > After simplifying my question, it probably is equivalent to:
    > does the following program terminates ?
    >
    > int main(void) {
    > typedef struct t { unsigned char f[1]; } t;
    > for(;1!=sizeof t;);
    > return 0; }


    What horrible style. Whitespace is nearly free, you know.

    But no, that may, and often will, but need not, terminate.

    Richard
     
    Richard Bos, Dec 19, 2006
    #5
  6. Francois Grieu

    Guest

    Francois Grieu wrote:
    > Hello,
    >
    > is the size of a type, defined using typedef as a collection of
    > arrays of unsigned char, the sum of the size of the arrays ?


    The reference to typedef is irrelevant. You are actually interested in
    the size of structs.

    The answer to your question is no. This may be so in some cases, but in
    general, padding may be added to make the struct "conveniently" sized.

    For example :-

    struct s {
    int a;
    char b[2];
    long c;
    };

    On a system with 8-bit chars, 32-bit ints and 64-bit longs, the
    structure could have sizeof = 16, as 2 bytes padding would be inserted
    (after the char array b) to align the long on an 8-byte boundary.

    On the machines I most commonly work with, all structs are aligned at
    8-byte boundarys, so even a struct containing a single "char" would
    have sizeof = 8.
     
    , Dec 19, 2006
    #6
  7. In article <>,
    wrote:

    > Francois Grieu wrote:


    > > is the size of a type, defined using typedef as a collection of
    > > arrays of unsigned char, the sum of the size of the arrays ?

    >
    > The reference to typedef is irrelevant. You are actually interested in
    > the size of structs.


    Right.


    > The answer to your question is no.


    OK, thanks.

    Now, if I can't use a struct for the purpose of naming fields
    in a record consisting of arbitrary byte-sized chunks, does the
    following work for this purpose (in particular, is is certain
    that sizeof(vMyRec) is 7*15), and can anyone suggest a better
    idiom ?


    enum
    {
    myRecSize = 15,
    fField2 = myRecSize-7, // fField2, 7 bytes,
    fField1 = fField2-5, // fField1, 5 bytes,
    fField0 = fField1-3, // fField0, 3 bytes,
    myRecSizeOK = 1/(fField0==0) // check that things add up
    };
    typedef unsigned char tMyRec[myRecSize];

    tMyRec vMyRec[7]; // memory image of file consisting of 7 records

    #include <stdio.h>
    int main(void)
    {
    FILE *f;
    if ((f = fopen("testfile","rb"))!=NULL
    && fread(vMyRec,sizeof(*vMyRec),sizeof(vMyRec)/sizeof(*vMyRec),f)==
    sizeof(vMyRec)/sizeof(*vMyRec))
    printf("Fourth byte of second field of seventh record is 0x%02X\n",
    vMyRec[6][fField1+3]);
    return 0;
    }


    TIA,

    Francois Grieu
     
    Francois Grieu, Dec 19, 2006
    #7
  8. Francois Grieu

    Guest

    Francois Grieu wrote:
    > In article <>,
    > wrote:
    >
    > > Francois Grieu wrote:

    >
    > > > is the size of a type, defined using typedef as a collection of
    > > > arrays of unsigned char, the sum of the size of the arrays ?

    > >
    > > The reference to typedef is irrelevant. You are actually interested in
    > > the size of structs.

    >
    > Right.
    >
    >
    > > The answer to your question is no.

    >
    > OK, thanks.
    >
    > Now, if I can't use a struct for the purpose of naming fields
    > in a record consisting of arbitrary byte-sized chunks, does the
    > following work for this purpose (in particular, is is certain
    > that sizeof(vMyRec) is 7*15), and can anyone suggest a better
    > idiom ?

    FAQ 2.11 is what you want, I think - http://c-faq.com/struct/io.html
     
    , Dec 21, 2006
    #8
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Steffen Fiksdal

    void*, char*, unsigned char*, signed char*

    Steffen Fiksdal, May 8, 2005, in forum: C Programming
    Replies:
    1
    Views:
    589
    Jack Klein
    May 9, 2005
  2. Ioannis Vranos
    Replies:
    11
    Views:
    763
    Ioannis Vranos
    Mar 28, 2008
  3. Ioannis Vranos

    Padding bits and char, unsigned char, signed char

    Ioannis Vranos, Mar 28, 2008, in forum: C Programming
    Replies:
    6
    Views:
    616
    Ben Bacarisse
    Mar 29, 2008
  4. Alex Vinokur
    Replies:
    9
    Views:
    799
    James Kanze
    Oct 13, 2008
  5. pozz
    Replies:
    12
    Views:
    745
    Tim Rentsch
    Mar 20, 2011
Loading...

Share This Page