char[256] ... why?

This is a discussion on char[256] ... why? within the C++ Programming forums, part of the General Programming Boards category; how come i see 256 as the number of bytes in character arrays so often? Code: char * myChar[256]; is ...

  1. #1
    Registered User GrNxxDaY's Avatar
    Join Date
    Jul 2002
    Posts
    140

    char[256] ... why?

    how come i see 256 as the number of bytes in character arrays so often?
    Code:
    char * myChar[256];
    is there something magical that happens?
    AOL: GrNxxDaY
    IDE: Dev-C++ Beta 5 (v4.9.4.1)
    Project: Eye of Sahjz (text-RPG)
    If you think I may need help, please IM me.

  2. #2
    Registered User
    Join Date
    Nov 2001
    Posts
    1,348
    No. There is nothing magical about 256. 1, 2, 4, 8, 16, 32, 64, 128, 256,...1024, are all powers of 2^x. It is just a practice among programs since we deal with 0s and 1s.

    Kuphryn

  3. #3
    Registered User
    Join Date
    Jun 2002
    Posts
    267
    it's a common size

  4. #4
    tgm
    tgm is offline
    Registered User tgm's Avatar
    Join Date
    Jun 2002
    Posts
    150
    Generally, a char is 1 byte - 8 bits - 256 possible values.

    In games and such it is wise to hold state information about all the possible values (for key presses and things). You can then reference a state based on it's ASCII value.

    Did I make enough sense with that?

  5. #5
    Registered User GrNxxDaY's Avatar
    Join Date
    Jul 2002
    Posts
    140
    you only need 1 byte to do that... not 256.
    so its just a convention to use char name[256]; ?
    AOL: GrNxxDaY
    IDE: Dev-C++ Beta 5 (v4.9.4.1)
    Project: Eye of Sahjz (text-RPG)
    If you think I may need help, please IM me.

  6. #6
    Blank
    Join Date
    Aug 2001
    Posts
    1,034
    No, you would need 256 bits if all you stored was if key was up or down. I've still always seen 256 bytes since it's probably faster to access.

    It's best that you keep your data on word boundaries,
    some compilers do this automatically.

    Knuth gives out $2.56 since 256 pennies is one hexadecimal dollar.

  7. #7
    Registered User GrNxxDaY's Avatar
    Join Date
    Jul 2002
    Posts
    140
    *goes crazy and doesn't understand*
    a regular char type has is 1 byte.. .that's 255 or so diff. values it can store. my question is why do people use 256 bytes (not bits!!) so often.
    AOL: GrNxxDaY
    IDE: Dev-C++ Beta 5 (v4.9.4.1)
    Project: Eye of Sahjz (text-RPG)
    If you think I may need help, please IM me.

  8. #8
    Blank
    Join Date
    Aug 2001
    Posts
    1,034
    You need to handle multiple keypresses.

    Some implementations of malloc are optimized for
    powers of 2.

    BUFSIZ and the size of a virtual page
    are usually powers of 2.

    When you subtract a unsigned number by 256 all your
    doing is clearing the 8 bit. Doing an and instruction might
    be faster on a really old processor. Well you can make your
    code more confusing doing it

  9. #9
    tgm
    tgm is offline
    Registered User tgm's Avatar
    Join Date
    Jun 2002
    Posts
    150
    >>why do people use 256 bytes (not bits!!) so often.

    Even though that would be the most size efficient, the smallest standard datatype in C/C++ is a char. There is no single bit variable type, and often times it's more work if you're trying to manipulate bits (and people are generally lazy).

  10. #10
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >how come i see 256 as the number of bytes in character arrays so often?
    The exact reason why 256 is used to so much is because the programmer chose to use it, probably because they felt it was not too much and not too little. I personally think that 256 is too small for a char array since they are usually used as buffers for input and you never know how much input there is.

    -Prelude
    My best code is written with the delete key.

  11. #11
    Registered User GrNxxDaY's Avatar
    Join Date
    Jul 2002
    Posts
    140
    i think prelude finally got what i was asking (i'm not good at explaining problems i think). thakns
    AOL: GrNxxDaY
    IDE: Dev-C++ Beta 5 (v4.9.4.1)
    Project: Eye of Sahjz (text-RPG)
    If you think I may need help, please IM me.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. directory searching causes a windows error
    By scwizzo in forum Windows Programming
    Replies: 25
    Last Post: 04-02-2008, 04:55 PM

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