Cecylia
Cecylia

Reputation: 361

Getting: 'cast to pointer from integer of different size' warning (amd64)

When I compile in and64 platform: I can't seem to figure it out. Here's one of the lines it has trouble with:

static gboolean scan_freq(gpointer data)
{
    static gint start, mom, max;
    gint dir = (gint)(data); /// <- cast to pointer from integer of different size

    if (!max) {
        max = (FREQ_MAX - FREQ_MIN) * STEPS;
    }   

    if (radio_check_station(gtk_adjustment_get_value(adj)/STEPS) || (start > max))  {
        start = mom = 0;
        radio_unmute();
        timeout_id = 0;
        return FALSE;
    }
    if (!mom) {
        mom = gtk_adjustment_get_value(adj);
    }

    if (mom > FREQ_MAX*STEPS) 
        mom = FREQ_MIN*STEPS;
    else if (mom < FREQ_MIN*STEPS)
        mom = FREQ_MAX*STEPS;
    else    
        mom = mom + dir;
    start += 1;
    gtk_adjustment_set_value(adj, mom);

    return TRUE;
}

And:

extern int mom_ps;
void preset_menuitem_activate_cb(GtkMenuItem *menuitem, gpointer user_data)
{
    preset* ps;
    mom_ps = (int)user_data;  /// <- cast to pointer from integer of different size

    g_assert(mom_ps >= 0 && mom_ps < g_list_length(settings.presets));

    ps = (preset*)g_list_nth_data(settings.presets, mom_ps);
    gtk_adjustment_set_value(adj, ps->freq * STEPS);
}

Getting: cast to pointer from integer of different size warning

Upvotes: 0

Views: 1732

Answers (4)

paxdiablo
paxdiablo

Reputation: 882716

It's actually a confusing error message since you seem to be casting away from a pointer.

However, the error is basically complaining that the types are not compatible.

Assuming that you've actually passed an integer as a pointer (Gnome specifically forbids trying to store pointers in an integer type), you should probably be using the actual type conversion macros like GPOINTER_TO_INT?

If instead, that pointer is a pointer to an integer, you should be deferencing it rather than casting it, something like:

gint dir = *((gint*)data);

The fact that it only happens on AMD64 is because that's the sort of platform where pointers and integers would be a different size, one 64-bit, the other 32-bit.

Upvotes: 1

Jens
Jens

Reputation: 72756

It likely means your pointers are 64 bits, while your ints are 32 bits. What do you expect a compiler to do when you assign 64 bits to a 32 bit type? I'd say "scream at me with a warning!" :-)

Upvotes: 0

Tawnos
Tawnos

Reputation: 1887

You should be using GPOINTER_TO_INT: http://developer.gnome.org/glib/unstable/glib-Type-Conversion-Macros.html

When doing this, you must have stored the pointer as an int using its corresponding macro. You should be able to see if the pointer size and the int size are the same using printf("%d %d",sizeof(gpointer),sizeof(int))

Upvotes: 0

myforwik
myforwik

Reputation: 11

Probably, the sizeof gint and gpointer are not the same. Are you sure you know what you are doing? Why are you using a pointer as an int?

If you are are sure that is right, then you need to use an integer the same size as a pointer, probably gint64 or glong instead of gint.

Also the glib maintainers have said "Remember, YOU MAY NOT STORE POINTERS IN INTEGERS. THIS IS NOT PORTABLE IN ANY WAY SHAPE OR FORM." So whatever you are trying to do - its being done wrong.

Upvotes: 1

Related Questions