How to define variable of specific size.

This is a discussion on How to define variable of specific size. within the C Programming forums, part of the General Programming Boards category; I need a way to refer to a dword. Is there a way to define a variable with a size ...

  1. #1
    Password:
    Join Date
    Dec 2009
    Location
    NC
    Posts
    587

    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
    Location
    TX
    Posts
    2,047
    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
    Posts
    476
    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
    Password:
    Join Date
    Dec 2009
    Location
    NC
    Posts
    587
    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 wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,494
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

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, 09:56 AM
  5. whats wrong here
    By sreetvert83 in forum C++ Programming
    Replies: 15
    Last Post: 09-21-2005, 10:05 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21