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

Issues with CCS C Compiler and Variable Initialization

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



Joined: 06 Aug 2024
Posts: 2

View user's profile Send private message

Issues with CCS C Compiler and Variable Initialization
PostPosted: Mon Aug 26, 2024 12:23 am     Reply with quote

Hello, everyone.

I am experiencing problems with variable initialization in the CCS C Compiler. When I execute my code on a PIC microcontroller, my variables do not appear to be initialized as planned. Specifically, I'm observing inconsistent behavior with global variables that should be changed to zero but appear to keep their former values.

Here is the code:

Code:
int globalVar;

void main() {
    // Initialize globalVar
    globalVar = 0;
    // Rest of the code
}


Even if globalVar is expressly set to 0 at the start, it occasionally seems to have a non-zero value when the program executes. This is especially problematic during repeated testing, as the variable's value remains between runs.

I also check this: USB Keyboard : how to find scancode for specific functionsrails But I have not found any solution. Has anyone have a similar problem or have any ideas on what is causing it? Any help on how to guarantee that variables are properly initialized would be highly appreciated.

Thanks![/url]
_________________
Thanks
temtronic



Joined: 01 Jul 2010
Posts: 9229
Location: Greensville,Ontario

View user's profile Send private message

PostPosted: Mon Aug 26, 2024 5:14 am     Reply with quote

try this method...

int globalVar=0;

..and see what happens.

It could be a compiler bug. Always post your compiler version , as well as which PIC. Post a small program that shows the problem. That way others can cut/paste/compile/test your code.
Ttelmah



Joined: 11 Mar 2010
Posts: 19520

View user's profile Send private message

PostPosted: Mon Aug 26, 2024 7:19 am     Reply with quote

A global variable is not initialised unless you say to do so.
A _static_ variable should be initialised to 0.
As Jay says if you want it to initialise, you have to specify this.

This is deliberate, since this is what allows you to have the variable
contents maintained after a watchdog restart. If you want them to be
zeroed, you simply add #ZERO_RAM

You don't explain what you have not found the solution to?.
The point in that thread, was that the functions being asked for (play
etc.), are _not_ keyboard functions, and there is no defined keyboard
scancode for these. These are multimedia controller codes, and you have
to program the device to implement two USB devices. A keyboard, and
a multimedia controller, to then allow this type of function to be
implemented.
A keyboard' cannot send these codes.

If the variable is non zero after you explicitly clear it, then you have a
code problem, Look at the symbol table. What variable is immediately
in front of this one?. What variable immediately after?. Do you use
pointers anywhere?. The common reason for a variable being corrupted,
is an overrun by another variable or pointer.
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