CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to CCS Technical Support

memory allocation

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
clockuser



Joined: 07 Oct 2003
Posts: 4

View user's profile Send private message Send e-mail

memory allocation
PostPosted: Tue Oct 07, 2003 10:40 am     Reply with quote

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
Ttelmah
Guest







Re: memory allocation
PostPosted: Tue Oct 07, 2003 10:54 am     Reply with quote

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
Guest








re:memory allocation
PostPosted: Tue Oct 07, 2003 11:05 am     Reply with quote

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
Neutone



Joined: 08 Sep 2003
Posts: 839
Location: Houston

View user's profile Send private message

PostPosted: Tue Oct 07, 2003 12:08 pm     Reply with quote

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 .
Mark



Joined: 07 Sep 2003
Posts: 2838
Location: Atlanta, GA

View user's profile Send private message Send e-mail

PostPosted: Tue Oct 07, 2003 12:52 pm     Reply with quote

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.
Ttelmah
Guest







Re: re:memory allocation
PostPosted: Tue Oct 07, 2003 2:33 pm     Reply with quote

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
dyeatman



Joined: 06 Sep 2003
Posts: 1934
Location: Norman, OK

View user's profile Send private message

Reusing Variables
PostPosted: Tue Oct 07, 2003 3:19 pm     Reply with quote

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.
frankb



Joined: 08 Sep 2003
Posts: 7

View user's profile Send private message

PostPosted: Tue Oct 07, 2003 3:28 pm     Reply with quote

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.
clockuser



Joined: 07 Oct 2003
Posts: 4

View user's profile Send private message Send e-mail

still didn't work
PostPosted: Wed Oct 08, 2003 11:25 am     Reply with quote

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
Mark



Joined: 07 Sep 2003
Posts: 2838
Location: Atlanta, GA

View user's profile Send private message Send e-mail

PostPosted: Wed Oct 08, 2003 11:29 am     Reply with quote

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.
clockuser



Joined: 07 Oct 2003
Posts: 4

View user's profile Send private message Send e-mail

my code
PostPosted: Wed Oct 08, 2003 11:44 am     Reply with quote

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
Mark



Joined: 07 Sep 2003
Posts: 2838
Location: Atlanta, GA

View user's profile Send private message Send e-mail

PostPosted: Wed Oct 08, 2003 11:55 am     Reply with quote

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);
}
clockuser



Joined: 07 Oct 2003
Posts: 4

View user's profile Send private message Send e-mail

problem solved!!
PostPosted: Wed Oct 08, 2003 12:39 pm     Reply with quote

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
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group