Implementing

C

cogno_byte

Hi all !!
I need to know that in C how to implement sizeof() without using the
keyword?
The implementation should be generic.
e.g
malloc(sizeof(struct));

Here the sizeof() returns the number of bytes reserved and is a
reserved keyword which calls a function through the library function.
But i want to implement the working of sizeof() without using the
keyword.
Hope all of ya got my Question.
Hoping to read from all of you soon
Thnx in advance.
 
K

Keith Thompson

cogno_byte said:
I need to know that in C how to implement sizeof() without using the
keyword?

Why do you want to do this? It's like asking how to drive a nail
without using a hammer.

Just use sizeof().

If you really do have a reason to want to do this, it's been asked
before. Check the archives.
 
R

Richard Heathfield

Keith Thompson said:
Just use sizeof().

Better still, use sizeof - which does not require its operand to be
parenthesised (unless it's a type name).
 
R

Richard Heathfield

cogno_byte said:
Here the sizeof() returns the number of bytes reserved

No, it doesn't.

"The sizeof operator yields the size (in bytes) of its operand, which may be
an expression or the parenthesized name of a type. The size is determined
from the type of the operand, which is not itself evaluated. The result is
an integer constant." - C89 3.3.3.4
and is a
reserved keyword which calls a function through the library function.

Chapter and verse, please. I know of no requirement for sizeof to call a
function.
 
C

CBFalconer

cogno_byte said:
I need to know that in C how to implement sizeof() without using
the keyword?
The implementation should be generic.
e.g
malloc(sizeof(struct));

Here the sizeof() returns the number of bytes reserved and is a
reserved keyword which calls a function through the library function.
But i want to implement the working of sizeof() without using the
keyword.

Can't be done. That is why it is supplied by the system, whose
designer is allowed to use non-portable things to implement it.

--
"The power of the Executive to cast a man into prison without
formulating any charge known to the law, and particularly to
deny him the judgement of his peers, is in the highest degree
odious and is the foundation of all totalitarian government
whether Nazi or Communist." -- W. Churchill, Nov 21, 1943
 
T

Thad Smith

cogno_byte said:
I need to know that in C how to implement sizeof() without using the
keyword?

Your sentence makes no sense. Please use proper English.

sizeof is an operator, not a function, in C. For all objects except
variable length arrays the compiler knows from the declaration how large
the object or type is and gives that value as the result of the
operator. The way it is implemented is part of the compilation process,
with access to the symbol table, not a runtime operation. VLAs are
handled somewhat differently, since their size is not constant.
The implementation should be generic.
e.g
malloc(sizeof(struct));

Here the sizeof() returns the number of bytes reserved and is a
reserved keyword which calls a function through the library function.
But i want to implement the working of sizeof() without using the
keyword.

You could design your own language with your own keywords and implement
a compiler for it.
 

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,755
Messages
2,569,537
Members
45,022
Latest member
MaybelleMa

Latest Threads

Top