Thread: converting a 32 to 64 bit app - casting different size pointers

  1. #1
    Registered User digininja's Avatar
    Join Date
    May 2009
    Location
    Sheffield, UK
    Posts
    4

    converting a 32 to 64 bit app - casting different size pointers

    I have the following code:
    Code:
    u_int8_t* ssid = (u_int8_t*)arg;
    frm = ieee80211_add_ssid(frm, ssid + 2, ssid[1]);
    where arg is an int and ieee80211_add_ssid is defined as
    Code:
    static u_int8_t * ieee80211_add_ssid(u_int8_t *frm, const u_int8_t ........id, u_int len)
    This compiles fine on 32 bit but on 64bit I get this warning
    Code:
    warning: cast to pointer from integer of different size
    on the first line.

    I can't quite get my head around what the problem is and how to cast arg into the right type so that it will work and compile. This is just beyond by pointer skills, I feel I should be able to work it out but have stared at it for an hour and not managed to suss it out. Some help please.

  2. #2
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    The lesser problem is that an integer is 32 bits (yes, even on a 64-bit system) but a pointer is 64 bits. The bigger problem was the original design decision to cram a pointer value into an integer via a cast, then going back the other way. The original pointer, being 64 bits, was most likely obliterated completely when it got crammed into an int.

    This is exactly the sort of data abuse that causes problems when doing this kind of port.

    Hopefully you can fix it without a total rewrite.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  3. #3
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    What is the type of arg? If it's an int, it is probably not going to work.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  4. #4
    Registered User digininja's Avatar
    Join Date
    May 2009
    Location
    Sheffield, UK
    Posts
    4
    arg is an int unfortunately.

    The code isn't mine, I'm doning some mods to the madwifi-ng wifi drivers. They work fine on 32 bit but now people want them in 64 bit so I'm trying to fit them in.

    Just so I can get this right in my head, in a 32 bit system ints and pointers are the same size so are interchangable (bad idea but possible)

    In a 64 bit system ints are 32 bits but pointers are 64 bit so when a pointer is put into an int it is truncated and so can't be pulled back out.

    Is that right?

    I can start looking further up the tree to see how arg is created and what happens with it but there is a lot of code and I don't want to rewrite a whole file just to get my small bit to work.

  5. #5
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Correct. The size of integer in 32 and 64-bit code is the same. But pointers (and long, if this is a Linux system) change from 32 to 64-bit. And yes, if you put a 64-bit integer into a 32-pointer, you loose the upper 32 bits... Since almost all 64-bit addresses are above 4GB and below the "top-4GB", the 32-bit value is about as useful as half a 50 dollar bill... Without the other half, it's pointless.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  6. #6
    Registered User digininja's Avatar
    Join Date
    May 2009
    Location
    Sheffield, UK
    Posts
    4
    Thanks for clearing that up. It will save me a good few hours of staring and wondering why things aren't working.

  7. #7
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Probably. It wouldn't be a bad idea to change any variable that is cast from or to an int into a long type.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  8. #8
    Registered User digininja's Avatar
    Join Date
    May 2009
    Location
    Sheffield, UK
    Posts
    4
    I'll have a dig through, see where arg comes from and how many times it is used. If it isn't too many I'll look at changing it to a long, if there are loads I'll just leave my patches at 32 bit.

  9. #9
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by digininja View Post
    I'll have a dig through, see where arg comes from and how many times it is used. If it isn't too many I'll look at changing it to a long, if there are loads I'll just leave my patches at 32 bit.
    I would also ascertain WHY, in the first place, a pointer value is being converted to an integer for storage. This is one of the things that makes C notorious for being unsafe and error-prone.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. An exercise in optimization
    By Prelude in forum Contests Board
    Replies: 10
    Last Post: 04-29-2005, 03:06 PM
  2. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 10:33 AM
  3. polymorphism - 64 bit - va_arg - advanced
    By anonytmouse in forum C Programming
    Replies: 2
    Last Post: 12-03-2003, 09:27 AM
  4. question about 32 bit addresses??
    By newbie02 in forum C++ Programming
    Replies: 3
    Last Post: 09-12-2003, 02:02 PM
  5. 16 bit or 32 bit
    By Juganoo in forum C Programming
    Replies: 9
    Last Post: 12-19-2002, 07:24 AM