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

CCS compiler 4.124 warning

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



Joined: 08 Dec 2011
Posts: 15
Location: Portugal

View user's profile Send private message

CCS compiler 4.124 warning
PostPosted: Wed Dec 21, 2011 8:19 am     Reply with quote

Hello, I would like to know if there's some code, like #pragma or something that makes the compiler show a warning when a global variable is declared more than once!

Code:

#pragma case
#include <16F877A.h>
#device ADC=8

#fuses HS // High Speed Osc (20 Mhz Crystal)
#FUSES PUT //Power Up Timer (72ms page 39 on pic16f87xA)
#FUSES NOPROTECT //Code not protected from reading
#FUSES NODEBUG //No Debug mode for ICD
#FUSES NOBROWNOUT //No brownout reset
#FUSES NOLVP  //No lvp, B3(PIC16) or B5(PIC18) used for I/O
#FUSES NOCPD //No EE protection
#FUSES NOWRT //Program memory not write protected
#FUSES NOWDT //No Watch Dog Timer

#use delay(clock=20000000)

short GLOBAL_SHORT;

void main()
{   
   disable_interrupts(GLOBAL);
   setup_adc_ports(NO_ANALOGS);
   setup_adc(ADC_OFF); // no AD(AnalogDigital) converter
   setup_psp(PSP_DISABLED); // no PSP port

   setup_timer_1(T1_DISABLED); // no TMR1 timer1
   setup_timer_2(T2_DISABLED,0,1); // no TMR2 timer 2
   setup_comparator(NC_NC_NC_NC); // no comparator
   setup_vref(FALSE);
   setup_spi(SPI_SS_DISABLED);

   disable_interrupts(GLOBAL);
   
   short GLOBAL_SHORT; // can the compiler report a warning ?
   
   GLOBAL_SHORT = TRUE;
   
}


In other words, maybe a way that the compiler only acepts a unique name per variable
Ttelmah



Joined: 11 Mar 2010
Posts: 19535

View user's profile Send private message

PostPosted: Wed Dec 21, 2011 8:27 am     Reply with quote

_If_ you declared a global variable twice, the compiler would complain.
However you are not. It is legal in C, to have a global variable with a name, and a local variable with the same name. the rules are that if there is a local variable, it has priority locally over the global variable. So syntactically it is quite legal.
Key solution is to standardise on naming. Basic rules - use capital names for DEFINES, not variables. Then use a key word/letter, to say that a variable is global. You appear to almost be doing so, by using 'GLOBAL' as part of the name.
General comment - in C, it is _not_ legal to define variables 'inside' the code. Definitions must be at the start of a routine or code block. CCS does not complain about this (it is legal in C++), but it does cause erratic behaviour.
If you make your definitions at the start of the block, it makes simple visual inspection for faults _much_ easier.

Best Wishes
ckielstra



Joined: 18 Mar 2004
Posts: 3680
Location: The Netherlands

View user's profile Send private message

PostPosted: Wed Dec 21, 2011 8:46 am     Reply with quote

As Ttelmah said it is a legal construction in C to have local and global variables with the same name. However, I can imagine that it is easy to create an error where you by accident give two variables the same name and then it could be very difficult to trace the problem.
I don't know of any compiler having a pragma code for enabling such kind of error checks but by good programming practices it is easy to avoid these problems. The rules I use are:
1) Use as little global variables as possible. Code is easier to maintain when the scope of a variable is small and has the extra benefit that the compiler can re-use RAM.
2) Give global variables a distinctive name starting with a 'g_', for example g_myvar and g_HugeReceiveBuffer.
edmundopt



Joined: 08 Dec 2011
Posts: 15
Location: Portugal

View user's profile Send private message

PostPosted: Wed Dec 21, 2011 9:20 am     Reply with quote

Thank you, I will go for the add global to variable name, it's the best option and a smart one, allways learning Smile
Considering the PIC18f, more global variables, or more variables only mean more RAM used right, since the harvard architecture allows this, or I'am thinking wrong ?
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