How to implement sizeof operator

M

Mallesh

Can anybody tell me how sizeof operator internally implemented. In my
project i want to implemnent my own sizeof operator function (like
mysizeof). How i can write the code? Please help me.
 
I

Ian Collins

Mallesh said:
Can anybody tell me how sizeof operator internally implemented. In my
project i want to implemnent my own sizeof operator function (like
mysizeof). How i can write the code? Please help me.
Look back through this group's archives, this has been asked before.
 
E

Eric Sosman

Mallesh said:
Can anybody tell me how sizeof operator internally implemented. In my
project i want to implemnent my own sizeof operator function (like
mysizeof). How i can write the code? Please help me.

It's implemented the same way the `(int)' operator
is implemented: By the compiler.
 
?

=?ISO-8859-1?Q?=22Nils_O=2E_Sel=E5sdal=22?=

Mallesh said:
Can anybody tell me how sizeof operator internally implemented. In my
project i want to implemnent my own sizeof operator function (like
mysizeof). How i can write the code? Please help me.

Look at the Type* typ(int et, Type *d); function at,
http://cm.bell-labs.com/sources/plan9/sys/src/cmd/cc/sub.c

It distributes the size of types based on an array , ewidth[et],
which is just a table for the various sizes of types for the
current platform and assigns that to the type..

(Ofcourse you must have some way of deducing the type of
operands and expressions, this code is part of Ken Thompsons
C compiler. C compilers are good at doing just that.)
 
K

Kenneth Brody

Mallesh said:
Can anybody tell me how sizeof operator internally implemented.

"Magic".

Since it is the compiler that determines the layout and size of any
type (whether built-in, or user-defined), it knows the exact size of
any and all types.
In my
project i want to implemnent my own sizeof operator function (like
mysizeof).

Why? Is there some reason, aside from "my instructor told me to",
to not use the sizeof operator, which was designed specifically for
this purpose?
How i can write the code? Please help me.

This comes up every now and then in this group. (Perhaps the
teacher assigns this "problem" several times a year?) Check the
archives for numerous "solutions".

--
+-------------------------+--------------------+-----------------------+
| Kenneth J. Brody | www.hvcomputer.com | #include |
| kenbrody/at\spamcop.net | www.fptech.com | <std_disclaimer.h> |
+-------------------------+--------------------+-----------------------+
Don't e-mail me at: <mailto:[email protected]>
 
A

Ancient_Hacker

Mallesh said:
Can anybody tell me how sizeof operator internally implemented. In my
project i want to implemnent my own sizeof operator function (like
mysizeof). How i can write the code? Please help me.

There are two major cases:

(1) You have a variable or type name and at *compile time* you need
the size of the variable or type. You can "sort of" get a number
that's loosely related to the size by trickery, but not recommended by
purists. Hint: macros can declare variables. Variables are often, but
not always, snuggled together in memory.

(2) You are passed an arbitrary variable address at *run time* and you
want to find the size of the variable. Not doable except in very
special circumstances, such as the variable was allocated by malloc()
and you have a malloc() hook.

But in general this question is better asked in some other group, this
group is for purists, and what you're asking is in no way "pure".
 
P

pete

(2) You are passed an arbitrary variable address at
*run time* and you
want to find the size of the variable. Not doable except in very
special circumstances, such as the variable was allocated by malloc()
and you have a malloc() hook.

If you had an operation that could tell you
how many bytes were allocated for an object by malloc,
then that operation wouldn't be doing what sizeof does,
because sizeof can't do that.
 
R

Richard Bos

Ancient_Hacker said:
There are two major cases:

(1) You have a variable or type name and at *compile time* you need
the size of the variable or type. You can "sort of" get a number
that's loosely related to the size by trickery, but not recommended by
purists. Hint: macros can declare variables. Variables are often, but
not always, snuggled together in memory.

Then you're thinking of the wrong tricks.

If you have an object, you can, not sort of, but definitely, get the
size of that object in bytes, without resorting to any non-ISO or
system-dependent hacks. Hint: [1].
If you have a type, you can almost equally simply get the size of that
type, by declaring a single object of that type, and then resorting to
the previous trick. The real snag here will be that you will have to
find a variable name that is safe to declare in this macro. You can find
such an identifier with almost, but not quite, 100% certainty.

What you cannot do in ISO C is declare a macro which does both the
object case _and_ the type case. The preprocessor doesn't know the
difference between foo and foo, where the first foo was declared using
int foo; and the second using typedef int foo;.
But in general this question is better asked in some other group, this
group is for purists, and what you're asking is in no way "pure".

No, at compile-time, it can be done in pure ISO C. The real problem with
the question is: why? Nobody who has access to sizeof itself (and that
is everybody who uses ISO C) should need to fake it.

Richard
 
M

Mallesh

Nils said:
Mallesh said:
Can anybody tell me how sizeof operator internally implemented. In my
project i want to implemnent my own sizeof operator function (like
mysizeof). How i can write the code? Please help me.

Look at the Type* typ(int et, Type *d); function at,
http://cm.bell-labs.com/sources/plan9/sys/src/cmd/cc/sub.c

It distributes the size of types based on an array , ewidth[et],
which is just a table for the various sizes of types for the
current platform and assigns that to the type..

(Ofcourse you must have some way of deducing the type of
operands and expressions, this code is part of Ken Thompsons
C compiler. C compilers are good at doing just that.)

Thanks
Nils O. Selåsdal
 

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

Forum statistics

Threads
473,770
Messages
2,569,586
Members
45,097
Latest member
RayE496148

Latest Threads

Top