sizeof operator in c [closed]
please can anybody help me to implement sizeof()
operator in c..
i know the usage .. but i was not able to implement it.
You cannot implement sizeof()
as a library function, it is a compiler intrinsic. Are you writing a compiler?
You can't implement sizeof
in C; it's a basic operator (you can't implement +
either).
You could write a macro that has a limited subset of sizeof
's behavior, by doing something along the lines of:
#define thisIsAHorribleHackDontDoThis(a) \
((size_t)((intptr_t)(&a + 1) - (intptr_t)&a))
but that only works if a
is an lvalue (and it's horrible to behold). sizeof
is not so limited, and that's why you should use it instead of reinventing a wheel that isn't actually round.
Below is the implementation of sizeof operator
#define SZOF(x) (size_t)(((x*)(0))+1)
精彩评论