c++ - 使用C编译器,如何设置使得int字长与系统字长相同
迷茫
迷茫 2017-04-17 11:52:17
0
6
1009

比如在处理问题:“如何不使用sizeof()用C程序获得系统字长?”

即便在64位机上,使用过的C编译器也会将int编译为32位。那么如何在C中设置,使得int编译字长和系统字长相同呢?

迷茫
迷茫

业精于勤,荒于嬉;行成于思,毁于随。

reply all(6)
迷茫

When compiled with GCC on Linux, the long of 32-bit machines is 4 bytes, and the long of 64-bit machines is 8 bytes.

I searched for the definition of macro INT_MAX on VS. It seems that there is only one place, which is 2147483647, so there should be no way to set it. I can only define a type myself.

And I learned that usually some projects do not use default types directly, but redefine a set of types for use. For example, I can define a set of types like this:

#ifdef m32
typedef int SpacelanInt;
typedef float SpacelanFloat;
#else
typedef long int SpacelanInt;
typedef double SpacelanFloat;
#endif
Ty80

You cannot "set" the word length of int. This thing is not something that can be set in the first place.
But in most C implementations, the word length of long is equal to the machine word length, provided that you generate a "native binary", such as generating a 64-bit program in a 64-bit system.
In addition, the word length of pointer/intptr_t/uintptr_t is generally equal to the machine word length.

Of course, this is not the case on microcontrollers such as 51 or in ancient 16-bit systems

伊谢尔伦

The data models used on different platforms (x86, x64, etc.) and different compilers are different. For details, please refer to:
http://en.wikipedia.org/wiki/64-bit_computing# 64-bit_data_models

In addition, if you want to get a data structure with the same machine word length, you can use size_t

左手右手慢动作

I can’t configure to generate 64-bit programs using VC 2010, but others have this option. But now that I think about it, this option is meaningless. Most people have 32-bit systems, even for the little bit of 64-bit. I guess the performance improvement is almost the same as 32-bit

__int64 j;

http://baike.baidu.com/item/int64

刘奇

Generally, the header files that come with the compiler include typedefs such as __int32, int32, int32_t, etc., which can ensure that it is 32-bit. However, if the code crosses the compiler, there will be problems, and you need to package it yourself.

伊谢尔伦
#include <stdint.h>

There is intptr_t in it

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template