Migrating to Different Compiler

Discussion in 'Programmer's Corner' started by Gaurav Pathak, May 11, 2015.

  1. Gaurav Pathak

    Thread Starter New Member

    May 11, 2015
    Hello Experts,

    I have one doubt.
    If I have compiled a program for 32-bit processor using 32-bit compiler, in which the size of integer in 4-bytes then, how can I port the same code to 64-bit processor using 64-bit compiler without changing the size of integer.
    I have read that size of data types is compiler depedent!!
  2. ErnieM

    AAC Fanatic!

    Apr 24, 2011
    How easy this is depends on how you defined the variable types.

    Since you asked the question I assume you just used the standard C "int" as the variable type. As you indicate this may be a problem going to a different platform where the size of an int may differ.

    The trend today is to define your own type with a name of say int_16 that is equated to a 16 bit integer. That way just one file containing these definitions need be changed when changing platforms.

    To change things now you need to wade thru your code and change all these definitions. A search and replace can accomplish much of this, though I would inspect each change as it is made to insure just the things you wish to change are all that is changed, and not other tokens that happen to conain the same string.
  3. WBahn


    Mar 31, 2012
    I used to define my own specific-sized types and had test code to check if they actually mapped correctly for a given compiler (and if they test failed it would tell me immediately what definitions needed tweaking).

    But the better way, if your compiler is C99 compliant (or at least is in this regard, which many pre-C99 compilers already were by extension) is to use the types defined in <stdint.h>. These include types that are guaranteed to be an exact size or to be at least a minimum size as well as other useful categories. If a particular type is not in that implementation, then it won't be in the header and you'll get a syntax error right off the bat.