468,765 Members | 812 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,765 developers. It's quick & easy.

How to implement sizeof operator in C

Can anyone tell me how to implement sizeof in C language
Jun 3 '08 #1
10 3231
r035198x
13,262 8TB
Can anyone tell me how to implement sizeof in C language
There already is an implemenation of it. Do you want to use it or to create your own?
Jun 3 '08 #2
yes.. I want to write a function which provide the same functionality...
Jun 3 '08 #3
Banfa
9,057 Expert Mod 8TB
Since you can not write a function that takes a type as an argument you will not be able to do what you wish.

You can get the size of a type programatically but the code would have to be specific to the type.

The sizeof operator exists because there is no real other way to get the functionality it provides and it is something that is useful.
Jun 3 '08 #4
oler1s
671 Expert 512MB
sizeof is a compile time operator. Asking how to implement sizeof only makes sense if you are writing your own compiler.
Jun 3 '08 #5
AmberJain
884 Expert 512MB
The link below may help you out more------>

LINK

HOPE THIS HELPS...............

============
AmbrNewlearner
============
Jun 3 '08 #6
JosAH
11,448 Expert 8TB
The link below may help you out more------>

LINK
All it does is show you that a sizeof operator is a compile time operator and
necessarily highly platform dependent. That is/was an interesting thread: I
didn't know that some of my compadres had posted in this forum ...
I think it's just a temporary tap from a usenet feed though.

kind regards,

Jos
Jun 3 '08 #7
r035198x
13,262 8TB
..That is/was an interesting thread: I
didn't know that some of my compadres had posted in this forum ...
A macro that gets the address, adds 1 and subtracts that address would be very close.
Jun 4 '08 #8
JosAH
11,448 Expert 8TB
A macro that gets the address, adds 1 and subtracts that address would be very close.
Only partly; there are two forms of the sizeof operator:

1: sizeof ( type-name )
2: sizeof unary-expression

your suggestion only works for the first alternative but what would the size be
of, say '~(a+b++)'? It can't be done without being part of the compiler and knowing
about the platform this darn thing is executed on.

kind regards,

Jos
Jun 4 '08 #9
r035198x
13,262 8TB
Only partly; there are two forms of the sizeof operator:

1: sizeof ( type-name )
2: sizeof unary-expression

your suggestion only works for the first alternative but what would the size be
of, say '~(a+b++)'? It can't be done without being part of the compiler and knowing
about the platform this darn thing is executed on.

kind regards,

Jos
Hence the "close".
BTW Have you implemented sizeof in .... <starting to get out of topic here so I'll stop>.
Jun 4 '08 #10
JosAH
11,448 Expert 8TB
Hence the "close".
BTW Have you implemented sizeof in .... <starting to get out of topic here so I'll stop>.
Try 'abs'

kind regards,

Jos

ps. sorry for being off-topic.
Jun 4 '08 #11

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

70 posts views Thread by Roy Yao | last post: by
11 posts views Thread by Kai-Uwe Bux | last post: by
31 posts views Thread by Anjali M | last post: by
7 posts views Thread by dam_fool_2003 | last post: by
12 posts views Thread by ozbear | last post: by
15 posts views Thread by Alex Vinokur | last post: by
8 posts views Thread by Mallesh | last post: by
28 posts views Thread by Howard Bryce | last post: by
139 posts views Thread by ravi | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.