动态确定基于系统的整数类型(c ++) [英] dynamically determine the type of integer based on the system (c++)

查看:143
本文介绍了动态确定基于系统的整数类型(c ++)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个程序,以每32位为单位(即每次4个字节)将数据存储到文件中。我在64位Windows系统中编写代码,但我使用的编译器是32位(mingw32)。在当前系统中,int的长度大小相同,为32位(4bytes)。我目前通过使用g ++重新编译(不更改代码)将代码移植到其他系统。然而,我发现int或long的大小是不同的,取决于系统。这是任何方式(如在头文件中使用宏)来确定整数的大小,以决定是否应该使用int或long作为数据类型的代码?我必须在4种不同类型的系统中重新编译代码,如果我修改代码为每个系统有4个不同的副本,真的很头痛。

I am writing a program to store data to a file on the unit of every 32 bits (i.e. 4 bytes at a time). I wrote the code in 64-bit windows system but the compiler I used is 32 bits (mingw32). In the current system, the size of int an long are the same, 32 bits (4bytes). I am current porting the code to other systems by recompiling with g++ (without changing the code). However, I found that the size of int or long are different and depending on the system. Is that any way (like using a macro in the header file) to determine the size of an integer so to decide if int or long should be used as the data type in the code? I have to recompile the code in 4 different type of system, it is really a headache if I modify the code to have 4 different copies for each system.

推荐答案

你想做的是使用标准类型,例如 int32_t 。此类型始终为32位。我目前在便携式数据库(berkeley db)中使用这些类型,以实现跨系统兼容性。

What you want to do is use the standard types like int32_t. This type is always 32 bits. I currently use these types in a portable database (berkeley db) for cross-system compatibility.

请参阅这里

包含stdint.h

Include stdint.h to get these definitions.

这篇关于动态确定基于系统的整数类型(c ++)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆