Size of Structure

Discussion in 'C Programming' started by MSReddy, May 17, 2004.

  1. MSReddy

    MSReddy Guest

    Hi Group,

    I have a structure than has other structures in it. I added 3 members
    to a strucuture and my program started behaving very strangly. When I
    printed sizes of the strucutures the values were very strange and
    unexpected. Here is an example. Applogies for not being able to post
    the exact code.

    Struct B{
    long b1;
    short b2;
    }

    Struct C{
    long c1;
    char c2;
    char *cptr;
    }

    Struct A{
    long a1;
    B sb;
    long a2;
    C sc;
    short a3;
    char *a4;
    }

    Now changes struct C to,
    Struct C{
    long c1;
    char c2;
    char c3;
    char c4;
    bool c5;
    char *cptr;
    }


    Then I printed the sizes of my strucutures using sizeof(). These are
    actual values from my program.

    Before Modification:
    Size of Struct A: 814552
    Size of Struct C: 8452

    After Modification,
    Size of Struct A: 815064
    Size of Struct C: 8460

    Those values obviously looks spurious. I could not access the members
    of Struct A correctly. All the members of Struct A declared after
    Struct C (in this case a3 and *a4) were off by 256 bytes. I ran this
    on two computers just to make sure it is not some strage problem with
    machine and got same results.

    How can I solve this. Any help is highly appreciated. Thank you very
    much.

    Reddy
     
    MSReddy, May 17, 2004
    #1
    1. Advertising

  2. "MSReddy" <> a écrit dans le message de
    news:...
    > Hi Group,


    Hi,

    > I have a structure than has other structures in it. I added 3 members
    > to a strucuture and my program started behaving very strangly. When I
    > printed sizes of the strucutures the values were very strange and
    > unexpected. Here is an example. Applogies for not being able to post
    > the exact code.
    >
    > Struct B{
    > long b1;
    > short b2;
    > }


    Missing ;

    >
    > Struct C{
    > long c1;
    > char c2;
    > char *cptr;
    > }


    Missing ;
    >
    > Struct A{
    > long a1;
    > B sb;
    > long a2;
    > C sc;
    > short a3;
    > char *a4;
    > }


    Missing ;

    > Now changes struct C to,
    > Struct C{
    > long c1;
    > char c2;
    > char c3;
    > char c4;
    > bool c5;
    > char *cptr;
    > }
    >


    Missing ;

    Regis
     
    Régis Troadec, May 17, 2004
    #2
    1. Advertising

  3. "MSReddy" <> a écrit dans le message de
    news:...
    > Hi Group,


    Hi again,

    I missed other points,

    > Struct B{


    the keyword is struct, not Struct

    > long b1;
    > short b2;
    > }

    };

    [snipped]

    > Struct A{

    struct A {

    > long a1;
    > B sb;


    B isn't a type or some aliased one, tou must specify that B stands for a
    structure:
    struct B sb;

    > long a2;
    > C sc;


    Same as above:
    struct C sc;

    > short a3;
    > char *a4;
    > }

    };

    [snipped]

    Running this:
    #include <stdio.h>

    struct B{
    long b1;
    short b2;
    };

    struct C{
    long c1;
    char c2;
    char *cptr;
    };

    struct A{
    long a1;
    struct B sb;
    long a2;
    struct C sc;
    short a3;
    char *a4;
    };

    int main(void)
    {
    printf("%u\n",(unsigned)sizeof(struct A));
    printf("%u\n",(unsigned)sizeof(struct B));
    printf("%u\n",(unsigned)sizeof(struct C));
    return 0;
    }

    ....I get respectively 36, 8 and 12 with my implementation.

    Regis
     
    Régis Troadec, May 17, 2004
    #3
  4. >Struct C{
    > long c1;
    > char c2;
    > char *cptr;
    >}

    ....

    >Now changes struct C to,
    >Struct C{
    > long c1;
    > char c2;
    > char c3;
    > char c4;
    > bool c5;
    > char *cptr;
    >}
    >
    >
    >Then I printed the sizes of my strucutures using sizeof(). These are
    >actual values from my program.
    >
    >Before Modification:
    >Size of Struct A: 814552
    >Size of Struct C: 8452


    How did you obtain the size of struct C? I find it very difficult
    to believe that a struct containing a long, 3 chars, a bool (whatever that is)
    and a pointer has a length anywhere near 8452 bytes.

    Show the code that printed the above output.

    Gordon L. Burditt
     
    Gordon Burditt, May 17, 2004
    #4
  5. MSReddy

    Ralmin Guest

    "MSReddy" <> wrote:
    > Hi Group,
    >
    > I have a structure than has other structures in it. I added 3
    > members to a strucuture and my program started behaving very
    > strangly. When I printed sizes of the strucutures the values
    > were very strange and unexpected. Here is an example. Applogies
    > for not being able to post the exact code.


    If you can't post the exact code, at least post code that compiles!

    > Struct B{


    There is no capital on the struct keyword.

    > long b1;
    > short b2;
    > }


    Missing semicolon.

    [...]
    > B sb;


    Undefined type. You must include the struct keyword to reference a struct
    tag in C.
    struct B sb;

    [...]
    > bool c5;


    Undefined type. You must include the <stdbool.h> header to use the bool type
    on C99. It is not available at all on C89.

    When I fixed up your code, I got entirely reasonable values for the struct
    sizes on three different compilers.

    #include <stdio.h>

    typedef int bool;

    struct B {
    long b1;
    short b2;
    };

    struct C {
    long c1;
    char c2;
    char *cptr;
    };

    struct A {
    long a1;
    struct B sb;
    long a2;
    struct C sc;
    short a3;
    char *a4;
    };

    struct C2 {
    long c1;
    char c2;
    char c3;
    char c4;
    bool c5;
    char *cptr;
    };

    int main(void)
    {
    printf("sizeof (struct B) == %lu\n", (long unsigned)sizeof (struct B));
    printf("sizeof (struct C) == %lu\n", (long unsigned)sizeof (struct C));
    printf("sizeof (struct A) == %lu\n", (long unsigned)sizeof (struct A));
    printf("sizeof (struct C2) == %lu\n", (long unsigned)sizeof (struct C2));
    return 0;
    }

    --
    Simon.
     
    Ralmin, May 18, 2004
    #5
  6. MSReddy

    MSReddy Guest

    "Ralmin" <> wrote in message news:<>...
    > "MSReddy" <> wrote:
    > > Hi Group,
    > >
    > > I have a structure than has other structures in it. I added 3
    > > members to a strucuture and my program started behaving very
    > > strangly. When I printed sizes of the strucutures the values
    > > were very strange and unexpected. Here is an example. Applogies
    > > for not being able to post the exact code.

    >
    > If you can't post the exact code, at least post code that compiles!
    >
    > > Struct B{

    >
    > There is no capital on the struct keyword.
    >
    > > long b1;
    > > short b2;
    > > }

    >
    > Missing semicolon.
    >
    > [...]
    > > B sb;

    >
    > Undefined type. You must include the struct keyword to reference a struct
    > tag in C.
    > struct B sb;
    >
    > [...]
    > > bool c5;

    >
    > Undefined type. You must include the <stdbool.h> header to use the bool type
    > on C99. It is not available at all on C89.
    >
    > When I fixed up your code, I got entirely reasonable values for the struct
    > sizes on three different compilers.
    >
    > #include <stdio.h>
    >
    > typedef int bool;
    >
    > struct B {
    > long b1;
    > short b2;
    > };
    >
    > struct C {
    > long c1;
    > char c2;
    > char *cptr;
    > };
    >
    > struct A {
    > long a1;
    > struct B sb;
    > long a2;
    > struct C sc;
    > short a3;
    > char *a4;
    > };
    >
    > struct C2 {
    > long c1;
    > char c2;
    > char c3;
    > char c4;
    > bool c5;
    > char *cptr;
    > };
    >
    > int main(void)
    > {
    > printf("sizeof (struct B) == %lu\n", (long unsigned)sizeof (struct B));
    > printf("sizeof (struct C) == %lu\n", (long unsigned)sizeof (struct C));
    > printf("sizeof (struct A) == %lu\n", (long unsigned)sizeof (struct A));
    > printf("sizeof (struct C2) == %lu\n", (long unsigned)sizeof (struct C2));
    > return 0;
    > }


    Appologies for all the typos. As I said, the code I gave is just an
    example. My structures are big . So the sizes that I showed are
    different from sizes of struct A, B and C. Unfortunately, I wouldnt be
    able to post my structures, so I made up these structures (A,B and C)
    just give an example of what I am talking about.
     
    MSReddy, May 18, 2004
    #6
  7. MSReddy

    Ralmin Guest

    "MSReddy" <> wrote:
    > "Ralmin" <> wrote:
    > > "MSReddy" <> wrote:

    [...]
    > Appologies for all the typos. As I said, the code I gave is
    > just an example. My structures are big . So the sizes that
    > I showed are different from sizes of struct A, B and C.
    > Unfortunately, I wouldnt be able to post my structures, so
    > I made up these structures (A,B and C) just give an example
    > of what I am talking about.


    Do the structures you gave (A, B and C) exhibit the problem? If not, they
    are not good examples. You should try to cut down the structures and the
    program, until you get to the smallest possible amount of code that still
    has the problem, then you should have a clearer view of what the problem
    could be caused by. Once you have done that, if you still don't know, then
    you can post the cut down code.

    Here's all I can say at the moment:

    The increase of eight bytes in struct C is as expected; the bool is of four
    bytes, and requires alignment to a four-byte boundary. So, there are two
    extra chars, two bytes of padding, then the bool which takes four bytes.

    Your problem is the increase of 512 bytes in the size of struct A? And you
    say that offsetof revealed that the increase is padding after the sc member
    and before the a3 member?

    I don't have an explanation of that... perhaps you need to provide more code
    and/or more explanation.

    --
    Simon.
     
    Ralmin, May 18, 2004
    #7
    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. Excluded_Middle

    Pointers to structure and array of structure.

    Excluded_Middle, Oct 24, 2004, in forum: C Programming
    Replies:
    4
    Views:
    760
    Martin Ambuhl
    Oct 26, 2004
  2. Leo Nunez
    Replies:
    3
    Views:
    1,231
    Neil Kurzman
    Feb 9, 2005
  3. Replies:
    2
    Views:
    613
  4. Kislay

    Size of a structure : Structure Padding

    Kislay, Oct 1, 2007, in forum: C Programming
    Replies:
    15
    Views:
    963
    clinuxpro
    Jul 13, 2011
  5. Jason Cavett

    Preferred Size, Minimum Size, Size

    Jason Cavett, May 23, 2008, in forum: Java
    Replies:
    5
    Views:
    12,586
    Michael Jung
    May 25, 2008
Loading...

Share This Page