sizeof operator - Implementing it !!

Do you have a question? Post it now! No Registration Necessary

Translate This Thread From English to

Threaded View
Hi,

How could i implement my own 'sizeof' operator ?

Thx in advans,
Karthik Balaguru


Re: sizeof operator - Implementing it !!
Quoted text here. Click to load it

You can't, it's a compiler built-in. Only the compiler knows how big it's going
to make something.

Re: sizeof operator - Implementing it !!
On Sun, 12 Aug 2007 20:58:44 -0700, karthikbalaguru

Quoted text here. Click to load it

Is this homework? Last hundred times it was asked, it really was
homework...
Please, search comp.lang.c or comp.lang.c++ to take a look at the
responses given

Regards

Re: sizeof operator - Implementing it !!
Quoted text here. Click to load it



I think the code following code will works :-)

#include <stdio.h>

void *var_name = NULL;

#define MY_SIZEOF(var)  (               \
        (unsigned long)((char *)(++((var *)var_name)))  \
        )

int main(void)
{
        printf("%d\n",MY_SIZEOF(char *));
        return 0;
}


Re: sizeof operator - Implementing it !!
Quoted text here. Click to load it

Hi,

Thx for your replies. I did search the net and got some cool info :):)

Interesting stuff to share with you :
 #define sizeof(X)  ((char *) &((X)+1) - (char *) &(X))

I think this method looks coooooooool :):)

Refer the link
http://groups.google.co.in/group/comp.lang.c/browse_thread/thread/f709a9eda383ffc5/?hl=en #

Thx in advans,
Karthik Balaguru


Re: sizeof operator - Implementing it !!
Quoted text here. Click to load it
linkhttp://groups.google.co.in/group/comp.lang.c/browse_thread/thread/f70 ...
Quoted text here. Click to load it

#include <stdio.h>

void *var_name = NULL;

#define MY_SIZEOF(var)  (               \
        (var_name = NULL, (unsigned long)((char *)(++((var
*)var_name)))) \
        )

int main(void)
{
        printf("%d\n",MY_SIZEOF(char *));
        return 0;


}


Re: sizeof operator - Implementing it !!
Quoted text here. Click to load it

That's not even close.

Re: sizeof operator - Implementing it !!
Quoted text here. Click to load it

It doesn't.  For starters, the name of the macro argument is nastily
misleading.

Re: sizeof operator - Implementing it !!
Quoted text here. Click to load it

Your Implementation is pretty good, but it can't take the argument
like: int *, char *, int...etc.


Re: sizeof operator - Implementing it !!
Quoted text here. Click to load it

Bzzzt. That's not executable code. It's a hack to get the compiler to
tell you what it's already prepared to tell you using the sizeof operator.
There is no executable code that can perform a sizeof operator in standard
C.

Re: sizeof operator - Implementing it !!
in comp.arch.embedded:

Quoted text here. Click to load it

Incrementing a null pointer, or applying any arithmetic operation on
it at all, is undefined behavior.  Period.

--
Jack Klein
Home: http://JK-Technology.Com
We've slightly trimmed the long signature. Click to see the full one.
Re: sizeof operator - Implementing it !!
Quoted text here. Click to load it

And if the code works, it isn't an implementation of sizeof -- it's a
backhanded way of getting the compiler to use the built-in sizeof operator
that is already there.



Re: sizeof operator - Implementing it !!
Quoted text here. Click to load it

Exactly. The same trick is widely used to implement offset_of, which doesn't
have an alternate, BTW, and can be quite useful if you're that kind of (ab)user
:-).

Re: sizeof operator - Implementing it !!

Quoted text here. Click to load it

You can't.  Nor would there be any point in trying.


Re: sizeof operator - Implementing it !!

Quoted text here. Click to load it

#define my_sizeof(x) sizeof(x)

--
David M. Palmer   snipped-for-privacy@email.com (formerly @clark.net, @ematic.com)

Re: sizeof operator - Implementing it !!
Quoted text here. Click to load it

You can't, because its results depend on system conditions.  That's
why it is a standard operator in C, i.e. the C implementor does it
for the system on which it runs.

--
 Chuck F (cbfalconer at maineline dot net)
   Available for consulting/temporary embedded and systems.
We've slightly trimmed the long signature. Click to see the full one.

Site Timeline