Home > Articles

  • Print
  • + Share This
This chapter is from the book

Q&A

  • Q Why define constants at all if you can use regular variables instead of them?

  • A Constants, especially those declared using the keyword const, are your way of telling the compiler that the value of a particular variable be fixed and not allowed to change. Consequently, the compiler always ensures that the constant variable is never assigned another value, not even if another programmer was to take up your work and inadvertently try to overwrite the value. So, declaring constants where you know the value of a variable should not change is a good programming practice and increases the quality of your application.

  • Q Why should I initialize the value of a variable?

  • A If you don’t initialize, you don’t know what the variable contains for a starting value. The starting value is just the contents of the location in the memory that are reserved for the variable. Initialization such as that seen here:

    int myFavoriteNumber = 0;

    writes the initial value of your choosing, in this case 0, to the memory location reserved for the variable myFavoriteNumber as soon as it is created. There are situations where you do conditional processing depending on the value of a variable (often checked against nonzero). Such logic does not work reliably without initialization because an unassigned or initiated variable contains junk that is often nonzero and random.

  • Q Why does C++ give me the option of using short int and int and long int? Why not just always use the integer that always allows for the highest number to be stored within?

  • A C++ is a programming language that is used to program for a variety of applications, many running on devices with little computing capacity or memory resources. The simple old cell phone is one example where processing capacity and available memory are both limited. In this case, the programmer can often save memory or speed or both by choosing the right kind of variable if he doesn’t need high values. If you are programming on a regular desktop or a high-end smartphone, chances are that the performance gained or memory saved in choosing one integer type over another is going to be insignificant and in some cases even absent.

  • Q Why should I not use global variables frequently? Isn’t it true that they’re usable throughout my application and I can save some time otherwise lost to passing values around functions?

  • A Global variables can be read and assigned globally. The latter is the problem as they can be changed globally. Assume you are working on a project with a few other programmers in a team. You have declared your integers and other variables to be global. If any programmer in your team changes the value of your integer inadvertently in his code—which even might be a different .CPP file than the one you are using—the reliability of your code is affected. So, sparing a few seconds or minutes should not be criteria, and you should not use global variables indiscriminately to ensure the stability of your code.

  • Q C++ is giving me the option of declaring unsigned integers that are supposed to contain only positive integer values and zero. What happens if I decrement a zero value contained in an unsigned int?

  • A You see a wrapping effect. Decrementing an unsigned integer that contains 0 by 1 means that it wraps to the highest value it can hold! Check Table 3.1—you see that an unsigned short can contain values from 0 to 65,535. So, declare an unsigned short and decrement it to see the unexpected:

    unsigned short myShortInt = 0;  // Initial Value
    myShortInt = myShortInt - 1; // Decrement by 1
    std::cout << myShortInt << std::endl; // Output: 65535!

    Note that this is not a problem with the unsigned short, rather with your usage of the same. An unsigned integer (or short or long) is not to be used when negative values are within the specifications. If the contents of myShortInt are to be used to dynamically allocate those many number of bytes, a little bug that allows a zero value to be decremented would result in 64KB being allocated! Worse, if myShortInt were to be used as an index in accessing a location of memory, chances are high that your application would access an external location and would crash!

  • + Share This
  • 🔖 Save To Your Account