Thread: Array of "bytes"

  1. #1
    Beginning game programmer Petike's Avatar
    Join Date
    Jan 2008
    Posts
    64

    Question Array of "bytes"

    Hi all,
    I would like to declare an array of "bytes" like that:
    Code:
    <SOMETHING> pool[10000];
    For example:
    Code:
    char pool[10000];
    But I am not sure if "sizeof(char)" always equals "1" on every machine and every operating system.
    If it is "not" equal, then how could I declare an array which all elements equal "1 byte" (8 bits)?

    Thanks.
    Petike

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    The C standard says that sizeof(char) is ALWAYS 1 (don't ask me to quote the exact paragraph). However, if the machine uses a char that is 8 bits or something else is a completely different matter (most have 8-bit char, but it's not set in stone) - I don't KNOW of any machine that doesn't, but I have read about such things.

    Also, bear in mind that every time you NEED something to be a particular (minimum or maximum) size, it's best to make it a typdef. You may for example do:
    Code:
    typedef char BYTE;
    
    BYTE buffer[...];
    That way, you can change the defintion of BYTE to something else on machines where something else is "better", without having to search through the entire source of your application/OS/etc to find where "char" is used as a BYTE and where "char" is used as a character.

    --
    Mats
    Last edited by matsp; 10-08-2008 at 03:46 PM.

  3. #3
    Beginning game programmer Petike's Avatar
    Join Date
    Jan 2008
    Posts
    64
    Thanks.
    Petike

  4. #4
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    As far as I am aware, however the size of a byte is not necessarily a constant.

  5. #5
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by master5001 View Post
    As far as I am aware, however the size of a byte is not necessarily a constant.
    Correct, and I also said in my post that a char may not always be 8 bits.
    http://en.wikipedia.org/wiki/Byte

    --
    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
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    So you did. Good show.

  7. #7
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Typically, raw data or buffers use unsigned char.
    I believe BYTE is defined as unsigned char in the Windows headers...
    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.

  8. #8
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by Elysia View Post
    Typically, raw data or buffers use unsigned char.
    I believe BYTE is defined as unsigned char in the Windows headers...
    a "BYTE" is not signed or unsigned - Windows uses BYTE, WORD, DWORD etc as signed values, but that is because those types originate from the MS Macro Assembler - but they are not signed or unsigned in assembler - that choice is made by choosing the right condition code for Jcc and SETcc instructins - if you choose the signed version, then the number is signed, and if you choose the unsigned version, then it's unsigned.

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

  9. #9
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,834
    If you want to talk about where the designers of C went wrong... Signed and unsigned should never have been made attributes of the variables. Because this leads to issues when unsigned and signed are mixed. Instead, just the size should have been sufficient. Then introduce signed vs. unsigned operators: plus, minus, times, etc. Some languages do it that way. After all, it's only during math that it matters. Not while it's stored.

    Here is another proof why the current system is foolish. Format strings in printf() and the like need to specify the signedness once again... because the ephemeral variable attribute is not recognizable any other way.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 16
    Last Post: 05-29-2009, 07:25 PM
  2. from 2D array to 1D array
    By cfdprogrammer in forum C Programming
    Replies: 17
    Last Post: 03-24-2009, 10:33 AM
  3. [question]Analyzing data in a two-dimensional array
    By burbose in forum C Programming
    Replies: 2
    Last Post: 06-13-2005, 07:31 AM
  4. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  5. Quick question about SIGSEGV
    By Cikotic in forum C Programming
    Replies: 30
    Last Post: 07-01-2004, 07:48 PM