如何在不同的机器上保持C变量类型的固定大小? [英] How to maintain fixed size of C variable types over different machines?

查看:88
本文介绍了如何在不同的机器上保持C变量类型的固定大小?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有所有这些功能。

  ssize_t fuc1(int var1,void * buf,size_t count); 
int func2(char * charPtr,int mode,int dev);
short func3(long var2);问题是C中的数据类型在不同机器(64位和32位)上编译时具有不同的大小。 。这对于即使void *也是如此。由于某些原因。我需要确保这些尺寸在每台机器上都是相同的(64位和32位)。

使用C99,< stdint.h>

code>和 int32_t 等类型。 维基百科的更多详情



没有办法对指针类型这样做,因为根据定义,它们是平台特定的。没有有效的用例,您可能需要它们是一致的,因为它们不是可序列化的。如果你使用指针变量存储指针其他数据(例如,取决于一些标志),使用一些精确的整数类型与指针类型的联合。或者,使用C99 uintptr_t ,并在需要时将其强制转换为指针。


I've all this kind of functions.

ssize_t fuc1(int var1, void *buf, size_t count);
int func2(char *charPtr, int mode, int dev);
short func3( long var2);

problem is that data types in C has different sizes when compiled on different machines(64bit & 32bit). This is true for even void*. For some reasons. I need to ensure that these sizes all are same on every machine(64bit & 32bit). So, how should I modify these ?

解决方案

Use C99, <stdint.h>, and int32_t etc types. More details on Wikipedia.

There's no way to do this for pointer types, because they are, by definition, platform-specific. There is no valid use case in which you could possibly need them to be consistent, because they are not serializable. If you use a pointer variable to store pointer and other data (e.g. depending on some flag), use a union of some exact integral type with a pointer type. Alternatively, use C99 uintptr_t, and cast to pointer when you need to treat it as such.

这篇关于如何在不同的机器上保持C变量类型的固定大小?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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