This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

stack waste for local variables in case of optimization level 2

Hello,

i am wondering about stack usage with optimization Level 2.
Thread job2 uses 264 Bytes on stack. Obviously much more than required.
Using optimization Level 0, stack usage is only 168 Bytes.
I thought that escaping scope1 would frees the allocated stack regardless
of optimization level, but it isn't.

Is this the purpose of optimazion?

#define ARRAYSIZE1 100
#define ARRAYSIZE2 100

void job2 (void const *argument)
{
  while(1) {
    int i;
    {
      // scop 1
      uint8_t array1[ARRAYSIZE1];
      for (i = 0; i < ARRAYSIZE1; i++) {
        array1[i] = reg; // read from reg
      }
      for (i = 0; i < ARRAYSIZE1; i++) {
        reg = array1[i]; // write to reg
      }
    }
    {
      // scope 2
      uint8_t array2[ARRAYSIZE2];
      for (i = 0; i < ARRAYSIZE2; i++) {
        array2[i] = reg; // read from reg
      }
      for (i = 0; i < ARRAYSIZE2; i++) {
        reg = array2[i]; // write to reg
      }
    }
  }
}