View previous topic :: View next topic |
Author |
Message |
clockuser
Joined: 07 Oct 2003 Posts: 4
|
memory allocation |
Posted: Tue Oct 07, 2003 10:40 am |
|
|
hi all,
i am new to the forum, i am using CCS PCM version 3.149. the variables share memory locations to optimize the RAM.
the problem is that my variables get reused WHILE the function is active. anyone faced this problem before? thanks for the advice.
kodi |
|
![](templates/subSilver/images/spacer.gif) |
Ttelmah Guest
|
Re: memory allocation |
Posted: Tue Oct 07, 2003 10:54 am |
|
|
clockuser wrote: | hi all,
i am new to the forum, i am using CCS PCM version 3.149. the variables share memory locations to optimize the RAM.
the problem is that my variables get reused WHILE the function is active. anyone faced this problem before? thanks for the advice.
kodi |
This certainly should not happen. Are you using pointers or arrays anywhere?. The CCS compiler, does not 'bounds check' array accesses, so if you are using an array, and talk to an address beyond it's upper limit (or do the same with pointers), subroutines can then 'walk over' memory for other variables.
Best Wishes |
|
![](templates/subSilver/images/spacer.gif) |
Guest
|
re:memory allocation |
Posted: Tue Oct 07, 2003 11:05 am |
|
|
hi,
thanks for the reply,
i am not using any arrays or pointers. what exactly is happening is that there are two functions, one called after another. an int8 variable in the second function is getting overwritten by a int16 variable in the first function!
thanks
kodi |
|
![](templates/subSilver/images/spacer.gif) |
Neutone
Joined: 08 Sep 2003 Posts: 839 Location: Houston
|
|
Posted: Tue Oct 07, 2003 12:08 pm |
|
|
You may wish to declare the variables as static. Other wise the variable does not hold it's value between calls to the functions they are declared within. This is a feature of the compiler that optimises the use of RAM by reusing the same byte with several functions if the variable is not static. A static variable is only accessable from within the function that declares it but otherwise is the same as a global variable . |
|
![](templates/subSilver/images/spacer.gif) |
Mark
Joined: 07 Sep 2003 Posts: 2838 Location: Atlanta, GA
|
|
Posted: Tue Oct 07, 2003 12:52 pm |
|
|
Are you wanting the function "to remember" the last value the last time the function was called. If so, then you should following Neutone's suggestion because that is how you are supposed to declare a variable in those cases. Otherwise, I don't see how a calling functionA could overwrite a variable in a functionB since functionB would have exited so that functionA could continue unless functionB called functionA which would be recursion which I do not think CCS supports. |
|
![](templates/subSilver/images/spacer.gif) |
Ttelmah Guest
|
Re: re:memory allocation |
Posted: Tue Oct 07, 2003 2:33 pm |
|
|
Anonymous wrote: | hi,
thanks for the reply,
i am not using any arrays or pointers. what exactly is happening is that there are two functions, one called after another. an int8 variable in the second function is getting overwritten by a int16 variable in the first function!
thanks
kodi |
Er.
What you describe, is correct, and normal behaviour (C code will behave like this on a PC as well...).
This is the whole point of a 'static' declaration.
If declared as static, variables will retain their values between successive calls. If not, they are 'fair game' to be overwritten.
Some languages default to variables being static, and require declarations to make them dynamic. C however defaults to letting all variables be overwritten.
Once you leave a function, unless the variables are declared as static, you should consider their contents to have been lost.
Best Wishes |
|
![](templates/subSilver/images/spacer.gif) |
dyeatman
Joined: 06 Sep 2003 Posts: 1924 Location: Norman, OK
|
Reusing Variables |
Posted: Tue Oct 07, 2003 3:19 pm |
|
|
This is a bug uncovered by PCM programmer (I think) a long time ago.
The local variables are overwritten by other local variables especially when using nested functions. I always declare my variables global to avoid this problem. I know this is not "optimum" memory usage wise but yu avoid a lot of problems this way. |
|
![](templates/subSilver/images/spacer.gif) |
frankb
Joined: 08 Sep 2003 Posts: 7
|
|
Posted: Tue Oct 07, 2003 3:28 pm |
|
|
I had this problem with a 12F629 and it started behaving after I added #device *=16 to the header file. If you're using PCM try this. Place the statement right after the first line in your header file which should be an #include for the device you're using. Let us know if this makes a difference. |
|
![](templates/subSilver/images/spacer.gif) |
clockuser
Joined: 07 Oct 2003 Posts: 4
|
still didn't work |
Posted: Wed Oct 08, 2003 11:25 am |
|
|
hi all,
I thank you for your suggestions. I tried making my variables static, that helped but it started affecting the other variables which can't be made static such as function parameters. using the #device *=16 didnt do any change either.
Now it is affecting built-in functions such as delay_ms(), which is beyond my control!
do you think its a problem with the compiler? (PCM version = 3.149)
please advice .
thanks!
kodi |
|
![](templates/subSilver/images/spacer.gif) |
Mark
Joined: 07 Sep 2003 Posts: 2838 Location: Atlanta, GA
|
|
Posted: Wed Oct 08, 2003 11:29 am |
|
|
Post an example of your code and indicate how it fails. From what you described, it sounded like a programming error rather than a compiler error. |
|
![](templates/subSilver/images/spacer.gif) |
clockuser
Joined: 07 Oct 2003 Posts: 4
|
my code |
Posted: Wed Oct 08, 2003 11:44 am |
|
|
hi all,
a portion of my code.......
#include<16f877.h>
#include<string.h>
#use delay(clock=18432000)
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7,errors)
#fuses HS,NOWDT,NOPROTECT,NOLVP,PUT
#use standard_io(a)
#use standard_io(b)
#use standard_io(c)
#use standard_io(d)
#use standard_io(e)
void main()
{
/*----my initialization code starts----*/
--------
-------
/*----initialization complete----*/
show_setup_screen(); // show setup screen on LCD
delay_ms(2000); //delay for 2 secs
show_check_screen(); // show check screen on LCD
delay_ms(2000); // delay for 2 secs
while(1); // wait and do nothing
}
void show_setup_screen()
{
show_image1(page_address,column_add);
show_arrow(arrow_address);
}
void show_check_screen()
{
show_image2(page_address,column_add);
show_arrow(arrow_address);
}
-------------------------------------
i am also using an emulator which calls the PCM compiler. when i run, the program hangs after displaying the first screen. i find that it hanged at
#use delay(clock=18432000)
previously, it used to run completely but with wrong output. some variables in the show_check_screen() were overwritten by show_setup_screen().
if i make variables static, other variables are overwritten.
please advice. thanks.
kodi |
|
![](templates/subSilver/images/spacer.gif) |
Mark
Joined: 07 Sep 2003 Posts: 2838 Location: Atlanta, GA
|
|
Posted: Wed Oct 08, 2003 11:55 am |
|
|
Where are the declarations for page_address,column_add, arrow_address?
What about the code for show_image1 and show_arrow functions?
Which variables are getting overwritten?
Code: |
void show_setup_screen()
{
show_image1(page_address,column_add);
show_arrow(arrow_address);
}
|
|
|
![](templates/subSilver/images/spacer.gif) |
clockuser
Joined: 07 Oct 2003 Posts: 4
|
problem solved!! |
Posted: Wed Oct 08, 2003 12:39 pm |
|
|
hi all,
thanks for all your replies. yes, it is a programming fault.
i couldnt post the whole code because it was huge and there were a lot of nested functions.
what i found was that in one of the nested functions, i wrote the code as:
for(i=5;i>=1;i-=2)
{
show_arrow(i,1,1);
delay_ms(2000);
}
the value 'i' would eventually reach the value -1 (signed int8). that was passed into show_arrow(int8,int8,int1). there is a delay(2000); immediately in move_arrow() function. thats why it was getting stuck at #delay(--) statement.
it was some lesson for me! thank you all once again.
kodi |
|
![](templates/subSilver/images/spacer.gif) |
|