Struct size

J

Jeff P. Syverson

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.
 
I

Ian Collins

Jeff said:
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?
 
C

C. Benson Manica

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.
 
R

Richard

C. Benson Manica said:
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?
 
R

Richard Heathfield

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.
 
E

Eric Sosman

Jeff said:
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!
 
R

Richard

Eric Sosman said:
Jeff said:
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.
 
R

Roberto Waltman

Ian Collins said:
Jeff said:
... 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 ]
 
E

Eric Sosman

Richard said:
Eric Sosman said:
Jeff said:
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'?"
 
R

Richard Heathfield

Eric Sosman said:

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.
 
C

C. Benson Manica

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.
 
K

Keith Thompson

Richard Heathfield said:
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.
 
K

Keith Thompson

Jeff P. Syverson said:
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.
 
R

Richard Heathfield

Keith Thompson said:
Richard Heathfield said:
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. :)
 
K

Keith Thompson

Roberto Waltman said:
Ian Collins said:
Jeff said:
... 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 "-- ".
 
C

C. Benson Manica

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

CBFalconer

Roberto said:
Ian Collins said:
Jeff said:
... 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.
 
C

CBFalconer

Eric said:
Richard wrote:
.... snip ...


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."


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>
 
J

jaysome

Eric Sosman said:



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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

variadic function 27
soap library for python-3.x 0
SAP MM Cupertino, CA 3
Python IDLE 2
Zero-size array as struct member 160
Contact details for Eric Sosman 18
Reading struct with structs 1
Problems with os.walk 1

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,901
Latest member
Noble71S45

Latest Threads

Top