Thread: Should I be smart about the types I use?

  1. #1
    UT2004 Addict Kleid-0's Avatar
    Join Date
    Dec 2004
    Posts
    656

    Should I be smart about the types I use?

    I know a value for a certain variable is going to be under or == 255...yet I always use ints... so it's:
    Code:
    typedef (unsigned char) byte;
    byte hi[] = {1, 2};
    VS
    Code:
    int hi[] = {1, 2};
    I mean it's basically saying to myself if I want to use 1 byte, or 4 bytes. Which 3 bytes would be wasted, because all of the values would be between 1 and 255. In your opinion, would you go with the byte route, or using plain ol' ints?

  2. #2
    ... kermit's Avatar
    Join Date
    Jan 2003
    Posts
    1,534
    Perhaps this would answer your question?

  3. #3
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >In your opinion, would you go with the byte route, or using plain ol' ints?
    I would use int because I'm wasteful. But seriously, it probably doesn't matter unless you're using a LOT of them and accessing them regularly, then the time/space tradeoff of the native integer type probably being faster to access versus the space savings of using a small integer type arises. There's no hard rule because it's all done on a case by case basis.
    My best code is written with the delete key.

  4. #4
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    Neither! You should do:
    Code:
    unsigned short int hi = 1 | (2 << 8);
    :P
    If you understand what you're doing, you're not learning anything.

  5. #5
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Things that start off with "Should I be smart...", automatically deserve to be answered: "No, be an idiot."


    Quzah.
    Last edited by quzah; 11-20-2005 at 02:48 AM. Reason: Fine tuning.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. invalid types 'int[int]' for array subscript
    By kolistivra in forum C++ Programming
    Replies: 6
    Last Post: 12-11-2010, 12:57 PM
  2. Replies: 6
    Last Post: 08-23-2008, 01:16 PM
  3. How to convert integral types into pointer types?
    By rohit99 in forum C++ Programming
    Replies: 3
    Last Post: 03-20-2008, 09:57 AM
  4. The Interactive Animation - my first released C program
    By ulillillia in forum A Brief History of Cprogramming.com
    Replies: 48
    Last Post: 05-10-2007, 02:25 AM
  5. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM