我遇到了一个问题"如何在不使用C中的sizeof()的情况下确定处理器字长?"在一次采访中,我相信我给出了错误的答案。
我的代码如下:
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;
}
输出答案仅由编译器选项决定。那么,我怎样才能得到正确答案(系统字长)?
如果我将这个问题的一部分从处理器字长改变为'到'操作系统字长'?
答案 0 :(得分:2)
作为@holgac mentioned,long
数据类型的大小始终与机器的本机字大小相同:
“一句话就是机器一次可以处理的数据量。” “处理器的通用寄存器(GPR)的大小等于其字大小。” “此外,C类型long的大小等于字大小,而int类型的大小有时小于字大小的大小”
- Linux内核开发,第17章(第3版,第381页)
但是,indicated by Thomas Matthews可能不适用于字长较短的机器。
要确定编译器上long
的大小,只需使用sizeof(long)
:
int main(void)
{
printf("long is %d bits on this system\n", (int)sizeof(long)*CHAR_BIT);
return 0;
}
另见:
答案 1 :(得分:2)
我认为我的OCD在这里踢了一下,结果如下:
#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
答案 2 :(得分:1)
不允许sizeof
?
此外,稍微改进了实现(不需要复制,更少的循环运行和不分割):
int main(){
int num = 1;
int count = 0;
while(num <<= 8){
count++;
}
printf("System size of int:%d", count+1);
return 0;
}
答案 3 :(得分:0)
作为一个面试问题,在C语言中唯一正确的方法是使用条件编译。条件编译允许对于运行软件的各种平台不同地定义字大小,或者以某种方式识别字大小以便可以从数据库获得正确的大小。由于公司知道产品将在哪些平台上运行,或者他们愿意支持哪些平台,因此可以在编译或运行时选择平台,因此将选择正确的字大小。
任何其他方式来决定单词大小将是平台/系统特定代码或启发式。一种可能的启发式方法是使用指针的大小来表示机器字大小。
word_size = sizeof(void *);
鉴于这是一种启发式的there are platforms for which it will fail。