1

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
}
too honest for this site
  • 12,050
  • 4
  • 30
  • 52
ZisIsNotZis
  • 1,570
  • 1
  • 13
  • 30
  • 3
    Does your profiler tell you this is a hot spot in your application, and what does the generated assembly say? – GManNickG Oct 06 '16 at 20:17
  • 3
    How about: `{ myClass a; a.doSomething(); } { if (...) { myClass c; c.doSomething(); } }` – Kerrek SB Oct 06 '16 at 20:17
  • GCC didn't use to reuse stack space very well even when there were short scopes in the function body. But I believe it has got better at that. – Kerrek SB Oct 06 '16 at 20:18
  • The answer is quite possibly different for stack-allocated C++ class instances than for C scalars. And since you're evidently focusing on class instances, you should remove [c] from the tag list. – John Bollinger Oct 06 '16 at 20:24
  • @GManNickG It's a function called about 1,000,000 times in the main with a big storage cost, and I can't inline that in main because I need that to be recursive. Haven't tried profiler or generate assembly yet. – ZisIsNotZis Oct 06 '16 at 20:35

2 Answers2

5

Generally, the compiler is not allowed to reuse a until the end of its scope, which is the closing brace } at the end of the function. This feature (destroying objects at predictable time) enables making guards in C++, when destructor executes some special code*.

I'm trying to minimize the allocation cost of a frequently called function.

Since allocating automatic variables costs virtually nothing, the majority of the cost is in calling the constructor. This is not something that you could optimize away.

* If your object has trivial destructor, the compiler could reuse the memory. This would save you memory, not time.

Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
0

These variables are allocated on a stack. Even if compiler does not reuse the space, this will not lead to any additional CPU instructions, only RAM. Allocation on stack is just adding number of allocated bytes to stack pointer. And usually it is done with one instruction by adding size of all variables that exist in the function. And in any case compiler will be smart enough to reuse CPU register where variable is allocated.

Note, that if your classes have destructor, then they must persist until the end of the block. So in this case memory for variable a will not be reused because it is needed at the end of the function. But compiler can still reuse registers used for it.

Alexey Guseynov
  • 5,116
  • 1
  • 19
  • 29
  • 1
    does it have to be on stack? those are just "auto" variables. Using stack is implementation-dependent. – Jean-François Fabre Oct 06 '16 at 20:22
  • Well, they may be optimized out and placed on a register. In this case there is no cost for allocation and register reuse is something which compiler has to do well enough. – Alexey Guseynov Oct 06 '16 at 20:24