Connecting Tech Pros Worldwide Forums | Help | Site Map

Implementing

cogno_byte
Guest
 
Posts: n/a
#1: Jan 29 '06
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.


Thad Smith
Guest
 
Posts: n/a
#2: Jan 29 '06

re: Implementing


cogno_byte wrote:
[color=blue]
> I need to know that in C how to implement sizeof() without using the
> keyword?[/color]

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.
[color=blue]
> 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.[/color]

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

--
Thad
Keith Thompson
Guest
 
Posts: n/a
#3: Jan 29 '06

re: Implementing


"cogno_byte" <b.rocks.domain@gmail.com> writes:[color=blue]
> I need to know that in C how to implement sizeof() without using the
> keyword?[/color]

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.

--
Keith Thompson (The_Other_Keith) kst-u@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Richard Heathfield
Guest
 
Posts: n/a
#4: Jan 29 '06

re: Implementing


Keith Thompson said:
[color=blue]
> Just use sizeof().[/color]

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


--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)
Richard Heathfield
Guest
 
Posts: n/a
#5: Jan 29 '06

re: Implementing


cogno_byte said:
[color=blue]
> Here the sizeof() returns the number of bytes reserved[/color]

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
[color=blue]
> and is a
> reserved keyword which calls a function through the library function.[/color]

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

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)
CBFalconer
Guest
 
Posts: n/a
#6: Jan 29 '06

re: Implementing


cogno_byte wrote:[color=blue]
>
> 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.[/color]

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


Jack Klein
Guest
 
Posts: n/a
#7: Jan 29 '06

re: Implementing


On 28 Jan 2006 22:35:43 -0800, "cogno_byte" <b.rocks.domain@gmail.com>
wrote in comp.lang.c:
[color=blue]
> Hi all !!
> I need to know that in C how to implement sizeof() without using the
> keyword?[/color]

Use COBOL.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://c-faq.com/
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html
Closed Thread