Thread: Variable types

  1. #1
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853

    Variable types

    Is there a way to actually set a variable size?
    Like if for whatever reason you want a 12byte integer. Can you do that?
    If you can, is there a drawback in performance? (meaning the system would do it in an indirect way)

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    You can check stdint.h to see if a int96_t type is defined by your compiler. If not you could try to fake it I suppose, with an array of some kind. I strongly doubt that you have a 12-byte register that could hold a variable to work with it (add things to it, multiply it by something, et cetera), although I suppose you may well have a 16-byte register around somewhere, depending on your chip.

  3. #3
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853
    Interesting what you say about the registers. Never thought of that. So, I have to ask what about the 12byte long double then?

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Mmm. I think the x86 chips have an 80-bit register for doing floating-point math, so long double is often that size. I don't know of any larger long doubles.

  5. #5
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    You can't "set" the size of a variable. The compiler (and this varies not just between different compiler vendors, but also based on the processor architecture and runtime models - e.g. a processor that supports 32 and 64-bit models would potentially support different size built-in types in the two different modes) will have a set size for a it's built-in types.

    If you need types that are BIGGER than the biggest supported size, then you have to combine multiple built-in types to make the full value. For example, you can use 3 of the 32-bit integers to make a 96-bit value. You'd obviously have to use the "big math" type solutions to perform the stepwise calculations (e.g. transfer the "carry over" from one step of the calculation to the next).

    For integer sizes smaller than the max size, you can use bitfields.

    In both cases, it will be slower than using the standard sizes, since the standard sizes are (normally) supported directly in the hardware architecture, and both bitfields and composite solutions will require extra code to produce the required results.

    --
    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. sorting number
    By Leslie in forum C Programming
    Replies: 8
    Last Post: 05-20-2009, 04:23 AM
  2. Use of variable
    By alice in forum C Programming
    Replies: 8
    Last Post: 06-05-2004, 07:32 AM
  3. newb question~
    By Anima in forum C++ Programming
    Replies: 10
    Last Post: 08-24-2003, 05:54 PM
  4. storing different variable types in arrays.........
    By Unregistered in forum C Programming
    Replies: 6
    Last Post: 06-28-2002, 11:45 AM
  5. Variable Allocation in a simple operating system
    By awkeller in forum C Programming
    Replies: 1
    Last Post: 12-08-2001, 02:26 PM