What is the easiest way to find the sizeof a type without compiling and executing code?

我们两清 提交于 2019-12-20 03:32:10

问题


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))

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.

Is there a faster, less roundabout way to find out what sizes gcc uses for datatypes?


回答1:


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__

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"

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.




回答2:


You can use the 'negative array size' trick that autoconf (see: AC_COMPUTE_INT) uses. That way, you don't need to link or execute code. Therefore, it also works when cross compiling. e.g.,

int n[1 - 2 * !(sizeof(double) == 8)];

fails to compile if: sizeof(double) != 8

The downside is, you might have to pass -DCHECK_SIZE=8 or something similar in the command line, since it might take more than one pass to detect an unusual value. So, I'm not sure if this will be any faster in general - but you might be able to take advantage of it.

Edit: If you are using gcc exclusively, I think @wintermute's comment is probably the best solution.




回答3:


Here are three possible solutions.

The first one will work with any type whose size is less than 256. On my system, it takes about 0.04s (since it doesn't need headers or libraries other than the basic runtime). One downside is that it will only do one at a time, because of the small size of the output channel. Another problem is that it doesn't compensate for slow linking on some systems (notably MinGW):

howbig() {
  gcc -x c - <<<'int main() { return sizeof ('$*'); }' && ./a.out
  echo $?
}

$ time howbig "struct { char c; union { double d; int i[3];};}" 
24

real    0m0.041s
user    0m0.031s
sys     0m0.014s

$ time howbig unsigned long long
8

real    0m0.044s
user    0m0.035s
sys     0m0.009s

If you wanted to be able to do larger types, you could get the size one byte at a time, at the cost of a couple more centiseconds:

howbig2 () 
{ 
    gcc -x c - <<< 'int main(int c,char**v) {
                      return sizeof ('$*')>>(8*(**++v&3)); }' &&
    echo $((0x$(printf %02x $(./a.out 3;echo $?) $(./a.out 2;echo $?) \
                            $(./a.out 1;echo $?) $(./a.out 0;echo $?)) ))
}

$ time howbig2 struct '{double d; long long u[12];}([973])'
101192

real    0m0.054s
user    0m0.036s
sys     0m0.019s

If you are compiling for x86, the following will probably work, although I'm not in a position to test it thoroughly on a wide variety of architectures and platforms. It avoids the link step (notoriously slow on MinGW, for example), by analyzing the compiled assembly output. (It would probably be slightly more robust to analyze the compiled object binary, but I fear that binutils on MinGW are also slow.) Even on Ubuntu, it is significantly faster:

howbig3 () { 
  gcc -S -o - -x c - <<< 'int hb(void) { return sizeof ('$*'); }' |
  awk '$1~/movl/&&$3=="%eax"{print substr($2,2,length($2)-2)}'
}

$ time howbig3 struct '{double d; long long u[12];}([973])'
101192

real    0m0.020s
user    0m0.017s
sys     0m0.004s


来源:https://stackoverflow.com/questions/28332806/what-is-the-easiest-way-to-find-the-sizeof-a-type-without-compiling-and-executin

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!