For example if I have the following code:
int main(){
myClass a(...);
a.doSomething();
if(...){
myClass c(...);
c.doSomething();
}
}
Will usual compilers like gcc or clang optimize the using of these variables by finding out "a" will not be used anymore during it's lifetime and instead of reallocating space for "c", just use space of a? If that do not work for class, will that work for "traditional" type like double or size_t?
I'm trying to minimize the allocation cost of a frequently called function. But at somewhere inside the function I feel some old variables are already useless but new variables should not be called that name. Will compiler directly reuse variable for me or should I do something like
myClass a(...);
something(a);
if(...){
#define c a
c=myClass(...);
something c;
#undef c
}