c - 将 32 位 C 源代码移植到 64 位

标签 c 32bit-64bit gcc-warning

我尝试移植到 64 位的 C 源代码在 32 位环境中运行时没有任何警告。当我在64位Linux环境下使用编译gcc(Ubuntu 4.4.1-4ubuntu9)4.4.1进行编译时,主要显示以下警告:

warning: cast to pointer from integer of different size

以上警告最多。我使用了 uintptr_t 类型,大部分警告都被删除了。我可以使用 uintptr_tint/unsigned int 类型更改为 64 位有利的类型。但如何更改以下类型以兼容 64 位:

typedef void*  POINTER;

我更改了以下代码:

typedef unsigned int    ABC; 

进入

typedef uintptr_t ABC

我收到以下警告:

warning: passing argument 2 of ‘function’ from   incompatible pointer type
note: expected ‘ABC *’ but argument is of type ‘unsigned int *’

此外,将类型 def 更改为 uintptr_t (之前的 int 或 unsigned int)后,我遇到了大多数警告,如下所示:

warning: inlining failed in call to ‘abc_StringCopy’: call is unlikely and code size would grow

tptp_StringCopy函数如下:

static __inline__ char* abc_StringCopy(void)
{
  char *copy;
  copy = (char*)Malloc(yyleng+1);
  strcpy(copy, yytext);
  return copy;

如何消除这些警告?

最佳答案

unsigned int 在 64 位系统上不能与 uintptr_t 互换。 intunsigned int 在 64 位机器上仍然是 32 位值,但 uintptr_t 变成了 64 位类型。该类型之所以称为 uintptr_t 是因为该类型是与指针宽度相同的无符号整数值。这意味着 uintptr_t 在 32 位计算机上为 32 位宽,但在 64 位计算机上变为 64 位宽。

在您的代码中,这意味着,通过您的 typedef 更改,在 64 位计算机上 NAT* 是指向 64 位变量的 64 位指针,但 unsigned int* 是一个指向 32 位变量的 64 位指针。

clause_ComputeSplitFieldAddress 仍然需要 unsigned int* 参数。

关于c - 将 32 位 C 源代码移植到 64 位,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2393978/

相关文章:

c - 在 C 中设置 TCP 接收窗口并在 Linux 中使用 tcpdump

c - 错误 : expected ';' before number constant

C strcat - 警告 : passing arg 2 of `strcat' makes pointer from integer without a cast

winapi - 为什么Windows 64仍然使用user32.dll等?

c++ - 为什么将除法改为右移时会发生有符号溢出?

c - 如何在c编程中分隔日期 c 日期函数

c++ - 32 位和 64 位程序可以使用 MSMQ 进行通信吗?

operating-system - 32 位应用程序在 64 位操作系统上运行得更快还是更慢?

c++ - gcc 4.9.2 -Wmissing-field-initializers 中的错误?

c - 没有成员编译错误