Like Tree3Likes
  • 1 Post By CommonTater
  • 2 Post By Salem

short index vs. int index

This is a discussion on short index vs. int index within the C Programming forums, part of the General Programming Boards category; Hello again, In every book or tutorial about C language I happened to take a look, they use indexes for ...

  1. #1
    Charming Registered User
    Join Date
    May 2011
    Posts
    49

    short index vs. int index

    Hello again,
    In every book or tutorial about C language I happened to take a look, they use indexes for loops and arrays, in programs used as examples, that won't count/index but a very small number, like that:
    Code:
    .
    .
    #define MAX_LENGTH 10
    
    int index;
    int array[MAX_LENGTH];
    
    for (index = 0; index < MAX_LENGTH; ++index)
      array[index] = 0;
    .
    .
    I have written the same examples with a minor change and they seem to compile and run fine.
    Code:
    .
    .
    #define MAX_LENGTH 10
    
    short index;
    int array[MAX_LENGTH];
    
    for (index = 0; index < MAX_LENGTH; ++index)
      array[index] = 0;
    .
    .
    Isn't it better idea and a good practice to use a short index instead of an int since we are going to deal with very small values?

    If not why ?

    Thank you for your time to help me!

  2. #2
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Either way is fine...

    The common argument is that an int is *supposed* to be the native size of the registers in the target processor (hense the business about 16 vs 32 vs 64 bit compilers) and *may* offer a slight performance advantage over short or char for loop counters and iterators by eliminating the need to convert when loading the values. However; most decent compilers keep loop counters in registers after the initial read from memory, so that is no longer much of an issue.
    ardavirus likes this.

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,854
    If you're on a machine that lacks easy byte / word / long access to registers (like say the x86), you might be causing more work to keep the value within bounds of the shorter data type.

    Do you ever have loops which run for more iterations than can be fit into a short?
    Or more insidiously, where your #define limit can change after you've written the code to be >short, you're basically introducing bug opportunities.

    Picking a single style which works all the time is preferable to trying to second-guess (and getting it wrong) what the future is likely to be.
    ardavirus and CommonTater like this.
    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.

  4. #4
    Charming Registered User
    Join Date
    May 2011
    Posts
    49
    @CommonTater & Salem
    Thank you so much for your quick and precise answer.
    Last edited by ardavirus; 06-13-2011 at 06:12 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Longest Index
    By megazord in forum C Programming
    Replies: 3
    Last Post: 12-12-2009, 08:32 AM
  2. Index no more space
    By limitmaster in forum C++ Programming
    Replies: 3
    Last Post: 01-05-2008, 11:26 PM
  3. SQL: When to Avoid Index?
    By alphaoide in forum Tech Board
    Replies: 3
    Last Post: 12-02-2006, 01:25 PM
  4. ? about this> index::index () : type_intex()
    By bobk544 in forum C++ Programming
    Replies: 5
    Last Post: 09-30-2005, 03:59 PM
  5. Tab index
    By Micko in forum Windows Programming
    Replies: 3
    Last Post: 07-14-2004, 08:05 PM

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