View previous topic :: View next topic |
Author |
Message |
Ttelmah
Joined: 11 Mar 2010 Posts: 19605
|
|
Posted: Mon Oct 03, 2016 4:00 am |
|
|
Well spotted RF_Developer.
I must admit I was only looking at the original code as posted. Hadn't seen the later version. At least explains 'why'..... |
|
|
septillion
Joined: 21 Jan 2010 Posts: 13
|
|
Posted: Wed Oct 05, 2016 6:36 am |
|
|
rikotech8 wrote: |
Consider this example:
Code: | #define ADC_setUpAdc() setup_adc(ADC_CLOCK_INTERNAL);\
setup_adc_ports(AN0_TO_AN2);
if(someCondition)
ADC_setUpAdc();
|
|
Then you are right. That's one of the reasons I consider macro's as powerful but harmful and I like C++ way more. That fixes this case with the inline keyword.
But for now if you want to use a macro:
Code: |
#define ADC_setUpAdc() {setup_adc(ADC_CLOCK_INTERNAL);\
setup_adc_ports(AN0_TO_AN2);}
|
Or use the CCS C add on #inline which tries to mimic the C++ behavior.
Code: |
#inline
void ADC_setUpAdc(){
setup_adc(ADC_CLOCK_INTERNAL);
setup_adc_ports(AN0_TO_AN2);
}
|
[quote="Ttelmah"][quote="septillion"] Ttelmah wrote: | .
No. The _pass_ through the code will occur once. There will be no 'loop'.
|
Ow man, that's just nagging about the English language! Yes, the loop here is not needed because of it's static nature. No, the compiler will not generate a loop in machine code. But here in C I call a do while loop a loop which always loops at least once. If you write a for loop (not static) like
Code: |
for(i = 0; i < value; i++){
//do something
}
|
Not a loop because value might be 1 or even 0? |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19605
|
|
Posted: Wed Oct 05, 2016 8:01 am |
|
|
There is a difference between something using a variable that 'might' loop, and something that can only ever execute once. Being pedantic is sometimes vital in programming....
However the answer that really was needed was the simple one of not using a non-looping loop, but just using a code block:
Code: |
#define ADC_setUpAdc() {setup_adc(ADC_CLOCK_INTERNAL);\
setup_adc_ports(AN0_TO_AN2);}
if(someCondition)
ADC_setUpAdc();
|
This is what is in fact contained in the original 'loop', but just says treat this as one single piece of code. A useful construct inside macros in particular.
There is a similar warning here with regards to using brackets. This is why you will sometimes see numeric things in macros defined with brackets round them. Problem is that without these, after expansion a mathematical formula for example placed into another formula may not expand as originally expected, depending on the precedence of the operations that end up each side.
Important caveats.
There are others, which is why I like to use the old C 'rule' (originally suggested in K&R), or reserving 'ALL CAPITALS' for macros.
This has the advantage of warning you that a bit of code that you may think is a function, is actually a macro, and thereby alerting you to these possible problems. |
|
|
septillion
Joined: 21 Jan 2010 Posts: 13
|
|
Posted: Wed Oct 05, 2016 9:32 am |
|
|
Like the ALL_CAPS rule
And here indeed a loop was completely misplaced. But what about a piece of expandable code?
Code: |
#define NUMBER_CONNECTED 3
for(i = 0; i < NUMBER_CONNECTED; i++){
doSomethingWithNumber(i);
}
|
This can be very useful piece of code but I might change NUMBER_CONNECTED to 1 next time. This indeed renders the loop not looping and if the compiler is smart it will optimize the loop away but looking at the C code I will still call it a loop. |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19605
|
|
Posted: Thu Oct 06, 2016 1:37 am |
|
|
That's why it warns you.
Makes sure it really is what you want. Warnings are perfectly acceptable. I'll even make a remark at the start of my code, saying that a particular warning is expected on line xxx, but hasn't been suppressed, so when I next compile the code, I won't worry. |
|
|
|