我知道结构的大小在编译时是已知的,因此应该可以在编程期间找到结构的大小。我怎样才能做到这一点?
更具体:
我有一个结构说:
struct mystruct
{
int a;
char b;
float c[100];
}
我可以在代码中编写此行并运行应用程序,然后查看此结构的大小:
int size=sizeof(mystruct);
cout<<"size is="<<size<<endl;
但这涉及向我的应用程序中添加一些代码并运行它。
Visual Studio IDE有什么方法可以帮助我找到此结构的大小(例如,将光标放在该结构上并按一个键!)
最佳答案
Intellisense可以告诉您这一点。例子:
template <size_t S> class Sizer { };
int x;
Sizer<sizeof(x)> foo;
如果将鼠标悬停在
foo
上,它将显示4Ui64
-x
的大小为4。Ui64
后缀是因为size_t
是Unsigned,Integral和64位。由于它使用Intellisense,因此您无需编译代码。您可以将Sizer
放入stdafx.h
预编译头中。