Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it better to check a variable before setting its value in C++?

If I have a boolean and some code which maybe changes it, and then I want to set it to true, should I check if it's false?

For example:

bool b = false;
// Some code
// Here "b" can be true or false
if (cond) {
    b = true;
}

vs

bool b = false;
// Some code
// Here `b` can be `true` or `false`
if (cond && !b){
    b = true;
}

Which is faster?

Note:

I ask that because of the following implementation of Sieve of Eratosthenes: http://bloc.gerardfarras.com/wp-content/uploads/2011/12/erastotenes.txt

if (( i % divisor == 0 ) && ( numsprimers[i] == 0 )) {
    numsprimers[i] = 1;
}

(If numsprimers[i]==1 it means that i isn't a prime number. And if it's 0 it can be prime or not)

like image 534
Oriol Avatar asked Oct 14 '12 22:10

Oriol


People also ask

Can a variable be executed without being assigned a value?

Thus when a variable is given a memory address to use to store data, the default value of that variable is whatever (garbage) value happens to already be in that memory address! A variable that has not been given a known value (usually through initialization or assignment) is called an uninitialized variable.

How do you check if a variable has been defined in C?

The definition of a variable in C is not checked by code writer. The compilers do it for you. When compile and link your C code, the compiler will check all variable's definitions. An error will be invoked and the compiling or linking process will stop if there are undefined variables found in your code.

What happens if you attempt to access the value of an uninitialized variable in C?

An uninitialized variable has an undefined value, often corresponding to the data that was already in the particular memory location that the variable is using. This can lead to errors that are very hard to detect since the variable's value is effectively random, different values cause different errors or none at all.

Can you change the value of a variable in C?

In C or C++, we can use the constant variables. The constant variable values cannot be changed after its initialization.


1 Answers

It's being very very nitpicky, but generally speaking it's better to just change the value.

Checking and setting a value have roughly the same overhead anyway, so why would you want to have to do both in some cases?

Now if you're wondering if you should overwrite some custom type (lets say a list of 100000 words) or if you should check to see if it needs to be overwritten first (let's say by simply checking a boolean value or a timestamp) then you should check first, because the cost of checking a boolean or timestamp is much less than writing so many words to memory.

This is of course all dependent on various things such as whether or not the memory you are editing is in cache, how expensive the "check" is, how often you need to overwrite the value versus how often it does not need to be overwritten, and of course the size of the memory.

like image 67
Ben Avatar answered Sep 28 '22 18:09

Ben