Thread: short vs int

  1. #1
    Registered User QuestionKing's Avatar
    Join Date
    Jan 2009
    Posts
    68

    short vs int

    Code:
    const short REPEATS = 10;
    for(short i=0; i<REPEATS; i++)
    {
           std::cout<< "...\n";
    }
    Would it make a difference to use short here instead of int?
    Asking a question you already know the answer to might teach you something you did not know...

  2. #2
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    Quote Originally Posted by QuestionKing View Post
    Code:
    const short REPEATS = 10;
    for(short i=0; i<REPEATS; i++)
    {
           std::cout<< "...\n";
    }
    Would it make a difference to use short here instead of int?
    There might be a difference in the generated assembly code, but you wouldn't notice a difference from a usage standpoint.
    bit∙hub [bit-huhb] n. A source and destination for information.

  3. #3
    Registered User QuestionKing's Avatar
    Join Date
    Jan 2009
    Posts
    68
    If a short is half the size of int in terms of memory allocated, wouldn't you be (however minor) saving memory by using short any time you can as in the example? Can someone explain in detail? I do not ever recall seeing a short used in a loop like this, and I would like to understand why. Ty.
    Asking a question you already know the answer to might teach you something you did not know...

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by QuestionKing
    If a short is half the size of int in terms of memory allocated, wouldn't you be (however minor) saving memory by using short any time you can as in the example?
    Maybe, but promotion to int will be performed anyway due to the arithmetic, so it may not make a difference in the end.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Dec 2006
    Location
    Canada
    Posts
    3,229
    And it's possible that short will be slower than int, though I don't think that's the case on x86.

    "int" is defined to be the "natural" word size of the machine, usually meaning it's the fastest, and should be used for everything "general purpose", like iterators.

    Only use other sizes if you have a good reason to. Eg, if you need an array of a few GBs, or if you need to store something bigger than the size of int can hold.

  6. #6
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    For that example you also could have used a char instead of a short or int and saved even more space, but like laserlight said, it would probably lose that space advantage because of a promotion to int.
    "I am probably the laziest programmer on the planet, a fact with which anyone who has ever seen my code will agree." - esbo, 11/15/2008

    "the internet is a scary place to be thats why i dont use it much." - billet, 03/17/2010

  7. #7
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    this decision should be made only after profiling. Good profiler could tell you if the code slowness is caused by the cache misses and so you would like to shorten the types used.

    If you do not have performance problems or you have not run profiler or profiler has not found such types of problems in the code - no premature optimization should be done
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  8. #8
    Registered User QuestionKing's Avatar
    Join Date
    Jan 2009
    Posts
    68
    Quote Originally Posted by cpjust View Post
    For that example you also could have used a char instead of a short or int and saved even more space, but like laserlight said, it would probably lose that space advantage because of a promotion to int.
    That was definately my next question
    Thank you all very much.
    Asking a question you already know the answer to might teach you something you did not know...

  9. #9
    Registered User
    Join Date
    Nov 2009
    Posts
    82
    Do you really lose the space advantage though?

    It will make a copy of the value into an int size container for math operations explicitly, but not convert the array of chars in to an array of ints in memory, right?

    If the system converts the source array in memory to int what was the point of even specifying a datatype, everything might as well be ints. xD

    Am I confused about something?

  10. #10
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by QuestionKing View Post
    Code:
    const short REPEATS = 10;
    for(short i=0; i<REPEATS; i++)
    {
           std::cout<< "...\n";
    }
    Would it make a difference to use short here instead of int?
    Only if you want to burden the maintainer with needing to remember to change the data type if he/she ever wants to change REPEATS to some value larger than a short could hold.

    At any rate, since REPEATS is a small constant, the compiler might unroll the loop anyway.

    Another concern is that the value is signed. I suggest that normally, loop counters and array indices should be unsigned.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  11. #11
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    Loop induction variables are nearly always promoted to a register, so you don't save any memory there anyway. In general, if you don't have an array of at least 500 elements, worrying about the data type is rather useless. And even then you'll still want to profile first.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Moving Average Question
    By GCNDoug in forum C Programming
    Replies: 4
    Last Post: 04-23-2007, 11:05 PM
  2. Working with random like dice
    By SebastionV3 in forum C++ Programming
    Replies: 10
    Last Post: 05-26-2006, 09:16 PM
  3. Half-life SDK, where are the constants?
    By bennyandthejets in forum Game Programming
    Replies: 29
    Last Post: 08-25-2003, 11:58 AM
  4. Quack! It doesn't work! >.<
    By *Michelle* in forum C++ Programming
    Replies: 8
    Last Post: 03-02-2003, 12:26 AM
  5. easy if you know how to use functions...
    By Unregistered in forum C Programming
    Replies: 7
    Last Post: 01-31-2002, 07:34 AM