这两种解决方案中哪一种占用的内存更少?
short X; short Y;
pair<short,short> Coords;
在C++2a(GNU)
编译器中使用以下代码:
#include <array>
#include <iostream>
using namespace std;
int main() {
short X;
array<pair<short, short>, 1> Coords;
cout << sizeof(X) << endl;
cout << sizeof(Coords) << endl;
}
似乎是同一回事,1short
是2字节,array
是4字节。
如果我们尝试将地址也设置为相同大小:
cout << sizeof(&X) << endl;
cout << sizeof(&Coords) << endl;
每个8字节。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句