0

Is there a benefit in using short and long instead of int and double? Other than int and double taking up more storage? My professor penalizes code that uses int and double with no explanation. I have not really found a good reason for it in the few textbooks I am using. Where can they be more useful? For example, this one of many functions in my code

    void randDir(int& x, int& y){
            do{
                x = rand() % 4 - 1;
                y = rand() % 4 - 1;
                if(x == 2) x=0;
                if(y == 2) y=0;
            }while(x == 0 && y == 0);
  }  

And this is what my professor wanted

    void randDir(short& x, short& y){
    do{
        x = rand() % 4 - 1;
        y = rand() % 4 - 1;
        if(x == 2) x=0;
        if(y == 2) y=0;
    }while(x == 0 && y == 0);
}
O2Addict
  • 147
  • 11
  • What course are you taking? Is it embedded systems programming or something like that where memory is very tight? – Brian Bi Oct 24 '17 at 00:03
  • 4
    If you want to know why your professor is grading in a certain way, ask them directly. We're not privy to that professor's thoughts, and know nothing about the course you're taking or what part of it you might be working on currently. – Ken White Oct 24 '17 at 00:05
  • 4
    Seeing as "plain `int`s have the natural size suggested by the architecture of the execution environment; the other signed integer types are provided to meet special needs" (C++ standard, **[basic.fundamental]/2**), penalizing the use of `int` appears to be, shall we say, misguided. `double` is a different matter - you don't want to use floating point gratuitously. `double` is in no way similar to `long`. – Igor Tandetnik Oct 24 '17 at 00:05
  • 2
    Do you mean "short and *float*"? – Ken Y-N Oct 24 '17 at 00:05
  • @Brian I am taking the second class offered at my college for computer science. It is a C++ class. The syllabus said we would focus on object-oriented programming. – O2Addict Oct 24 '17 at 00:06
  • 1
    `int` used to be the *native word* type. On compilers for many embedded systems it still is. It is also the standard integer type (for example many of the operations in the professors function will be performed using `int` and then converted to `short`). I think you should ask your professor about the rationale for that rule. Programming C++ on a non-embedded system (like a somewhat modern PC) it just makes no sense to *not* use `int`. – Some programmer dude Oct 24 '17 at 00:06
  • 1
    `double` is a floating-point type, with very different properties than the integer types `short`, `int`, and `long`. As for `int` vs. `short`, were the parameter types for the `randDir` function specified in the assignment? If so, you're being penalized for not following the instructions, not specifically for using `int` rather than `short`. – Keith Thompson Oct 24 '17 at 00:06
  • 2
    Professor may be under the assumption that `RAND_MAX` is 32767 and doesn't need anything larger than a `short` to be represented. They are, unfortunately, wrong. 32767 is the minimum for `RAND_MAX`, so stuffing the output of `rand` into a short is risky. Best check with the instructor. – user4581301 Oct 24 '17 at 00:07
  • 1
    in the past int was supposed to be fast while short is supposed to economic, and long is supposed to have larger range. now int is supposed to be 32 bit, short 16 bit, long is supposed to be able to hold a pointer. but i still believe int is still at least the most convenient, although it might be better practice to use u/intXX_fast_t and u/intXX_t – user3528438 Oct 24 '17 at 00:07
  • 1
    @user4581301 But in this example, `rand() % 4` will fit into `short`. – Ken Y-N Oct 24 '17 at 00:08
  • 1
    That I can't argue with. Fit into a `char`. Nybble if you got one. – user4581301 Oct 24 '17 at 00:10
  • 1
    Furthermore, [`std::rand`](http://en.cppreference.com/w/cpp/numeric/random/rand) is specified to return an `int`. Using `short` makes even less sense then. – Some programmer dude Oct 24 '17 at 00:12
  • 1
    You could even fit both those numbers in a single `char`! \o/ On a more serious note, your prof seems to have some funny ideas, might be a good idea to supplement that course with a good and up-to-date C++ book. – Baum mit Augen Oct 24 '17 at 00:13
  • @BaummitAugen I honestly agree with you. His sample codes seem to be outdated. Is it detrimental to practice this older style? – O2Addict Oct 24 '17 at 00:15
  • There is no guarantee that a processor or compiler will use 16 bits for a short. All you are doing is shrinking the range that the variable will support. The compiler is allowed to use 64 bits for a `short` integer. There is no maximum size guarantee by the language standard. – Thomas Matthews Oct 24 '17 at 00:19
  • You should at least be aware when you are using outdated or (like e.g. `rand`) even obsolete stuff from the language. Those reference output parameters aren't ultra idiomatic either. – Baum mit Augen Oct 24 '17 at 00:19

1 Answers1

3

The only advantage of using short is that it takes up less space. If you're programming for a very memory-tight environment, or you have a large array of numbers, or your data will leave the program's address space (e.g., to be saved to disk or transmitted across a network) then this might be important. In other cases, using types other than int unnecessarily might actually slow down your program due to the way processor architectures are designed. See for example:

If you need a type that has exactly the size you think short has, in order to store some bit pattern or something like that, you should be using one of the exact-width types such as std::int16_t, not short. So that's usually not a valid reason for using short.

It's possible that your C++ instructor learned to code a long time ago in an environment where every byte counts, and mistakenly believes that that's still the case nowadays. Sadly, this kind of preconceived notion is very common. (Other symptoms include forbidding exceptions and forbidding the use of standard library containers). In such cases you should generally be aware of the fact that smart people can often say stupid things. Stack Overflow is a good place to get information about current best practices, as are the books listed here: The Definitive C++ Book Guide and List

Brian Bi
  • 111,498
  • 10
  • 176
  • 312