开发者

sizeof operator in c [closed]

开发者 https://www.devze.com 2023-01-27 18:37 出处:网络
This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time,or an extraordinarily narrow situation that is not generally applic
This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide aud开发者_开发百科ience of the internet. For help making this question more broadly applicable, visit the help center. Closed 10 years ago.

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)
0

精彩评论

暂无评论...
验证码 换一张
取 消