2

Is there syntax in C to let the compiler know that a certain variable is done for and will henceforth not be used anymore, thus potentially freeing up a register? I'd rather not use function calls because they are expensive.

I know I could reuse a variable, but that leads to ambiguity (what does this variable mean?).

Example:

first_byte = somestring[0];
/*[do a couple of operations on first_byte]*/
done_for(first_byte);
second_byte = somestring[1];

The reason I'm not feeding the array+index directly into my operations is because I'm not sure if all compilers will optimize that to use a temp register rather than translating the address every time.

And no, I can't use a name like 'temp'. This is about reading a packed datastructure.

AnnoyinC
  • 426
  • 4
  • 17

1 Answers1

6
{
 int first_byte = somestring[0];
/*[do a couple of operations on first_byte]*/
}
second_byte = somestring[1];

wrap it into the complex statement. The first_byte will exist only inside the braces.

0___________
  • 60,014
  • 4
  • 34
  • 74
  • As shown here: https://stackoverflow.com/questions/45387465/how-to-undeclare-delete-variable-in-c , the scope and liveliness of the variable will be limited in the block of code of the braces. Alternatively, you can always use a pointer, allocate some memory, use it, and eventually discard it with free(). – Confidenc3 Jul 10 '20 at 13:29
  • @Confidenc3 I'm writing for an avr microcontroller, ram is rather rare and costly :P, but a good tip on desktop environments. – AnnoyinC Jul 10 '20 at 13:33
  • @AnnoyinC enable the optimizations. Only without it it will be always stored somewhere. Otherwise the compiler will decide. You do not have any control on the register allocation (except global register assignmet as avr-gcc extension - but it makes compiler work more difficult and almost always result in worse code) – 0___________ Jul 10 '20 at 13:36
  • I just want to reiterate that the compiler is perfectly able to analyze liveness without any help from the programmer. I would be very surprised if any compiler in common use would generate different code with or without the inner scope. – sepp2k Jul 10 '20 at 15:43
  • @sepp2k but it also sets the scope of the symbol and it can used somewhere else. compilers will not help with it – 0___________ Jul 10 '20 at 16:53
  • @P__J__ If `first_byte` isn't used after a certain point, then the compiler will realize this (even if it is still in scope) and optimize accordingly. Adding the inner scope is not necessary for the compiler to figure out that the variable isn't used any more. – sepp2k Jul 10 '20 at 18:48
  • @sepp2k so the `first_byte` can be defined again as `int` for example and compiler will do it itself. Very interesting but I afraid wrong. It has to be in the scope. You also think about very simple cases where it can be kept all the time in the register – 0___________ Jul 10 '20 at 20:41
  • No, you can't define another variable with the same name in the same scope, but the question was about optimization, not about being able to reuse variable names. And in terms of re-using registers or stack memory or other optimizations, the inner scope makes no difference. That's all I'm saying. – sepp2k Jul 10 '20 at 21:42