简体   繁体   中英

Is it necessary to store a variable twice when using cin?

I'm working my way through "Accelerated C++" and I was comparing my answer to an end-of-chapter exercise to the one found here . When creating the user input for row and column padding, I wrote:

int colpad;
cout << "Please enter the number of columns to pad: ";
cin >> colpad;
int rowpad;
cout << "Please enter the number of rows to pad: ";
cin >> rowpad;

and then proceeded to use rowpad and colpad throughout the remainder of the function. This worked for me.

However, the author of the site above's solution did what I did, and then added (variable names changed for clarity):

const int rowpad = inrowpad; // vertical padding
const int colpad = incolpad; // horizontal padding

His solution works as well. My question is: Should I be storing the value of colpad and rowpad a second time as a const? Would this not just be taking up extra memory?

It isn't necessary to store the values into a const version. Using const objects prevents accidental changes and within a function it probably won't use extra memory if the compiler can decide that the variables used to initialize the the const objects don't change.

BTW, the code is wrong in a different way, though: you should always verify after reading that the read operation was successful:

if (!(std::cin >> colpad)) {
    // deal with the read failure
}

The extra memory is going to be insignificant, the compiler may well optimize it away anyway, but the use of const reduces the chance of errors a little.

I wonder if the added complexity of having two variables for the same thing in the same scope is worth it though.

I would prefer to extract out the non-const variable to a separate function:

int getInt() {
  int in;
  if(cin >> in)
    return in;
   ...  // handle errors
}

cout << "Please enter the number of columns to pad: ";
const int colpad = getInt();
cout << "Please enter the number of rows to pad: ";
const int rowpad = getInt();

Then there is only one variable in the main scope and it has the added benefit that code duplication is reduced a little.

Using const is a good way to protect the input value from changes later. Given the example programs on the website you provided there's no reason to add new const variables and yes it would consume a couple extra bytes of code space and memory during execution.

If later you wanted to pass variables or pointers around to other functions, you may consider using const to protect pointers and references. But there's no harm in doing what the example is showing and your questions are right on.

Also to directly answer your subject line: no it's not necessary.

Such constructs are normally "optimized away" during compilation. This means that, in your binary program, there'll often be a single copy of the variable.

So, as long as you're not making unnecessary copies of structures of non-constant space complexity (arrays, trees, graphs...), you shouldn't worry about one or two extra variables.

Rather than 2 variables I might define a function:

int getIntFromUserInput()
{
    int inputValue(0);
    std::cin >> inputValue;
    return inputValue;
}

which can be used as below:

std::cout << "Please enter the number of columns to pad: ";
const int numColumnsToPad(getIntFromUserInput());
std::cout << "Please enter the number of rows to pad: ";
std::const int numRowsToPad(getIntFromUserInput());

That way you can store the result of the input as const without having any concerns about unneeded memory usage.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM