什么是找到一个类型的sizeof没有编译和执行code中的最简单的方法? [英] What is the easiest way to find the sizeof a type without compiling and executing code?
问题描述
我写了一个bash脚本来确定海湾合作委员会的数据类型(如
的大小输出各自的大小 ./ sizeof的INT双
INT
和双击
)通过包装每个参数在以下 p()
宏,然后编译和运行code。
I wrote a bash script to determine the size of gcc
's datatypes (e.g. ./sizeof int double
outputs the respective sizes of int
and double
) by wrapping each of its arguments in the following P()
macro and then compiling and running the code.
#define P(x) printf("sizeof(" #x ") = %u\n", (unsigned int)sizeof(x))
问题是,这是相对缓慢的(需要整整一秒!),尤其是联步骤(因为有 -c
或编译-S
花费几乎没有时间,所以没有运行输出二进制)。一秒是不是真的由本身慢,但如果我使用其他脚本这个脚本,它会积少成多。
The problem is that this is relative slow (it takes a whole second!), especially the linking step (since compiling with -c
or -S
takes virtually no time, and so does running the outputted binary). One second is not really that slow by itself, but if I were to use this script in other scripts, it would add up.
有没有办法找到一个更快,更迂回的方式是什么尺寸 GCC
的数据类型使用?
Is there a faster, less roundabout way to find out what sizes gcc
uses for datatypes?
推荐答案
可以实现只能使用GCC的preprocessor标准类型的功能。对于标准的类型有predefined宏:
You can achieve the functionality for standard types using the GCC's preprocessor only. For standard types there are predefined macros:
__SIZEOF_INT__
__SIZEOF_LONG__
__SIZEOF_LONG_LONG__
__SIZEOF_SHORT__
__SIZEOF_POINTER__
__SIZEOF_FLOAT__
__SIZEOF_DOUBLE__
__SIZEOF_LONG_DOUBLE__
__SIZEOF_SIZE_T__
__SIZEOF_WCHAR_T__
__SIZEOF_WINT_T__
__SIZEOF_PTRDIFF_T__
因此,通过使用code这样的:
So, by using code like the following:
#define TYPE_TO_CHECK __SIZEOF_INT__
#define VAL_TO_STRING(x) #x
#define V_TO_S(x) VAL_TO_STRING(x)
#pragma message V_TO_S(TYPE_TO_CHECK)
#error "terminate"
您将能够获得 __ __ SIZEOF_INT
从preprocessor本身的价值,甚至没有开始编译。在你的脚本,你可以定义 TYPE_TO_CHECK
(用 -D
),以任何你需要并将其传递给GCC。当然,你会得到一些垃圾输出,但我相信你可以面对这一切。
you will be able to get the value of __SIZEOF_INT__
from the preprocessor itself without even starting the compilation. In your script you can define the TYPE_TO_CHECK
(with -D
) to whatever you need and pass it to gcc. Of course you will get some junk output, but I believe you can deal with that.
这篇关于什么是找到一个类型的sizeof没有编译和执行code中的最简单的方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!