如何确定 C 中的处理器字长?

How to determine processor word length in C?

我在面试中遇到了一个问题"How to determine processor word length without using sizeof() in C?",我认为我给出了错误的答案。

我的代码如下:

int main(){
    int num = -1;
    int count = 0;    

    unsigned int num_copy = (unsigned int)num;
    while(num_copy >>= 1){
        count++;
    }

    printf("System size of int:%d", (count  + 1)/ 8);

    return 0;
}

输出答案仅由编译器选项决定。那么,我怎样才能得到正确的答案(系统字长)呢?

如果我将此问题的一部分从 'processor word length' 更改为 'operating system word length' 会怎样?

一样,long 数据类型的大小始终与机器的本机字大小相同:

"A word is the amount of data that a machine can process at one time." "The size of a processor’s general-purpose registers (GPRs) is equal to its word size." "Additionally, the size of the C type long is equal to the word size, whereas the size of the int type is sometimes less than that of the word size"

-- Linux Kernel Development, Ch 17 (3rd edition, pg 381)

As 但是,这可能不适用于字长较小的机器。

要在您的编译器上确定 long 的大小,只需使用 sizeof(long):

int main(void)
{
    printf("long is %d bits on this system\n", (int)sizeof(long)*CHAR_BIT);
    return 0;
}

另请参阅:

  • What is CHAR_BIT?

sizeof 不允许吗?

此外,稍微改进了实现(无需复制,循环运行次数减少且无除法):

int main(){
    int num = 1;
    int count = 0;    

    while(num <<= 8){
        count++;
    }

    printf("System size of int:%d", count+1);

    return 0;
}

作为一道面试题,直接用C语言做的唯一正确方法是使用条件编译。条件编译允许为软件 运行 所在的各种平台定义不同的字长,或者以某种方式识别字长,以便可以从数据库中获得正确的大小。由于一家公司知道产品将 运行 在哪些平台上,或者他们愿意支持哪些平台,因此可以在编译或 运行 时选择平台,并选择正确的字长作为结果。

任何其他确定字长的方法要么是 platform/system 特定代码,要么是启发式方法。一种可能的启发式方法是使用指针的大小来表示机器字的大小。

word_size = sizeof(void *);

鉴于这是一种启发式算法,there are platforms for which it will fail

我觉得我的强迫症有点发作了,这是结果:

#include <stdio.h>
#include <limits.h>

#define SIZEOF_CHAR sizeof(char)
#define SIZEOF_INT sizeof(int)
#define SIZEOF_LONG sizeof(long)
#define SIZEOF_POINTER sizeof(void *)

#define NIBBLE_BIT 4
#ifndef CHAR_BIT
#define CHAR_BIT 8    // should have been defined in <limits.h>
#endif
#define INT_BIT (SIZEOF_INT * CHAR_BIT)
#define LONG_BIT (SIZEOF_LONG * CHAR_BIT)
#define POINTER_BIT (SIZEOF_POINTER * CHAR_BIT)

int main(void)
{
  char hexchar[SIZEOF_CHAR * 2 + 1],
       hexint[SIZEOF_INT * 2 + 1],
       hexlong[SIZEOF_LONG * 2 + 1],
       hexpointer[SIZEOF_POINTER * 2 + 1];
  int strlen_hexchar, strlen_hexint, strlen_hexlong, strlen_hexpointer;

  strlen_hexchar = sprintf(hexchar, "%x", (unsigned char)-1);
  strlen_hexint = sprintf(hexint, "%x", (unsigned int)-1);
  strlen_hexlong = sprintf(hexlong, "%x", (unsigned long)-1l);
  strlen_hexpointer = sprintf(hexpointer, "%p", (void*)-1l);

  printf("#define SIZEOF_CHAR sizeof(char)                // %2d\n", SIZEOF_CHAR);
  printf("#define SIZEOF_INT sizeof(int)                  // %2d\n", SIZEOF_INT);
  printf("#define SIZEOF_LONG sizeof(long)                  // %2d\n", SIZEOF_LONG);
  printf("#define SIZEOF_POINTER sizeof(void *)           // %2d\n", SIZEOF_POINTER);

  printf("\n");

  printf("#define NIBBLE_BIT %-2d\n", NIBBLE_BIT);
  printf("#ifndef CHAR_BIT\n");
  printf("#define CHAR_BIT %-2d   // should have been defined in <limits.h>\n", CHAR_BIT);
  printf("#endif\n");
  printf("#define INT_BIT (SIZEOF_INT * CHAR_BIT)         // %2d\n", INT_BIT);
  printf("#define INT_LONG (INT_LONG * CHAR_BIT)         // %2d\n", LONG_BIT);
  printf("#define POINTER_BIT (SIZEOF_POINTER * CHAR_BIT) // %2d\n", POINTER_BIT);

  printf("\n");

  printf("\nTest setup...\n");
  printf("\n");

  printf("char hexchar[CHAR_BIT * SIZEOF_CHAR + 1],\n");
  printf("    hexint[CHAR_BIT * SIZEOF_INT + 1],\n");
  printf("    hexlong[CHAR_BIT * SIZEOF_LONG + 1],\n");
  printf("    hexpointer[CHAR_BIT * SIZEOF_POINTER + 1];\n");
  printf("int strlen_hexchar, strlen_hexint, strlen_hexlong, strlen_hexpointer;\n");
  printf("\n");
  printf("strlen_hexchar = sprintf(hexchar, \"%%x\", (unsigned char)-1);\n//    returned %d, hexchar populated with \"%s\"\n",
      strlen_hexchar, hexchar);
  printf("strlen_hexint = sprintf(hexint, \"%%x\", (unsigned int)-1);\n//    returned %d, hexint populated with \"%s\"\n",
      strlen_hexint, hexint);
  printf("strlen_hexlong = sprintf(hexlong, \"%%x\", (unsigned long)-1);\n//    returned %d, hexlong populated with \"%s\"\n",
      strlen_hexlong, hexlong);
  printf("strlen_hexpointer = sprintf(hexpointer, \"%%x\", (void*)-1l);\n//    returned %d, hexpointer populated with \"%s\"\n",
      strlen_hexpointer, hexpointer);

  printf("\n\nTest results...\n");
  printf("\n");

  if (SIZEOF_CHAR * 2 == strlen_hexchar) {
    printf("testing (SIZEOF_CHAR * 2 == strlen_hexchar) [pass]\n");
  } else {
    printf("testing (SIZEOF_CHAR * 2 == strlen_hexchar) [fail]\n");
    printf("  (%d != $d)\n", SIZEOF_CHAR * 2, strlen_hexchar);
  }

  if (SIZEOF_INT * 2 == strlen_hexint) {
    printf("testing (SIZEOF_INT * 2 == strlen_hexint) [pass]\n");
  } else {
    printf("testing (SIZEOF_INT * 2 == strlen_hexint) [fail]\n");
    printf("  (%d != $d)\n", SIZEOF_INT * 2, strlen_hexint);
  }

  if (SIZEOF_LONG * 2 == strlen_hexlong) {
    printf("testing (SIZEOF_LONG * 2 == strlen_hexlong) [pass]\n");
  } else {
    printf("testing (SIZEOF_LONG * 2 == strlen_hexlong) [fail]\n");
    printf("  (%d != $d)\n", SIZEOF_LONG * 2, strlen_hexlong);
  }

  if (SIZEOF_POINTER * 2 == strlen_hexpointer) {
    printf("testing (SIZEOF_POINTER * 2 == strlen_hexpointer) [pass]\n");
  } else {
    printf("testing (SIZEOF_POINTER * 2 == strlen_hexpointer) [fail]\n");
    printf("  (%d != $d)\n", SIZEOF_POINTER * 2, strlen_hexpointer);
  }

  printf("\n");

  if (CHAR_BIT == strlen_hexchar * NIBBLE_BIT) {
    printf("testing (CHAR_BIT == strlen_hexchar * NIBBLE_BIT) [pass]\n");
  } else {
    printf("testing (CHAR_BIT == strlen_hexchar * NIBBLE_BIT) [fail]\n");
    printf("  (%d != $d)\n", CHAR_BIT, strlen_hexchar * NIBBLE_BIT);
  }

  if (INT_BIT == strlen_hexint * NIBBLE_BIT) {
    printf("testing (INT_BIT == strlen_hexint * NIBBLE_BIT) [pass]\n");
  } else {
    printf("testing (INT_BIT == strlen_hexint * NIBBLE_BIT) [fail]\n");
    printf("  (%d != $d)\n", INT_BIT, strlen_hexint * NIBBLE_BIT);
  }

  if (LONG_BIT == strlen_hexlong * NIBBLE_BIT) {
    printf("testing (LONG_BIT == strlen_hexlong * NIBBLE_BIT) [pass]\n");
  } else {
    printf("testing (LONG_BIT == strlen_hexlong * NIBBLE_BIT) [fail]\n");
    printf("  (%d != $d)\n", LONG_BIT, strlen_hexlong * NIBBLE_BIT);
  }

  if (POINTER_BIT == strlen_hexpointer * 4) {
    printf("testing (POINTER_BIT == strlen_hexpointer * NIBBLE_BIT) [pass]\n");
  } else {
    printf("testing (POINTER_BIT == strlen_hexpointer * NIBBLE_BIT) [fail]\n");
    printf("  (%d != $d)\n", POINTER_BIT, strlen_hexpointer * NIBBLE_BIT);
  }

  printf("\n");

  if ((int)(SIZEOF_POINTER * CHAR_BIT) == strlen_hexpointer * NIBBLE_BIT) {
    printf("testing ((int)(SIZEOF_POINTER * CHAR_BIT) == strlen_hexpointer * NIBBLE_BIT) [pass]\n");
  } else {
    printf("testing ((int)(SIZEOF_POINTER * CHAR_BIT) == strlen_hexpointer * NIBBLE_BIT) [fail]\n");
    printf("  (%d != %d)\n", (int)(SIZEOF_POINTER * CHAR_BIT), strlen_hexpointer * NIBBLE_BIT);
  }

  printf("\nConclusion: this machine word is %d bytes and %d bits\n", SIZEOF_POINTER * 8 / CHAR_BIT, strlen_hexpointer * NIBBLE_BIT);
  if ((int)(SIZEOF_POINTER * CHAR_BIT) != strlen_hexpointer * NIBBLE_BIT) {
    printf(" * however this conclusion did not pass the (int)(SIZEOF_POINTER * 8 / CHAR_BIT) == strlen_hexpointer * NIBBLE_BIT) test\n");
  }

  return 0;
}

这段代码的输出在我的机器上显示如下:

$ sizeofword.exe # from mingw32 shell on windows7
#define SIZEOF_CHAR sizeof(char)                //  1
#define SIZEOF_INT sizeof(int)                  //  4
#define SIZEOF_LONG sizeof(long)                  //  4
#define SIZEOF_POINTER sizeof(void *)           //  4

#define NIBBLE_BIT 4
#ifndef CHAR_BIT
#define CHAR_BIT 8    // should have been defined in <limits.h>
#endif
#define INT_BIT (SIZEOF_INT * CHAR_BIT)         // 32
#define INT_LONG (INT_LONG * CHAR_BIT)         // 32
#define POINTER_BIT (SIZEOF_POINTER * CHAR_BIT) // 32


Test setup...

char hexchar[CHAR_BIT * SIZEOF_CHAR + 1],
    hexint[CHAR_BIT * SIZEOF_INT + 1],
    hexlong[CHAR_BIT * SIZEOF_LONG + 1],
    hexpointer[CHAR_BIT * SIZEOF_POINTER + 1];
int strlen_hexchar, strlen_hexint, strlen_hexlong, strlen_hexpointer;

strlen_hexchar = sprintf(hexchar, "%x", (unsigned char)-1);
//    returned 2, hexchar populated with "ff"
strlen_hexint = sprintf(hexint, "%x", (unsigned int)-1);
//    returned 8, hexint populated with "ffffffff"
strlen_hexlong = sprintf(hexlong, "%x", (unsigned long)-1);
//    returned 8, hexlong populated with "ffffffff"
strlen_hexpointer = sprintf(hexpointer, "%x", (void*)-1l);
//    returned 8, hexpointer populated with "FFFFFFFF"


Test results...

testing (SIZEOF_CHAR * 2 == strlen_hexchar) [pass]
testing (SIZEOF_INT * 2 == strlen_hexint) [pass]
testing (SIZEOF_LONG * 2 == strlen_hexlong) [pass]
testing (SIZEOF_POINTER * 2 == strlen_hexpointer) [pass]

testing (CHAR_BIT == strlen_hexchar * NIBBLE_BIT) [pass]
testing (INT_BIT == strlen_hexint * NIBBLE_BIT) [pass]
testing (LONG_BIT == strlen_hexlong * NIBBLE_BIT) [pass]
testing (POINTER_BIT == strlen_hexpointer * NIBBLE_BIT) [pass]

testing ((int)(SIZEOF_POINTER * CHAR_BIT) == strlen_hexpointer * NIBBLE_BIT) [pass]

Conclusion: this machine word is 4 bytes and 32 bits