I am removing gcc warnings from a legacy code.
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.
Can someone please guide me how to resolve such gcc warnings?
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, changefuncto use theuintptr_ttype (defined instdint.h). It is designed for treating pointers as integers when necessary. (There is alsointptr_tif signed arithmetic is better for some reason, but I generally find the unsigneduintptr_tto be less troublesome.) Preferably,funcshould convert theuintptr_tto a pointer when returning it, so the return type offuncwould be a pointer (to something, perhapssome_structorvoid).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 returned byfunchad the same size as the pointer type, but you are now compiling on a system where the pointer type is larger or less than that integer size.In that case, you must ensure that the computation performed by
funcworks 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 thatfuncshould calculate ever exceeds the maximum value of the integer type it uses? Iffuncis using signed integer types, consider the sign bit too.If you have ensured that the value returned by
funcis correct, then you can use explicit casts, such as:some_struct *ptr = (some_struct *) (intptr_t) func();.