obtaining the size of a structure

Discussion in 'C Programming' started by kris, Aug 1, 2007.

  1. kris

    kris Guest

    Hi I am writing a small program where I need to obtain the actual size
    of a structure.
    The programm is as follows

    struct abc
    {
    int j;
    char k;
    int i;
    }*a;

    main()
    {
    a->j=10;

    printf("size of structure is %d",sizeof(a));
    }

    In this program when I print the size of structure I get the whole
    size (i.e size of 2 int variables+size of char variable).

    But I need to get the size of structure such that it resembles the
    chunk of memory that is actually being used.

    Thanks
    Krish
    kris, Aug 1, 2007
    #1
    1. Advertising

  2. "kris" <> wrote in message
    news:...
    >
    > Hi I am writing a small program where I need to obtain the actual size
    > of a structure.
    > The programm is as follows
    >
    > struct abc
    > {
    > int j;
    > char k;
    > int i;
    > }*a;
    >
    > main()
    > {
    > a->j=10;
    >
    > printf("size of structure is %d",sizeof(a));


    This is the size of a pointer to the structure.
    'a' is of type 'struct abc *'

    > }
    >
    > In this program when I print the size of structure I get the whole
    > size (i.e size of 2 int variables+size of char variable).
    >
    > But I need to get the size of structure such that it resembles the
    > chunk of memory that is actually being used.
    >


    try sizeof(*a)

    Why do you need this?

    Note that the actual size of this
    structure is implementation dependent; the compiler may
    add padding between j and k, and between k and i, and
    even after i. Different compilers may add different
    amounts of padding.
    --
    Fred L. Kleinschmidt
    Fred Kleinschmidt, Aug 1, 2007
    #2
    1. Advertising

  3. kris

    jacob navia Guest

    kris wrote:
    > Hi I am writing a small program where I need to obtain the actual size
    > of a structure.
    > The programm is as follows
    >
    > struct abc
    > {
    > int j;
    > char k;
    > int i;
    > }*a;
    >
    > main()
    > {
    > a->j=10;
    >
    > printf("size of structure is %d",sizeof(a));
    > }
    >
    > In this program when I print the size of structure I get the whole
    > size (i.e size of 2 int variables+size of char variable).
    >
    > But I need to get the size of structure such that it resembles the
    > chunk of memory that is actually being used.
    >
    > Thanks
    > Krish
    >


    You have to tell your compiler to not align anything. Different
    compilers use different alignment directives.

    Just read your compiler documentation.
    jacob navia, Aug 1, 2007
    #3
  4. kris

    Mark Bluemel Guest

    kris wrote:
    > Hi I am writing a small program where I need to obtain the actual size
    > of a structure.
    > The programm is as follows
    >


    Standard headers such as <stdio.h> missing

    > struct abc
    > {
    > int j;
    > char k;
    > int i;
    > }*a;
    >
    > main()


    I'd prefer "int main(void)" and I think it may be required in C99

    > {
    > a->j=10;


    using the value of a uninitialized pointer. Bad dog! No biscuit!

    >
    > printf("size of structure is %d",sizeof(a));


    That tells you the size of the pointer. "sizeof(*a)" would tell you the
    size of the structure.

    > }


    No "return" statement is poor style.
    Mark Bluemel, Aug 1, 2007
    #4
  5. kris wrote:
    > Hi I am writing a small program where I need to obtain the actual size
    > of a structure.
    > The programm is as follows
    >
    > struct abc
    > {
    > int j;
    > char k;
    > int i;
    > }*a;
    >
    > main()
    > {
    > a->j=10;
    >
    > printf("size of structure is %d",sizeof(a));
    > }
    >
    > In this program when I print the size of structure I get the whole
    > size (i.e size of 2 int variables+size of char variable).
    >
    > But I need to get the size of structure such that it resembles the
    > chunk of memory that is actually being used.


    (That program won't work, but I assume it's just a copy&paste error?)

    First, sizeof(a) will always return the number of bytes occupied by
    pointers (since "a" is a pointer), which should be 4 on most 32-bit systems.

    Second, the size of the struct is "sizeof(struct abc)". This is the
    same as "sizeof(*a)", assuming you allocated the space for "a" with:

    a = malloc(sizeof(struct abc));

    The size of the struct is always the same. I don't understand what you
    mean with "I need to get the size of structure such that it resembles
    the chuck of memory that is actually being used".
    Nikos Chantziaras, Aug 1, 2007
    #5
  6. kris

    Mark Bluemel Guest

    Nikos Chantziaras wrote:
    > kris wrote:
    >
    >> Hi I am writing a small program where I need to obtain the actual size
    >> of a structure.
    >> The programm is as follows
    >>
    >> struct abc
    >> {
    >> int j;
    >> char k;
    >> int i;
    >> }*a;
    >>
    >> main()
    >> {
    >> a->j=10;
    >>
    >> printf("size of structure is %d",sizeof(a));
    >> }


    > Second, the size of the struct is "sizeof(struct abc)". This is the
    > same as "sizeof(*a)", assuming you allocated the space for "a" with:
    >
    > a = malloc(sizeof(struct abc));


    Nope - sizeof(*a) will return the sizeof the structure, even for an
    uninitialized or NULL pointer...
    Mark Bluemel, Aug 1, 2007
    #6
  7. kris

    santosh Guest

    kris wrote:

    >
    > Hi I am writing a small program where I need to obtain the actual size
    > of a structure.
    > The programm is as follows
    >
    > struct abc
    > {
    > int j;
    > char k;
    > int i;
    > }*a;


    a is a pointer of type struct abc*.

    > main()


    As per the C Standard main must return an int. So declare it to do so.

    int main(void)

    > {
    > a->j=10;


    a is a pointer. You need to initialise it to point to a valid structure abc
    object. Do;

    struct abc foo;
    a = &foo;
    a->j = 10;

    > printf("size of structure is %d",sizeof(a));


    The d format specifier is for an int argument. Use either lu or zu for a
    size-t argument, which is the return type of the sizeof operator.

    To calculate the size of the structure do:
    sizeof *a;

    Note the lack of parenthesis.

    > }
    >
    > In this program when I print the size of structure I get the whole
    > size (i.e size of 2 int variables+size of char variable).


    No you don't. You get the size of the pointer a.

    > But I need to get the size of structure such that it resembles the
    > chunk of memory that is actually being used.


    Do as above.
    santosh, Aug 1, 2007
    #7
  8. kris

    Army1987 Guest

    On Wed, 01 Aug 2007 07:09:35 -0700, kris wrote:

    >
    > Hi I am writing a small program where I need to obtain the actual size
    > of a structure.
    > The programm is as follows
    >
    > struct abc
    > {
    > int j;
    > char k;
    > int i;
    > }*a;
    >
    > main()

    In C99, you need to specify the return type (int).
    > {
    > a->j=10;
    >
    > printf("size of structure is %d",sizeof(a));

    sizeof returns a size_t, which cannot be an int (as expected by
    %d). See www.c-faq.com, question 12.9b.

    In C89, you need to explicitly return a value (0 to indicate
    success).
    > }
    >
    > In this program when I print the size of structure I get the whole
    > size (i.e size of 2 int variables+size of char variable).

    No, you get the size of a pointer to it.

    > But I need to get the size of structure such that it resembles the
    > chunk of memory that is actually being used.

    sizeof *a, i.e. sizeof(struct abc) is required to include the
    padding.
    (a is a pointer to struct abc, so *a is a struct abc.)
    --
    Army1987 (Replace "NOSPAM" with "email")
    "Never attribute to malice that which can be adequately explained
    by stupidity." -- R. J. Hanlon (?)
    Army1987, Aug 1, 2007
    #8
  9. kris

    Army1987 Guest

    On Wed, 01 Aug 2007 17:35:38 +0300, Nikos Chantziaras wrote:

    > kris wrote:
    >> Hi I am writing a small program where I need to obtain the actual size
    >> of a structure.
    >> The programm is as follows
    >>
    >> struct abc
    >> {
    >> int j;
    >> char k;
    >> int i;
    >> }*a;
    >>
    >> main()
    >> {
    >> a->j=10;
    >>
    >> printf("size of structure is %d",sizeof(a));
    >> }
    >>
    >> In this program when I print the size of structure I get the whole
    >> size (i.e size of 2 int variables+size of char variable).
    >>
    >> But I need to get the size of structure such that it resembles the
    >> chunk of memory that is actually being used.

    >
    > (That program won't work, but I assume it's just a copy&paste error?)
    >
    > First, sizeof(a) will always return the number of bytes occupied by
    > pointers (since "a" is a pointer), which should be 4 on most 32-bit systems.
    >
    > Second, the size of the struct is "sizeof(struct abc)". This is the
    > same as "sizeof(*a)", assuming you allocated the space for "a" with:
    >
    > a = malloc(sizeof(struct abc));
    >
    > The size of the struct is always the same. I don't understand what you
    > mean with "I need to get the size of structure such that it resembles
    > the chuck of memory that is actually being used".

    I think he means the size of the struct including the padding,
    rather than just the sum of the sizes of its members.
    --
    Army1987 (Replace "NOSPAM" with "email")
    "Never attribute to malice that which can be adequately explained
    by stupidity." -- R. J. Hanlon (?)
    Army1987, Aug 1, 2007
    #9
  10. kris

    santosh Guest

    kris wrote:

    >
    > Hi I am writing a small program where I need to obtain the actual size
    > of a structure.
    > The programm is as follows
    >
    > struct abc
    > {
    > int j;
    > char k;
    > int i;
    > }*a;
    >
    > main()
    > {
    > a->j=10;
    >
    > printf("size of structure is %d",sizeof(a));
    > }
    >
    > In this program when I print the size of structure I get the whole
    > size (i.e size of 2 int variables+size of char variable).
    >
    > But I need to get the size of structure such that it resembles the
    > chunk of memory that is actually being used.


    Don't post multiple times. Usenet, to which Google Groups interfaces, can
    sometimes be slow. Wait for a few hours before reposting. See you earlier
    post for replies.
    santosh, Aug 1, 2007
    #10
  11. kris

    Army1987 Guest

    On Wed, 01 Aug 2007 16:30:36 +0200, jacob navia wrote:

    > kris wrote:

    [snip]
    >> But I need to get the size of structure such that it resembles the
    >> chunk of memory that is actually being used.

    >
    > You have to tell your compiler to not align anything. Different
    > compilers use different alignment directives.
    >
    > Just read your compiler documentation.

    He just asked how to know the size of the struct including the
    padding, not how to force the padding to be 0. Also, a compiler
    isn't required to let you specify the alignment.

    --
    Army1987 (Replace "NOSPAM" with "email")
    "Never attribute to malice that which can be adequately explained
    by stupidity." -- R. J. Hanlon (?)
    Army1987, Aug 1, 2007
    #11
  12. kris

    jacob navia Guest

    Army1987 wrote:
    > On Wed, 01 Aug 2007 16:30:36 +0200, jacob navia wrote:
    >
    >> kris wrote:

    > [snip]
    >>> But I need to get the size of structure such that it resembles the
    >>> chunk of memory that is actually being used.

    >> You have to tell your compiler to not align anything. Different
    >> compilers use different alignment directives.
    >>
    >> Just read your compiler documentation.

    > He just asked how to know the size of the struct including the
    > padding, not how to force the padding to be 0.


    Well I think he asked for no padding. The OP only can answer this


    > Also, a compiler
    > isn't required to let you specify the alignment.
    >



    Ahh that is news to me. How would you do it then?

    (just curious how you do it without a compiler)
    jacob navia, Aug 1, 2007
    #12
  13. kris

    santosh Guest

    jacob navia wrote:

    > Army1987 wrote:
    >> On Wed, 01 Aug 2007 16:30:36 +0200, jacob navia wrote:
    >>
    >>> kris wrote:

    >> [snip]
    >>>> But I need to get the size of structure such that it resembles the
    >>>> chunk of memory that is actually being used.
    >>> You have to tell your compiler to not align anything. Different
    >>> compilers use different alignment directives.
    >>>
    >>> Just read your compiler documentation.

    >> He just asked how to know the size of the struct including the
    >> padding, not how to force the padding to be 0.

    >
    > Well I think he asked for no padding. The OP only can answer this
    >
    >
    >> Also, a compiler
    >> isn't required to let you specify the alignment.
    >>

    >
    >
    > Ahh that is news to me. How would you do it then?
    >
    > (just curious how you do it without a compiler)


    He probably means that the C Standard does not require implementations to
    allow the user to specify alignment for structure members.
    santosh, Aug 1, 2007
    #13
  14. kris

    JT Guest

    Army1987 wrote:
    > Also, a compiler
    > isn't required to let you specify the alignment.


    On Aug 1, 3:25 pm, jacob navia <> wrote:
    > Ahh that is news to me. How would you do it then?
    > (just curious how you do it without a compiler)


    I'm sure he meant
    "a compiler doesn't have to let you specify the alignment", i.e.
    "a compiler isn't required to let you specify the alignment".

    And he would be right.
    JT, Aug 1, 2007
    #14
  15. kris

    jacob navia Guest

    JT wrote:
    > Army1987 wrote:
    >> Also, a compiler
    >> isn't required to let you specify the alignment.

    >
    > On Aug 1, 3:25 pm, jacob navia <> wrote:
    >> Ahh that is news to me. How would you do it then?
    >> (just curious how you do it without a compiler)

    >
    > I'm sure he meant
    > "a compiler doesn't have to let you specify the alignment", i.e.
    > "a compiler isn't required to let you specify the alignment".
    >
    > And he would be right.
    >


    Ahh

    OK, true.

    I just did not understand the sentence
    jacob navia, Aug 1, 2007
    #15
  16. kris wrote:
    > Hi I am writing a small program where I need to obtain the actual size
    > of a structure.
    > The programm is as follows
    >
    > struct abc
    > {
    > int j;
    > char k;
    > int i;
    > }*a;
    >
    > main()
    > {
    > a->j=10;


    No space has been allocated for a to point to. Dereferencing a pointer
    is meaningless, and a nice implementation would generate a runtime error.
    >
    > printf("size of structure is %d",sizeof(a));
    > }
    >
    > In this program when I print the size of structure I get the whole
    > size (i.e size of 2 int variables+size of char variable).


    You should never get to the printf(), the program already having blown up.

    >
    > But I need to get the size of structure such that it resembles the
    > chunk of memory that is actually being used.


    You allocated no memory to actually be used. There is no "chunk of
    memory that is actually being used." Assuming you had allocated such
    memory, your question would be meaningless. The sizeof an object is
    (not merely "resembles") the size of the chunk of memory used for it.


    >
    > Thanks
    > Krish
    >
    Martin Ambuhl, Aug 1, 2007
    #16
  17. kris

    Army1987 Guest

    On Wed, 01 Aug 2007 17:49:59 +0200, jacob navia wrote:

    > JT wrote:
    >> Army1987 wrote:
    >>> Also, a compiler
    >>> isn't required to let you specify the alignment.

    >>
    >> On Aug 1, 3:25 pm, jacob navia <> wrote:
    >>> Ahh that is news to me. How would you do it then?
    >>> (just curious how you do it without a compiler)

    >>
    >> I'm sure he meant
    >> "a compiler doesn't have to let you specify the alignment", i.e.
    >> "a compiler isn't required to let you specify the alignment".
    >>
    >> And he would be right.
    >>

    >
    > Ahh
    >
    > OK, true.
    >
    > I just did not understand the sentence

    How on Earth did you interpret it?
    --
    Army1987 (Replace "NOSPAM" with "email")
    "Never attribute to malice that which can be adequately explained
    by stupidity." -- R. J. Hanlon (?)
    Army1987, Aug 1, 2007
    #17
  18. kris

    jacob navia Guest

    Army1987 wrote:
    > On Wed, 01 Aug 2007 17:49:59 +0200, jacob navia wrote:
    >
    >> JT wrote:
    >>> Army1987 wrote:
    >>>> Also, a compiler
    >>>> isn't required to let you specify the alignment.
    >>> On Aug 1, 3:25 pm, jacob navia <> wrote:
    >>>> Ahh that is news to me. How would you do it then?
    >>>> (just curious how you do it without a compiler)
    >>> I'm sure he meant
    >>> "a compiler doesn't have to let you specify the alignment", i.e.
    >>> "a compiler isn't required to let you specify the alignment".
    >>>
    >>> And he would be right.
    >>>

    >> Ahh
    >>
    >> OK, true.
    >>
    >> I just did not understand the sentence

    > How on Earth did you interpret it?


    I am ashamed to confess that I understood that no compiler
    is required !!!

    PARSE ERROR!

    :)
    jacob navia, Aug 1, 2007
    #18
  19. kris

    Army1987 Guest

    On Wed, 01 Aug 2007 21:27:44 +0200, jacob navia wrote:

    > Army1987 wrote:
    >> On Wed, 01 Aug 2007 17:49:59 +0200, jacob navia wrote:
    >>
    >>> JT wrote:
    >>>> Army1987 wrote:
    >>>>> Also, a compiler
    >>>>> isn't required to let you specify the alignment.
    >>>> On Aug 1, 3:25 pm, jacob navia <> wrote:
    >>>>> Ahh that is news to me. How would you do it then?
    >>>>> (just curious how you do it without a compiler)
    >>>> I'm sure he meant
    >>>> "a compiler doesn't have to let you specify the alignment", i.e.
    >>>> "a compiler isn't required to let you specify the alignment".
    >>>>
    >>>> And he would be right.
    >>>>
    >>> Ahh
    >>>
    >>> OK, true.
    >>>
    >>> I just did not understand the sentence

    >> How on Earth did you interpret it?

    >
    > I am ashamed to confess that I understood that no compiler
    > is required !!!
    >
    > PARSE ERROR!
    >
    > :)

    Now I get it. "You don't need a compiler in order to specify the
    alignment." I would have never realized that my sentence could
    mean *that* by myself...
    --
    Army1987 (Replace "NOSPAM" with "email")
    "Never attribute to malice that which can be adequately explained
    by stupidity." -- R. J. Hanlon (?)
    Army1987, Aug 1, 2007
    #19
  20. kris

    Jack Klein Guest

    On Wed, 01 Aug 2007 15:35:21 +0100, Mark Bluemel
    <> wrote in comp.lang.c:

    > kris wrote:
    > > Hi I am writing a small program where I need to obtain the actual size
    > > of a structure.
    > > The programm is as follows
    > >

    >
    > Standard headers such as <stdio.h> missing
    >
    > > struct abc
    > > {
    > > int j;
    > > char k;
    > > int i;
    > > }*a;
    > >
    > > main()

    >
    > I'd prefer "int main(void)" and I think it may be required in C99


    The void in the parentheses is not, if you don't need the definition
    to also serve as a prototype. Implicit int is gone, so either:

    int main()

    ....or:

    int main(void)

    ....are acceptable.

    > > {
    > > a->j=10;

    >
    > using the value of a uninitialized pointer. Bad dog! No biscuit!


    No, a is most certainly initialized. It's a null pointer.

    > > printf("size of structure is %d",sizeof(a));

    >
    > That tells you the size of the pointer. "sizeof(*a)" would tell you the
    > size of the structure.
    >
    > > }

    >
    > No "return" statement is poor style.


    --
    Jack Klein
    Home: http://JK-Technology.Com
    FAQs for
    comp.lang.c http://c-faq.com/
    comp.lang.c++ http://www.parashift.com/c -faq-lite/
    alt.comp.lang.learn.c-c++
    http://www.club.cc.cmu.edu/~ajo/docs/FAQ-acllc.html
    Jack Klein, Aug 1, 2007
    #20
    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. Kislay

    Size of a structure : Structure Padding

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

    Preferred Size, Minimum Size, Size

    Jason Cavett, May 23, 2008, in forum: Java
    Replies:
    5
    Views:
    12,502
    Michael Jung
    May 25, 2008
  3. What-a-Tool

    Method for obtaining client display size?

    What-a-Tool, Jan 4, 2005, in forum: ASP General
    Replies:
    4
    Views:
    155
    What-a-Tool
    Jan 4, 2005
  4. Joel

    Obtaining size of the scrollbar

    Joel, Jul 7, 2004, in forum: Javascript
    Replies:
    3
    Views:
    83
  5. Daz
    Replies:
    2
    Views:
    76
Loading...

Share This Page