今天群里讨论了sizeof()函数的实现.下面用了两个宏实现了这功能.
- #define w_sizeof_a(T) ( ( long )( ( T* )0 + 1 ) )
- #define w_sizeof_b(T) ( ( long )( &T + 1 ) - ( long )&T )
- #include <iostream>
- using namespace std;
- int main()
- {
- int a[100][100][10];
- cout<<w_sizeof_a(int)<<endl;
- cout<<w_sizeof_a(int*)<<endl;
- cout<<w_sizeof_a(char)<<endl;
- cout<<w_sizeof_a(float)<<endl;
- cout<<w_sizeof_a(double)<<endl;
- cout<<w_sizeof_b(a)<<endl;
- return 0;
- }
实现是利用了指针加一,跳过的字节数来实现.