Thread: Data Types

  1. #1
    Registered User
    Join Date
    Jan 2008
    Posts
    24

    Data Types

    I know that type "int" is not guaranteed to be a specific size, as well as all of the other primitives, but is there a quick and easy way to dynamically create custom data types that isn't going to break the program when switching to a non-standard platform?

    For example, I like to define char, short, int, long int as int8, int16, int32, int64, respectively, and then only use the minimum type required for all possible values held in the variable (if I know the domain prior to defining it) to minimize wasted space. The problem lies in the fact that I may need a variable to hold 64 bits, and define it as int64, but this will be erroneous on a system that defines "long int" as only 32 bits.

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    I would create typedefs for the different types you want to use. Then, when you change platforms, you only have to update the typedefs to use the proper built-in type for that platform.

    Since you can't force a specific size in a platform independent way, that will be the best option I can think of.

    However, you should ask why you think you need to do this in the first place. Are you sure you would be wasting space? Are you sure that there won't be other performance penalties for not using the default sizes for the particular platform?

  3. #3
    The larch
    Join Date
    May 2006
    Posts
    3,573
    May-be you can use the <cstdint> (or <stdint.h>) headers.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    I believe there are some typedefs stored somewhere in the Windows.h mess. So on Windows you wouldn't have to worry about it.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  5. #5
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    All code I've seen that uses this will have something like:
    Code:
    #if __x86_64__
    typedef long int int64;
    #else
    typedef long long int int64;
    #endif
    Obviously there may be more variants of processors, either combined into a single #if, or using a #if/#elsif/#else branch-tree.

    You can of course also have different include files for different architectures, so you have a "i386/types.h", "x86_64/types.h", "ppc32/types.h", "ppc64/types.h", "arm/types.h", etc, etc.

    If you are just starting on a project, you may want to just define the types once, and then let it be a future problem to solve the actual "how to we make this work for multiple architectures".

    But as stated above, there is no "one solution works for everything" - since different processor architectures and compilers have different "ideas" about what is what size.

    --
    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
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    The Boost library has <boost/cstdint.hpp>, which contains a lot of typedefs.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  7. #7
    Registered User
    Join Date
    Jan 2008
    Posts
    24
    Quote Originally Posted by matsp View Post
    All code I've seen that uses this will have something like:
    Code:
    #if __x86_64__
    typedef long int int64;
    #else
    typedef long long int int64;
    #endif
    Obviously there may be more variants of processors, either combined into a single #if, or using a #if/#elsif/#else branch-tree.

    You can of course also have different include files for different architectures, so you have a "i386/types.h", "x86_64/types.h", "ppc32/types.h", "ppc64/types.h", "arm/types.h", etc, etc.

    If you are just starting on a project, you may want to just define the types once, and then let it be a future problem to solve the actual "how to we make this work for multiple architectures".

    But as stated above, there is no "one solution works for everything" - since different processor architectures and compilers have different "ideas" about what is what size.

    --
    Mats
    That's kind of what I was looking for, I guess. I was really hoping there was something like #ifdef ___64BIT___ that would work for all 64-bit processors, and one for 32-bit, etc.

  8. #8
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by drrcknlsn View Post
    That's kind of what I was looking for, I guess. I was really hoping there was something like #ifdef ___64BIT___ that would work for all 64-bit processors, and one for 32-bit, etc.
    Well, one of the difficulties here is that not all 64-bit architectures/compilers have the same "rules" - for example MS VC++ for x86_64 has "long" as a 32-bit integer, whilst gcc for x86_64 (at least on Linux) has "long" as 64-bit. So just simply saying "the processor is 64-bit" doesn't answer all the questions, if you see what I mean.

    Further, gcc supports "long long", whilst MS VC++ uses "_int64" to indicate a 64-bit integer.

    So it's not just the processor, it's which compiler you are using as well.

    There are probably other compilers with further different standards.

    --
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Extending basic data types.
    By nempo in forum C++ Programming
    Replies: 23
    Last Post: 09-25-2007, 03:28 PM
  2. Replies: 4
    Last Post: 06-14-2005, 05:45 AM
  3. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  4. gcc problem
    By bjdea1 in forum Linux Programming
    Replies: 13
    Last Post: 04-29-2002, 06:51 PM
  5. Using enumerated data types
    By SXO in forum C++ Programming
    Replies: 7
    Last Post: 09-04-2001, 06:26 PM