sizeof operator - Implementing it !!

Hi,

How could i implement my own 'sizeof' operator ?

Thx in advans, Karthik Balaguru

Reply to
karthikbalaguru
Loading thread data ...

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

Reply to
Clifford Heath

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

Reply to
Zara

I think the code following code will works :-)

#include

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; }

Reply to
Dibyendu

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

formatting link

Thx in advans, Karthik Balaguru

Reply to
karthikbalaguru

link

formatting link

#include

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;

}
Reply to
Dibyendu

That's not even close.

Reply to
Hans-Bernhard Bröker

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

Reply to
Hans-Bernhard Bröker

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

Reply to
Hans-Bernhard Bröker

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

Reply to
Dibyendu

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.

Reply to
Clifford Heath

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.
Reply to
CBFalconer

On Mon, 13 Aug 2007 00:22:38 -0700, Dibyendu wrote in comp.arch.embedded:

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

--
Jack Klein
Home: http://JK-Technology.Com
 Click to see the full signature
Reply to
Jack Klein

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.

Reply to
mc

#define my_sizeof(x) sizeof(x)

--
David M. Palmer  dmpalmer@email.com (formerly @clark.net, @ematic.com)
Reply to
David M. Palmer

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 :-).

Reply to
Clifford Heath

ElectronDepot website is not affiliated with any of the manufacturers or service providers discussed here. All logos and trade names are the property of their respective owners.