17

I have some code that does lots of casting from int to void* and vice-versa (i don't care if it's ugly. I like having generic stuff)

Example:

typedef struct _List {
    long size;
    long mSize; // Max size
    void** elementArray;
}List;

List l;
...
int i = 2;
l.elementArray[i] = i; // Intentional usage of pointer as integer
// Actual size of pointer does not matter

but when i compile i get a bajillion

 warning: cast to 'void *' from smaller integer type 'int' [-Wint-to-void-pointer-cast]

warnings. Is there a flag to tell gcc to not print this specific warning?

I'm compiling with -Wall, so I'm not sure if i can make this go away that easilly

Jean-Luc Nacif Coelho
  • 1,006
  • 3
  • 14
  • 30
  • 2
    Use `uintptr_t` instead. – zwol Mar 31 '14 at 00:27
  • 1
    Also, your example does not show any pointer casts. The warning you're getting *may* be harmless, or it may be the only, ahem, warning you will receive before the program blows up in your face; without seeing the actual code at stake we can't tell. – zwol Mar 31 '14 at 00:28

5 Answers5

37

For the sake of others who are possibly looking for an answer like me:

If you want to not add an extra compilation flag due to the fact that you might be upcasting an int to a void* accidentally somewhere else, you can use the following snippet to force a cast from an int to a void* where you are sure you want it to happen and then the compiler won't bug you about the cast:

#define INT2VOIDP(i) (void*)(uintptr_t)(i)

Of course be sure to include stdint.h, so then you can do the following:

void *val = INT2VOIDP(5);
ThePosey
  • 2,734
  • 2
  • 19
  • 20
DanZimm
  • 2,528
  • 2
  • 19
  • 27
  • Just realized this is like what Quig posted, ah well... If you don't want any + characters mine is here ;P – DanZimm May 07 '15 at 16:29
17

you might want to use 'size_t'.

For example, if you have int i and void *a,

i = (void *) a; will give you that warning

to avoid this add size_t

i = (void *) (size_t) a;

Vasu
  • 171
  • 1
  • 2
15

apparently just take the flag that the compiler gives you and slap a "no" in front of it does the trick!

-Wno-int-to-void-pointer-cast
Jean-Luc Nacif Coelho
  • 1,006
  • 3
  • 14
  • 30
  • 4
    Yes, that is what that is there for. But I would be very hesitant to just slap that switch on there... this particular warning is *very often* indicative of real, honest-to-Ghod, you-can't-do-that-that-way-it-will-explode bugs. – zwol Mar 31 '14 at 00:28
  • I already have a debug make that doesn't ignore warnings. Chill. – Jean-Luc Nacif Coelho Apr 03 '14 at 18:59
  • This is imo not a good solution, because it doesn't solve the possible issue, it only makes the compiler ignore it. Also it turns the switch off globally for the whole program, which might cause problems elsewhere in the code. – Michal Artazov Mar 19 '16 at 19:21
  • My question was how to make the compiler not show the flags. Not how to fix the flags. I am well aware of what I am doing in the code and I chose to ignore it. – Jean-Luc Nacif Coelho Sep 27 '16 at 04:34
4

In some cases doing this would be desired (eg. for the last pointer parameter of glVertexAttribPointer in OpenGL). You should be able to avoid the warning by doing something like this:

#define BUFFER_OFFSET(i) ((char*)NULL+(i))
myFunction(BUFFER_OFFSET(3));
Quig
  • 166
  • 4
0

The warning is telling you that ints are narrower than pointers, so you are losing information when you cast from a pointer to an int. As suggested above, use uintptr_t. That is an integer type that is as wide as a pointer. If you are forced to use int, then you are SOL. "Generic" == broken.

pat
  • 12,587
  • 1
  • 23
  • 52
  • 3
    He is casting from int to void* instead of the inverse. – Roger Sep 02 '15 at 17:23
  • Fine. `int`s are still narrower than pointers, so the compiler has no way to fill in the missing bits. Also, I presume that the pointer didn't start out as an `int`, so they must be a place somewhere else where a pointer is being cast to an `int`. – pat Sep 02 '15 at 17:28