sizeof

C

chandanlinster

There is a sentence in the book "The C programming language" that says
- "C provides a compile-time unary operator called sizeof that can be
used to compute the size of any object." What does the phrase
"compile-time unary operator mean?
 
C

Cong Wang

chandanlinster said:
There is a sentence in the book "The C programming language" that says
- "C provides a compile-time unary operator called sizeof that can be
used to compute the size of any object." What does the phrase
"compile-time unary operator mean?
"Compile-time" means determining by compiler when the program is
compiled, instead of deciding before compiling.
And "unary" means that the operator only has one operand.
 
R

Richard Heathfield

chandanlinster said:
There is a sentence in the book "The C programming language" that says
- "C provides a compile-time unary operator called sizeof that can be
used to compute the size of any object." What does the phrase
"compile-time unary operator mean?

"Compile-time" means "it does its thing during compilation". It can't be
before compilation because the compiler writer can't know in advance how
big your struct chandanlinster object will be, right? And it can't be after
compilation because, if it were, they'd have said "run-time unary
operator", right? :)

"Unary" just means "takes one operand".

Incidentally, as of C99, sizeof got a bit confused, and it will now do its
thing at *runtime* if the object is a variable-length array:

int foo(int size)
{
int arr[int size];
size_t bytes = sizeof arr; /* this will be done at runtime */
 
C

Chris Dollin

chandanlinster said:
There is a sentence in the book "The C programming language" that says
- "C provides a compile-time unary operator called sizeof that can be
used to compute the size of any object." What does the phrase
"compile-time unary operator mean?

It's an operator ("operator"); it takes one argument ("unary"); the result
is calculated at compile-time (not left to be evaluated at run-time [1])
("compile-time"); further, it so happens that this counts as a
constant-expression (so you can use it where a constant is required,
eg as an array size or case label [2] or static initialiser).

[1] Except for C99 variable-length arrays.

[2] Which may lead to madness or badness.
 
A

Ancient_Hacker

Richard Heathfield wrote:
[snip]
It can't be
before compilation because the compiler writer can't know in advance how
big your struct chandanlinster object will be, right?

There are those things called "precompiled headers". :)
 
R

Richard Heathfield

Ancient_Hacker said:
Richard Heathfield wrote:
[snip]
It can't be
before compilation because the compiler writer can't know in advance how
big your struct chandanlinster object will be, right?

There are those things called "precompiled headers". :)

Chapter and verse, please.
 
A

Ancient_Hacker

Richard said:
Ancient_Hacker said:

Chapter and verse, please.

Oh, righgt, it's from that part of the Apocrypha, The Book of Bill,
Chapter VC, Verse 6:

"And it came to pass, (pass 1, pass 2, pass3...) that the compiler did
grind exceedingly slow, and all the peoples were distraught. A man in
rags came up to Him, and said "Oh Blessed Bill forsooth, my children
and wife go hungry, and I must wear rags, as I don't get paid until
this program compiles, and with seventy three files of the include, and
they be not seventy two, nor do they reach seventy four, their number
is exactly seventy three (as I must drag in most of X11 to get one
bleepin' #define). And He saw that is was true-- at each #include the
disk did chug forth, searching the directories of the angles and of the
quotes, and the compiler's symbol tables did groan and quake, and the
sun did seem to fly through the heavens, and the compiler rambled on
and on and on. And the hacker people were exceedingly wroth, and
tended toward uncomely dissipations and unflattering contumely while
waiting for the compiler to digest all the include files.

And then a light of the bulb went on above the head of Bill, "Hark, the
stdio.h and math.h, do they not idle? neither do they flux nor do
they flow-- they mostly lieth in repose. The holy trinity of
preprocessor, compiler, and linker, do they need to say "Hark-- what is
this new symbol ULONG_MAX? Is it nanosecond fresh and new, untarnished
by the careless waves of time? Nay, it most likely is that which we
know so well and have many times clutched to our bosom-- our comrade
and friend, unchanged for all these many years. We need not a long and
arduous introduction to you-- please prepare yourself, togged out in
glorious hashtable rainments, come now, let me take you in with one
grand binary fread(), you and all of your friends and disciples too,
all are welcome."

And so, it came to pass, since version 4, and so on down through its
descendants, without quiver or hesitation, the mantle was passed. And
the compiler rejoiced, for it had 99% less hashing to do, and the pooor
hackers rejoiced, for hello_world.c no longer took six minutes to
compile.

And the hackers rejoiced, no longer did they have time to wander the
halls or visit the machine of the pop. In a flash they'd get the
latest bellowings of the errors: "missing ) before ;". And all was
slightly better threrefore.
 
R

Richard Heathfield

Ancient_Hacker said:
Oh, righgt, it's from that part of the Apocrypha, The Book of Bill,
Chapter VC, Verse 6:

Excellent answer (reluctantly snipped). Alas, the Apocrypha is uncanonical.
 
T

T.M. Sommers

Ancient_Hacker said:
Richard Heathfield wrote:
[snip]
It can't be
before compilation because the compiler writer can't know in advance how
big your struct chandanlinster object will be, right?

There are those things called "precompiled headers". :)

Even pre-compiled headers get compiled some time.
 
W

Walter Roberson

Even pre-compiled headers get compiled some time.

If they are headers of the implementation that are specified by
the C standards, they are not necessarily compiled at all: they could
be built in to the compiler and merely activated by seeing the
reference to the appropriate header file. The C standards deliberately
do not restrict the standard header files to be text files.
 
T

T.M. Sommers

Walter said:
If they are headers of the implementation that are specified by
the C standards, they are not necessarily compiled at all:

If they were never compiled, they wouldn't be pre-compiled
headers, they would be un-compiled headers.
 
W

Walter Roberson

Walter said:
If they were never compiled, they wouldn't be pre-compiled
headers, they would be un-compiled headers.

If we split the meanings of "compiled"...

Implementation headers could be "compiled" (gathered together and
arranged, as in compiling a dictionary -- OED meaning I.1),
without being "compiled" (translated from a higher level language to
a machine code -- OED meaning I.2c)
 

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

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top