sizeof operator implementation

S

srivatsan_b

Can anybody please explain how sizeof operator is implemented?

Thanks in advance
Srivatsan B
 
E

Emmanuel Delahaye

srivatsan_b wrote on 01/05/05 :
Can anybody please explain how sizeof operator is implemented?

No. Implementation is not part of the C language. The standard only
describes the behaviour and the interface of this operator.

"Returns the size of a (type) or of an object expressed in bytes. The
returned type is size_t."

Why do you want to know these detalls ?

--
Emmanuel
The C-FAQ: http://www.eskimo.com/~scs/C-faq/faq.html
The C-library: http://www.dinkumware.com/refxc.html

..sig under repair
 
M

Malcolm

srivatsan_b said:
Can anybody please explain how sizeof operator is implemented?
In traditional C it looks like a function but isn't. sizeof(int) resolves to
a compile time constant, which the compiler cna do because it knows the
number of bytes taken for each type.
(You cannot portably replace with a constant in code because int may be 2, 4
or sometimes even a different number of bytes on different machines).
 
L

Lawrence Kirby

Can anybody please explain how sizeof operator is implemented?

Thanks in advance
Srivatsan B

As you say sizeof is an operator i.e. it is built into the compiler just
like + or >>

The sizeof operator evaluates to the size in bytes of the type of its
operand. Since the compiler knows the size of any type it supports it
simply determines the type and then the size of that type.

Lawrence
 
J

James McIninch

<posted & mailed>

srivatsan_b said:
Can anybody please explain how sizeof operator is implemented?

Since it's implementation is dependent on the compiler, ask the compiler's
author how he did it.
 
K

Keith Thompson

Malcolm said:
In traditional C it looks like a function but isn't. sizeof(int) resolves to
a compile time constant, which the compiler cna do because it knows the
number of bytes taken for each type.
(You cannot portably replace with a constant in code because int may be 2, 4
or sometimes even a different number of bytes on different machines).

It can look like a function call if you use parentheses, but it's
actually an operator. For example, given "int x;", the expression to
compute the size of x is "sizeof x" (you can use "sizeof(x)", but the
parentheses are superfluous). The parentheses are required if the
argument is a type name, as in "sizeof(int)", but of course a function
can't take a type name as an argument.
 

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

Staff online

Members online

Forum statistics

Threads
473,767
Messages
2,569,571
Members
45,045
Latest member
DRCM

Latest Threads

Top