Is there a difference in C++ between the following, with regards to the amount and efficiency in space and time utilisation, and does the answer depend on the compiler?
int main() {
int a, b = 0;
doSomething1(b);
a = 9;
doSomething2(a);
return 0;
}
int main() {
int a = 0, b = 0;
doSomething1(b);
a = 9;
doSomething2(a);
return 0;
}
int main() {
int b = 0;
doSomething1(b);
int a = 9;
doSomething2(a);
return 0;
}
Edit: This question has been marked as a duplicate of this, which in my opinion is incorrect because
- The other question is asking if variables - regardless of where they are used in a function - should be declared at the very start, or if they should be declared only within the code block where it is used (i.e. the most local scope).
- My question is asking if there is a performance difference, space- or time-wise, to initializing variables at the very start of a function, or only at the point where it is needed in the same scope.
Please review the flag on this question. It's not a duplicate.