I've programmed a task scheduler for STM32 (ARM) MCU which enables multitasking. I can create concurrent tasks, each having an own allocated stack, as this is the most straightforward way to do it. These stacks are allocated on the heap. Since these stacks are static in size, which is a very inefficient way to use memory space, I'm planning to add dynamic stack reallocation.
My question is: If I reallocate a task's stack on another memory address and copy all stack contents and update the task (ie. stack pointer), can the task continue running without any problem, if I'm not using in the task's code any absolute address of the stack? Is the C compiler using relative addressing only in the stack even if I'm taking an address of a variable in the scope?
Example:
void A() {
int i = 0;
int* iPtr = &i;
}
In the case above the value of iPtr will be a static address, or relative like currAddress-4? (If I'm not passing it to another function, just using it inside the scope.)
So is there any way the compiler uses relative address with offset in this scope, or just uses the direct address of the variable?
If there is relative address handling then I can reallocate the stack freely on another memory space, if not then I can't, which would be a problem.
I didn't really find any good documentation about this, so that also would be appreciated!
If not this is the right approach, then how stack reallocation for tasks used to be implemented?