如何解决:"从不同规模和QUOT的整数转换为指针;在C code警告? [英] How to resolve: "cast to pointer from integer of different size" warning in C code?

查看:251
本文介绍了如何解决:"从不同规模和QUOT的整数转换为指针;在C code警告?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是从旧code删除GCC警告。

I am removing gcc warnings from a legacy code.

是否有可能燮preSS通过类型转换转换为指针,从大小不同的整数警告:

Is it possible to suppress the warning "cast to pointer from integer of different size" through typecasting:

example:

some_struct *ptr = func()  // func() returns an integer.

有人能指导我如何解决这样的GCC警告?

Can someone please guide me how to resolve such gcc warnings?

推荐答案

首先,如果你能解决FUNC(允许修改其源),然后解决它。如果它的计算可以用指针来完成,然后做他们的指针和返回指针。有时,有正当的理由与作为整数地址的工作(例如,有对齐问题特别code处理)。在这种情况下,变化FUNC使用使用intptr_t类型(在stdint.h定义)。在使用intptr_t类型是专为治疗指针为整数,必要时。 preferably,FUNC应使用intptr_t返回时将其转换为指针,所以FUNC的返回类型将是一个指针(的东西,或者指针到some_struct,也许指针到无效)。

First, if you can fix func (are allowed to modify its source), then fix it. If its computations can be done with pointers, then do them with pointers and return pointers. Sometimes, there are valid reasons to work with addresses as integers (e.g., dealing with alignment issues in special code). In that case, change func to use the intptr_t type (defined in stdint.h). The intptr_t type is designed for treating pointers as integers when necessary. Preferably, func should convert the intptr_t to a pointer when returning it, so the return type of func would be a pointer (to something, perhaps pointer-to-some_struct, perhaps pointer-to-void).

如果你不能修复FUNC,那么你可以使用强制类型转换告诉你打算这样做,正在执行转换的编译器。然而,这个特殊的错误消息告诉你,你是不是仅仅是一个整数转换为一个指针,但您到其他尺寸(例如8字节)的一个指针转换一个尺寸(例如,四个字节的整数)。它很可能会在code最初编写了一个系统,其中FUNC的整数类型是大小为指针类型一样,但现在你正在编译一个系统,如果指针类型较大,但在整数的大小型保持不变。

If you cannot fix func, then you can use casts to tell the compiler that you intend to do the conversions that are being performed. However, this particular error message is telling you that you are not merely converting an integer to a pointer, but that you are converting an integer of one size (e.g., four bytes) to a pointer of another size (e.g., eight bytes). It is likely this code was originally written for a system where the integer type of func was the same size as the pointer type, but you are now compiling on a system where the pointer type is larger, but the size of the integer type is unchanged.

在这种情况下,你必须确保通过FUNC执行计算在新的架构工作。如果它仅返回一个32位的值,将始终保持正确的值。也就是说,什么都不会被丢失的高32位会丢失?不,那FUNC都不应计算地址超过它使用整数类型的最大值?如果func为使用符号整型,考虑符号位了。

In that case, you must ensure that the computation performed by func works in the new architecture. If it is returning only a 32-bit value, will that always hold the correct value. That is, nothing will be lost by the missing high 32 bits? No address that func should calculate ever exceeds the maximum value of the integer type it uses? If func is using signed integer types, consider the sign bit too.

如果您已经确保通过FUNC返回的值是正确的,那么你可以使用显式转换,如: some_struct * PTR =(some_struct *)(使用intptr_t)FUNC();

If you have ensured that the value returned by func is correct, then you can use explicit casts, such as: some_struct *ptr = (some_struct *) (intptr_t) func();.

这篇关于如何解决:"从不同规模和QUOT的整数转换为指针;在C code警告?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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