I need to host a 500 byte array momentarily for a block of a large program that consists of several blocks but I don't have enough ram so I want to share an area of ram between two blocks of the program. Obviously the execution of both blocks is exclusive.
The program written in C will run on atmega16 and written in avr studio 7.
How can I define the variables of each block so that the compiler assigns them the same ram area without generating an error?
Thank you.
I tried to define my own segment but that doesn't mean they share the same ram area.
By declaring the array inside a function, it tells the C compiler that the array will no longer be needed once returning from the function (so it will be stack allocated almost always, and the memory will be automatically freed once the function returned). Otherwise, the array might be statically allocated, which reserves the memory for the entire runtime.
If the array is already declared inside a function, there probably is no way to do what you want, since there is no space to store the rest of your program data for a temporary period, and trying to "trick" the compiler will only introduce potentially fatal bugs and undefined behavior.
You could always try to refactor your code so that the array's life is contained as early as possible in the call stack. Meaning that instead of creating the array after 10 function calls (without returning) you can try to create it , use it and then proceed to the rest of the calls, but this is an approach which requires some understanding of how C compilers work.