View previous topic :: View next topic |
Author |
Message |
fy9466
Joined: 28 Feb 2020 Posts: 2
|
How can i clear my mcu ram ( release the ram block ) |
Posted: Fri Feb 28, 2020 12:59 pm |
|
|
Hi everybody !
I need help. I have very big integer array and I use it only in a certain part of the program then I have to release the array because i have not much more space.
Attention guys I don't mention the fill arrays zero. It should be as never created, so the compiler behave like it empty and use for different variables.
Summary i want to delete the array that I created.
I'm about to go crazy. I am waiting for your advice.
Thank you for now. |
|
|
temtronic
Joined: 01 Jul 2010 Posts: 9243 Location: Greensville,Ontario
|
|
Posted: Fri Feb 28, 2020 1:22 pm |
|
|
You'll need to declare that array within the function that uses it as 'global' variables, once used are always 'used'.
The 'hardware solution is to use a 'bigger' PIC that is pin compatible, from the same family. |
|
|
fy9466
Joined: 28 Feb 2020 Posts: 2
|
... |
Posted: Fri Feb 28, 2020 1:52 pm |
|
|
Guy is there anyone to help me !
My request is quite basic.
I want to delete the big array for save my ram space at the execution.
That's all it. |
|
|
jeremiah
Joined: 20 Jul 2010 Posts: 1354
|
Re: ... |
Posted: Fri Feb 28, 2020 2:34 pm |
|
|
fy9466 wrote: | guy is there anyone to help me !
My request is quite basic
I want to delete the big array for save my ram space at the execution
that's all it |
The request is not basic at all. You have options, but they all have pros and cons:
1. Use an external memory for the array. PRO: you can control usage of the memory directly. CON: extra part and logic.
2. Do what temtronic suggested and move your array as far into your calling tree as possible (right next to the calculations). Don't pass it into any parameters for any functions and only call this function from the main. PRO: CCS will handle it for you. CON: It requires a lot of tricky program logic to pull off correctly EX:
Code: |
#include <33ep128gp506.h>
void big_var_processing(/*your params here*/){
unsigned int thing1[2000] = {100};
// Do your stuff, any functions you call here or that call this function
// cannot share this variable space.
}
void all_your_other_stuff(/*your params here*/){
unsigned int thing2[100] = {20};
// The rest of your program logic. Treat this like your new main, just
// pass out data so that big_var_processing can take it in and process it
// and pass in the results from big_bar_processing so it can be evaluated
// as needed
}
void main()
{
big_var_processing(/*your params here*/);
all_your_other_stuff(/*your params here*/);
}
|
You can see doing it this way reuses your memory.
Code: |
1002-1FA1 big_var_processing.thing1
1002-10C9 all_your_other_stuff.thing2
4F80-4FFF _STACK_
|
3. Use heap memory (malloc/free). PRO: you can control when memory is used and released. CON: you have to do this for any variables you want to share memory with the big array or else this method doesn't help at all. Additionally you have to manually remember to correctly free everything or you get memory leaks and eventually run out of memory to use. It is also (relatively) slow to use heap memory instead of stack memory due to the extra processing heap memory needs to work. Your memory usage will still look high because the compiler will have to reserve a large block of memory to cover your needs. |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19539
|
|
Posted: Sat Feb 29, 2020 1:16 am |
|
|
Understand something about RAM usage:
Code: |
int an_array[100];
void main(void)
{
}
//declared like this 'an_array' is global and can be used anywhere. It's space
//is permanently used.
void main(void)
{
int an_array[100];
}
//declared like this, 'an_array' exists so long as 'main' or any subroutine
//called by 'main' exists. So is effectively 'permanent'.
void my_sub(void)
{
int an_array[100];
}
void my_second_sub(void)
{
int a_second_array[100];
}
void main(void)
{
//do something
my_sub();
my_second_sub();
}
//declared like this, the space used by 'an_array', is _only_ reserved,
//while the code is running 'my_sub'. The same space can be re-used
//in other subroutines, so 'a_second_array', can re-use the space (and
//will do so).
|
You need to start to declare variables _locally_ for routines. Then the space
can be re-used in other routines. |
|
|
|