Struct size

Discussion in 'C Programming' started by Jeff P. Syverson, Sep 20, 2007.

  1. Can anyone tell me the best way to find out the size of a struct, without
    using the sizeof function? I'm currently thinking of:

    struct foo s[2];
    int size_of_foo = (char *)&s[1] - (char *)s;

    but is there a more elegant way (e.g. requiring no memory).

    Thanks in advance!
    -Jeff


    ******************************************************************************
    * *
    * JEFF P. SYVERSON, PHD *
    * Senior Systems Engineer *
    * *
    * CITRIC SOLUTIONS INC. -- 2495 SUNRISE BLVD -- RANCHO CORDOVA -- CA *
    * *
    ******************************************************************************

    This email and any file attachments(s) are confidential, may be legally
    privileged, and are intended solely for use by the identified recipient(s). If
    you received the email in error, please notify the sender and delete the
    message and any copies completely from your computer. Dissemination,
    distribution, or copying of this communication, in whole or in part, by any
    unintended or unauthorized recipient is prohibited and may subject you to
    liability under 18 U.S.C. sec. 2511. Citric Solutions, Inc. assumes no
    liability and makes no warranties or representations that the email or any
    file attachments are totally secure and/or virus free. The recipient is
    advised to check for viruses prior to opening any file attached to the email.
    Statements contained in the email or any file attachment may not be authorized
    or endorsed by Citric Solutions, Inc. Citric Solutions, Inc. accepts no
    liability, whether in contract, tort, or equity, for any unauthorized
    statement or offer of contract communicated in the email or any file attached
    by the sender.
    Jeff P. Syverson, Sep 20, 2007
    #1
    1. Advertising

  2. Jeff P. Syverson

    Ian Collins Guest

    Jeff P. Syverson wrote:
    > Can anyone tell me the best way to find out the size of a struct, without
    > using the sizeof function? I'm currently thinking of:
    >
    > struct foo s[2];
    > int size_of_foo = (char *)&s[1] - (char *)s;
    >
    > but is there a more elegant way (e.g. requiring no memory).
    >

    Why, sizeof is there for a good reason

    > Thanks in advance!
    > -Jeff
    >
    >
    > ******************************************************************************
    > * *
    > * JEFF P. SYVERSON, PHD *
    > * Senior Systems Engineer *
    > * *
    > * CITRIC SOLUTIONS INC. -- 2495 SUNRISE BLVD -- RANCHO CORDOVA -- CA *
    > * *
    > ******************************************************************************
    >
    > This email and any file attachments(s) are confidential, may be legally
    > privileged, and are intended solely for use by the identified recipient(s). If
    > you received the email in error, please notify the sender and delete the
    > message and any copies completely from your computer. Dissemination,
    > distribution, or copying of this communication, in whole or in part, by any
    > unintended or unauthorized recipient is prohibited and may subject you to
    > liability under 18 U.S.C. sec. 2511. Citric Solutions, Inc. assumes no
    > liability and makes no warranties or representations that the email or any
    > file attachments are totally secure and/or virus free. The recipient is
    > advised to check for viruses prior to opening any file attached to the email.
    > Statements contained in the email or any file attachment may not be authorized
    > or endorsed by Citric Solutions, Inc. Citric Solutions, Inc. accepts no
    > liability, whether in contract, tort, or equity, for any unauthorized
    > statement or offer of contract communicated in the email or any file attached
    > by the sender.


    Surly you can trim all this nonsense?

    --
    Ian Collins.
    Ian Collins, Sep 20, 2007
    #2
    1. Advertising

  3. On Sep 19, 8:44 pm, "Jeff P. Syverson" <> wrote:
    > Can anyone tell me the best way to find out the size of a struct, without
    > using the sizeof function?


    This question is asked routinely - search the group archives. I would
    think that someone with the title of "Senior Systems Engineer" and who
    is apparently important enough to be constrained to include a long
    enough disclaimer to rival the M$ EULA in every post would be up to
    that task. Seriously, get yourself some other posting host or at
    least get yourself a properly-delimited signature.
    C. Benson Manica, Sep 20, 2007
    #3
  4. Jeff P. Syverson

    Richard Guest

    "C. Benson Manica" <> writes:

    > On Sep 19, 8:44 pm, "Jeff P. Syverson" <> wrote:
    >> Can anyone tell me the best way to find out the size of a struct, without
    >> using the sizeof function?

    >
    > This question is asked routinely - search the group archives. I would
    > think that someone with the title of "Senior Systems Engineer" and who
    > is apparently important enough to be constrained to include a long
    > enough disclaimer to rival the M$ EULA in every post would be up to
    > that task. Seriously, get yourself some other posting host or at
    > least get yourself a properly-delimited signature.


    Why are you here? Will you post this to every question which is already
    answered in google? Or did you just want to put your hand up and
    announce yourself unwilling to help?
    Richard, Sep 20, 2007
    #4
  5. Jeff P. Syverson said:

    > Can anyone tell me the best way to find out the size of a struct, without
    > using the sizeof function?


    The designer of the C programming language, Dennis Ritchie, is one smart
    cookie, and he saw at once, all those years ago, that it would simply not
    be right for people to use a sizeof function to establish the sizes of
    types and expressions of those types. So he invented a keyword, sizeof,
    thus making it illegal (nowadays, we'd say "a constraint violation
    requiring a diagnostic message") to even /write/ a function named sizeof.
    You may therefore rest assured that it is impossible to use a sizeof
    function (let alone *the* sizeof function) in C.

    Incidentally, as well as rendering the creating of a function named sizeof
    impossible, the sizeof keyword has a little-known secondary use - it
    yields the size of its operand, in bytes. What's more, it evaluates this
    size during the translation of the program, rather than waiting until
    runtime, which is a nice touch.

    So the answer to your question is this: the best way to find out the size
    of a struct, without using the sizeof function, is to use the sizeof
    keyword.

    --
    Richard Heathfield <http://www.cpax.org.uk>
    Email: -www. +rjh@
    Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
    "Usenet is a strange place" - dmr 29 July 1999
    Richard Heathfield, Sep 20, 2007
    #5
  6. Jeff P. Syverson

    Eric Sosman Guest

    Jeff P. Syverson wrote:
    > Can anyone tell me the best way to find out the size of a struct, without
    > using the sizeof function? [...]
    >
    > * JEFF P. SYVERSON, PHD *
    > * Senior Systems Engineer *
    > [... twenty-four lines of boilerplate all told]


    A PhD asks (relays, more likely) this silly question?
    A Senior Systems Engineer thinks sizeof is a function?
    Jeee-zus!

    In my day, people got PhD's only if they were smart and/or
    had some ability to conduct research; JEFF P. SYVERSON, PHD is
    apparently unable to find Google. In my day, Senior Systems
    Engineers were people of experience, battle-scarred and wise in
    the ways of many languages and disciplines; JEFF P. SYVERSON, PHD
    has attained the title without acquiring any knowledge of C, not
    even the knowledge that would come from a first-level textbook.

    Just use sizeof, Jeff. That's what it's for. Jeee-zus!

    --
    Eric Sosman
    lid
    Eric Sosman, Sep 20, 2007
    #6
  7. Jeff P. Syverson

    Richard Guest

    Eric Sosman <> writes:

    > Jeff P. Syverson wrote:
    >> Can anyone tell me the best way to find out the size of a struct, without
    >> using the sizeof function? [...]
    >>
    >> * JEFF P. SYVERSON, PHD *
    >> * Senior Systems Engineer *
    >> [... twenty-four lines of boilerplate all told]

    >
    > A PhD asks (relays, more likely) this silly question?
    > A Senior Systems Engineer thinks sizeof is a function?
    > Jeee-zus!
    >
    > In my day, people got PhD's only if they were smart and/or
    > had some ability to conduct research; JEFF P. SYVERSON, PHD is
    > apparently unable to find Google. In my day, Senior Systems
    > Engineers were people of experience, battle-scarred and wise in
    > the ways of many languages and disciplines; JEFF P. SYVERSON, PHD
    > has attained the title without acquiring any knowledge of C, not
    > even the knowledge that would come from a first-level textbook.
    >
    > Just use sizeof, Jeff. That's what it's for. Jeee-zus!


    Just because he has a PhD it doesn't mean it was in C. A Senior Systems
    Engineer doesn't have to know any C. And clearly he has an ulterior
    motive for not wanting to use sizeof. I suspect home work for an
    "impprovement" course.
    Richard, Sep 20, 2007
    #7
  8. [OT] Legal statements / disclaimers in signatures. (Was: Struct size)

    Ian Collins <> wrote:
    >Jeff P. Syverson wrote:
    >> ... without using the sizeof function? ...

    ....
    >Why, sizeof is there for a good reason

    ....
    >> [And then the grand finale, 15 lines of legalese]

    >
    >Surly you can trim all this nonsense?


    To all those who find this unusual, consider yourselves lucky you were
    not forced to do the same.
    There are companies that require this type of statements on outgoing
    email. That text may have been added automatically by his employer
    email system, with or without Mr. Syverson's consent.

    Roberto Waltman

    [ Please reply to the group,
    return address is invalid ]
    Roberto Waltman, Sep 20, 2007
    #8
  9. Jeff P. Syverson

    Eric Sosman Guest

    Richard wrote:
    > Eric Sosman <> writes:
    >
    >> Jeff P. Syverson wrote:
    >>> Can anyone tell me the best way to find out the size of a struct, without
    >>> using the sizeof function? [...]
    >>>
    >>> * JEFF P. SYVERSON, PHD *
    >>> * Senior Systems Engineer *
    >>> [... twenty-four lines of boilerplate all told]

    >> A PhD asks (relays, more likely) this silly question?
    >> A Senior Systems Engineer thinks sizeof is a function?
    >> Jeee-zus!
    >>
    >> In my day, people got PhD's only if they were smart and/or
    >> had some ability to conduct research; JEFF P. SYVERSON, PHD is
    >> apparently unable to find Google. In my day, Senior Systems
    >> Engineers were people of experience, battle-scarred and wise in
    >> the ways of many languages and disciplines; JEFF P. SYVERSON, PHD
    >> has attained the title without acquiring any knowledge of C, not
    >> even the knowledge that would come from a first-level textbook.
    >>
    >> Just use sizeof, Jeff. That's what it's for. Jeee-zus!

    >
    > Just because he has a PhD it doesn't mean it was in C.


    That part's true. A PhD in C would be pretty silly,
    anyhow.[*] But even a PhD in Experimental Theology ought
    to be able to do research well beyond the Google level.

    [*] When I (very briefly, and only through an odd set of
    circumstances) taught programming, I was a little put out at
    the idea that a course about a language carried college credits.
    It seemed to me that languages were the tools one used in pursuit
    of other goals, not goals in themselves; giving semester-hours
    for learning FORTRAN was like giving semester-hours for learning
    to spell. I wanted to teach discrete simulation, or numerical
    methods, or things of that nature; I wanted to "take it for
    granted" that the students would have or acquire enough FORTRAN
    or PL/I or whatever to be able to work through their projects.
    The projects were what mattered to me, not the languages. Alas,
    the people who set the curricula (I was *very* junior) had me
    teach "Introduction to FORTRAN" and "FORTRAN for the Sciences"
    and "PL/I for Simulation" and suchlike. Maybe someday they'll
    give PhD's for learning how to use the subjunctive ...

    > A Senior Systems
    > Engineer doesn't have to know any C.


    Twenty years ago, no. And twenty years from now, perhaps
    not. But today, right now, a systems engineer ignorant of C --
    even of the bare basics of C -- ought not to carry the label
    "senior."

    > And clearly he has an ulterior
    > motive for not wanting to use sizeof. I suspect home work for an
    > "impprovement" course.


    Ayeh. One of the improvements should be the ability to
    recognize and reject stupid requirements (another mark of
    seniority and experience). "What's the most elegant and
    efficient way to distinguish even numbers from odds without
    using the notion of `two'?"

    --
    Eric Sosman
    lid
    Eric Sosman, Sep 20, 2007
    #9
  10. Eric Sosman said:

    <snip>

    > One of the improvements should be the ability to
    > recognize and reject stupid requirements (another mark of
    > seniority and experience). "What's the most elegant and
    > efficient way to distinguish even numbers from odds without
    > using the notion of `two'?"


    Another important attribute is the ability to meet stupid requirements
    easily, quickly, and with a straight face:

    int is_odd(unsigned long n)
    {
    return n & 1;
    }

    int is_even(unsigned long n)
    {
    return !is_odd(n);
    }

    Some fast talking may also be required.

    --
    Richard Heathfield <http://www.cpax.org.uk>
    Email: -www. +rjh@
    Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
    "Usenet is a strange place" - dmr 29 July 1999
    Richard Heathfield, Sep 20, 2007
    #10
  11. Re: Legal statements / disclaimers in signatures. (Was: Struct size)

    On Sep 19, 10:18 pm, Roberto Waltman <> wrote:

    > To all those who find this unusual, consider yourselves lucky you were
    > not forced to do the same.
    > There are companies that require this type of statements on outgoing
    > email. That text may have been added automatically by his employer
    > email system, with or without Mr. Syverson's consent.


    IMO that is one of the few excellent reasons to use Google (or
    anything other than one's employer e-mail) for posting. Given the
    terms listed in the boilerplate, one could argue that Google is itself
    an "unintended recipient" and therefore criminally liable for
    "disseminating" the original post, not to mention a hapless responder
    who quotes the original post.
    C. Benson Manica, Sep 20, 2007
    #11
  12. Richard Heathfield <> writes:
    [...]
    > The designer of the C programming language, Dennis Ritchie, is one smart
    > cookie, and he saw at once, all those years ago, that it would simply not
    > be right for people to use a sizeof function to establish the sizes of
    > types and expressions of those types. So he invented a keyword, sizeof,
    > thus making it illegal (nowadays, we'd say "a constraint violation
    > requiring a diagnostic message") to even /write/ a function named sizeof.
    > You may therefore rest assured that it is impossible to use a sizeof
    > function (let alone *the* sizeof function) in C.

    [...]

    A quibble: Attempting to declare a function named "sizeof" is not a
    constraint violation requiring a diagnostic message. It's a syntax
    rule violation requiring a diagnostic message.

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
    "We must do something. This is something. Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"
    Keith Thompson, Sep 20, 2007
    #12
  13. "Jeff P. Syverson" <> writes:
    > Can anyone tell me the best way to find out the size of a struct, without
    > using the sizeof function? I'm currently thinking of:
    >
    > struct foo s[2];
    > int size_of_foo = (char *)&s[1] - (char *)s;
    >
    > but is there a more elegant way (e.g. requiring no memory).


    It's easy to make the mistake of thinking that 'sizeof' is a function.
    In fact, it's an operator. It's unusual in that it's the only
    operator whose name is a keyword (which looks like an identifier)
    rather than a token consisting of one or more punctuation marks.

    Yes, I can think of a way to simplify your code slightly (you don't
    need a 2-element array), but there's not much point in doing so.
    You're asking the equivalent of "What's the best way to drive a nail
    without using a hammer?", or "What's the best way to steer my car
    without using the steering wheel?". In all three cases, there's
    probably an answer, but the real answer is to use the right tool for
    the job in the first place.

    Explain *why* you don't want to use the sizeof operator, and perhaps
    we can help. We get this question fairly frequently; it's usually a
    homework question. The only valid reason I can think of to compute
    the size of a struct without using 'sizeof' is to show how ugly the
    resulting code is, demonstrating that including 'sizeof' as a built-in
    operator was a really good idea.

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
    "We must do something. This is something. Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"
    Keith Thompson, Sep 20, 2007
    #13
  14. Keith Thompson said:

    > Richard Heathfield <> writes:
    > [...]
    >> The designer of the C programming language, Dennis Ritchie, is one smart
    >> cookie, and he saw at once, all those years ago, that it would simply
    >> not be right for people to use a sizeof function to establish the sizes
    >> of types and expressions of those types. So he invented a keyword,
    >> sizeof, thus making it illegal (nowadays, we'd say "a constraint
    >> violation requiring a diagnostic message") to even /write/ a function
    >> named sizeof. You may therefore rest assured that it is impossible to
    >> use a sizeof function (let alone *the* sizeof function) in C.

    > [...]
    >
    > A quibble: Attempting to declare a function named "sizeof" is not a
    > constraint violation requiring a diagnostic message. It's a syntax
    > rule violation requiring a diagnostic message.


    In C89, it violates the constraint in 3.1.2:

    "In translation phases 7 and 8, an identifier shall not consist of
    the same sequence of characters as a keyword."

    In C99, this constraint seems to have disappeared. So we're both right. :)

    --
    Richard Heathfield <http://www.cpax.org.uk>
    Email: -www. +rjh@
    Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
    "Usenet is a strange place" - dmr 29 July 1999
    Richard Heathfield, Sep 20, 2007
    #14
  15. Re: [OT] Legal statements / disclaimers in signatures. (Was: Struct size)

    Roberto Waltman <> writes:
    > Ian Collins <> wrote:
    >>Jeff P. Syverson wrote:
    >>> ... without using the sizeof function? ...

    > ...
    >>Why, sizeof is there for a good reason

    > ...
    >>> [And then the grand finale, 15 lines of legalese]

    >>
    >>Surly you can trim all this nonsense?

    >
    > To all those who find this unusual, consider yourselves lucky you were
    > not forced to do the same.
    > There are companies that require this type of statements on outgoing
    > email. That text may have been added automatically by his employer
    > email system, with or without Mr. Syverson's consent.


    But he's using an aioe.org NNTP server.

    Mr. Syverson: If at all possible, please avoid including the huge
    legal disclaimer in any future postings here. Usenet messages are not
    e-mail; the disclaimer is meaningless (but I'm not a lawyer). If for
    some reason you absolutely must include the disclaimer, please precede
    it and your signature with a delimiter line consisting of "-- ".

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
    "We must do something. This is something. Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"
    Keith Thompson, Sep 20, 2007
    #15
  16. On Sep 19, 9:26 pm, Richard <> wrote:

    > Why are you here? Will you post this to every question which is already
    > answered in google? Or did you just want to put your hand up and
    > announce yourself unwilling to help?


    Well, I suppose it would have been more helpful if I had troubled
    myself to locate the relevant posts myself, but it's not *my*
    question. If you answer a man's question, he gets an A/a raise/the
    warm glow of knowledge one time, but if you teach him to search the
    Google archives, he can experience that euphoria for a lifetime, and
    with no further tiresome importunement upon the group to boot.
    C. Benson Manica, Sep 20, 2007
    #16
  17. Jeff P. Syverson

    CBFalconer Guest

    Re: [OT] Legal statements / disclaimers in signatures. (Was: Structsize)

    Roberto Waltman wrote:
    > Ian Collins <> wrote:
    >> Jeff P. Syverson wrote:
    >>
    >>> ... without using the sizeof function? ...

    > ...
    >> Why, sizeof is there for a good reason

    > ...
    >>> [And then the grand finale, 15 lines of legalese]

    >>
    >> Surly you can trim all this nonsense?

    >
    > To all those who find this unusual, consider yourselves lucky you
    > were not forced to do the same. There are companies that require
    > this type of statements on outgoing email. That text may have been
    > added automatically by his employer email system, with or without
    > Mr. Syverson's consent.


    However that was a Usenet entry, not e-mail. It should not have
    gone through an email system at all. Of course his organization
    may not be aware of all this, and still have forced the ridiculous
    appendage, without even a sig marker.

    --
    Chuck F (cbfalconer at maineline dot net)
    Available for consulting/temporary embedded and systems.
    <http://cbfalconer.home.att.net>



    --
    Posted via a free Usenet account from http://www.teranews.com
    CBFalconer, Sep 20, 2007
    #17
  18. Jeff P. Syverson

    CBFalconer Guest

    Eric Sosman wrote:
    > Richard wrote:
    >

    .... snip ...
    >
    >> A Senior Systems Engineer doesn't have to know any C.

    >
    > Twenty years ago, no. And twenty years from now, perhaps not.
    > But today, right now, a systems engineer ignorant of C -- even of
    > the bare basics of C -- ought not to carry the label "senior."
    >
    >> And clearly he has an ulterior motive for not wanting to use
    >> sizeof. I suspect home work for an "impprovement" course.

    >
    > Ayeh. One of the improvements should be the ability to recognize
    > and reject stupid requirements (another mark of seniority and
    > experience). "What's the most elegant and efficient way to
    > distinguish even numbers from odds without using the notion of
    > `two'?"


    And maybe someone should suggest that he get and read K&R2 and/or
    the C standard. The links in my sig may help.

    --
    Some useful references about C:
    <http://www.ungerhu.com/jxh/clc.welcome.txt>
    <http://www.eskimo.com/~scs/C-faq/top.html> (C-faq)
    <http://benpfaff.org/writings/clc/off-topic.html>
    <http://anubis.dkuug.dk/jtc1/sc22/wg14/www/docs/n869/> (C99 std)
    <http://www.dinkumware.com/refxc.html> (C-library}
    <http://gcc.gnu.org/onlinedocs/> (GNU docs)
    <http://clc-wiki.net/wiki/C_community:comp.lang.c:Introduction>



    --
    Posted via a free Usenet account from http://www.teranews.com
    CBFalconer, Sep 20, 2007
    #18
  19. Jeff P. Syverson

    jaysome Guest

    On Thu, 20 Sep 2007 03:12:41 +0000, Richard Heathfield
    <> wrote:

    >Eric Sosman said:
    >
    ><snip>
    >
    >> One of the improvements should be the ability to
    >> recognize and reject stupid requirements (another mark of
    >> seniority and experience). "What's the most elegant and
    >> efficient way to distinguish even numbers from odds without
    >> using the notion of `two'?"

    >
    >Another important attribute is the ability to meet stupid requirements
    >easily, quickly, and with a straight face:
    >
    >int is_odd(unsigned long n)
    >{
    > return n & 1;
    >}


    Quick.

    >int is_even(unsigned long n)
    >{
    > return !is_odd(n);
    >}


    Easy.

    >Some fast talking may also be required.


    Fortunately, C99 introduced the keyword "inline". As a result, in the
    above code, "Easy" should equate to "Quick"--just insert "inline" in
    its proper place. It follows that no fast talking should be required.
    Unless of course those requirement writers are savvy enough to ask
    which C99 compiler you're using. Perhaps some fast-talking may well be
    required.

    Requirement writer: "Can you make it quick and easy"?
    Programmer: "Yes".
    Requirement writer: "What does that require"?
    Programmer: "A C99 compiler".
    Requirement writer: "That should be no problem, right, given that
    we're in the year 2007"?
    Programmer: "Yes, of course".
    Requirement writer: "I'm curious, which C99 compiler will you be
    using"?
    Programmer: "Did you see that Bear's game last night"?

    Regards
    --
    jay
    jaysome, Sep 20, 2007
    #19
  20. "Eric Sosman" <> schrieb im Newsbeitrag
    news:...
    > a PhD in Experimental Theology

    ROTFL, hilarious! Thanks for that, you made my day

    bye, Jojo
    Joachim Schmitz, Sep 20, 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. Chris Fogelklou
    Replies:
    36
    Views:
    1,371
    Chris Fogelklou
    Apr 20, 2004
  2. Replies:
    5
    Views:
    534
    Flash Gordon
    Apr 9, 2006
  3. Replies:
    8
    Views:
    500
    Bob Hairgrove
    Apr 10, 2006
  4. Jason Cavett

    Preferred Size, Minimum Size, Size

    Jason Cavett, May 23, 2008, in forum: Java
    Replies:
    5
    Views:
    12,559
    Michael Jung
    May 25, 2008
  5. Jaco Naude
    Replies:
    2
    Views:
    1,461
    Jaco Naude
    Jan 13, 2010
Loading...

Share This Page