Thread: How to define variable of specific size.

  1. #1
    Join Date
    Dec 2009

    How to define variable of specific size.

    I need a way to refer to a dword. Is there a way to define a variable with a size that doesn't change even for different compile settings. I need a way to declare a dword to access portions of Grub's boot structure passed to my kernel. I'd like to keep from using int or anything else that would change if I decide to compile as 64 bit, so if I decide to latter, it'll be easier to port it.

    No code to show yet... this question is kinda a prerequisite.

  2. #2
    Registered User
    Join Date
    Oct 2008
    sizeof int does not change between 32/64 bit micros, only that of long and pointer.
    Assuming of course that the compiler conforms to the ILP32 and LP64 architectures.

  3. #3
    Making mistakes
    Join Date
    Dec 2008
    If you want to be portable, you can use limits.h and then check for a variable that matches U<TYPE>_MAX == 1 << 31.

  4. #4
    Join Date
    Dec 2009
    I appreciate your input, but is that the only way? It seems crude to refer to something as something it's not. In general, for all basic groupings(byte, word, dword, qword, ...), is it possible to make a macro or something that would allow me to define a specified size to be reserved as either stack or bss?

    How about just a byte, is it possible to define a byte(as a byte, not a char)?

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    The edge of the known universe
    In C99 compilers, you have stdint.h

    int32_t - always 32 bits
    int_least32_t - at least 32 bits
    int_fast32_t - the fastest int with at least 32 bits

    Without C99, the best you can do is have something like
    assert( sizeof(int) * CHAR_BIT == 32 );
    which will at least tell you if your compiler isn't producing the exact size required.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. stdio.h?
    By kiros88 in forum C Programming
    Replies: 5
    Last Post: 05-21-2010, 07:09 PM
  2. Buidl Library with ./configure script
    By Jardon in forum C Programming
    Replies: 6
    Last Post: 07-24-2009, 09:36 AM
  3. Replies: 3
    Last Post: 05-13-2007, 08:55 AM
  4. NAQ: Everything you never wanted to know about CPP
    By evildave in forum C Programming
    Replies: 21
    Last Post: 12-12-2005, 10:56 AM
  5. whats wrong here
    By sreetvert83 in forum C++ Programming
    Replies: 15
    Last Post: 09-21-2005, 10:05 AM