不使用sizeof,如何求int占用的字节数

1、使用宏定义的方法

#define MySizeof(Value) (char*)(&Value + 1) - (char*)&Value

其中(char*)&Value返回Value的地址的第一个字节,(char*)&(Value + 1)返回Value的地址的下一个地址的第一个字节,他们之差为它所占的字节数。

2、如下所示

#include <iostream>

using namespace std;

template<class Any>

int LengthofArray(Any* p){

    return int(p + 1) - int(p);

}

猜你喜欢

转载自blog.csdn.net/summer00072/article/details/80918192
今日推荐