Knivsta,Sweden
Offline
Sr. Member
Karma: 0
Posts: 274
Low level's cool
|
 |
« on: August 03, 2010, 01:24:40 am » |
In my current project, I have a few #defines in the middle of a sketch, and it seems to work fine. (arduino-0018). But I've seen posts on the forum indicating that it might be safer to put all #defines at the top. Why is that, and what might happen otherwise?
|
|
|
|
|
Logged
|
|
|
|
|
Phoenix, Arizona USA
Offline
Faraday Member
Karma: 27
Posts: 5079
Where's the beer?
|
 |
« Reply #1 on: August 03, 2010, 01:32:45 am » |
I'm not sure it really matters, but I am willing to learn otherwise...btw, you really should use the const keyword for constants instead: http://www.arduino.cc/en/Reference/DefineAlso - putting them at the "top" of the code makes it easier for maintenance and debugging... 
|
|
|
|
|
Logged
|
|
|
|
|
Knivsta,Sweden
Offline
Sr. Member
Karma: 0
Posts: 274
Low level's cool
|
 |
« Reply #2 on: August 03, 2010, 02:39:49 am » |
you really should use the const keyword for constants I do. (promise!) The defines are for things like: #define num(arr) (sizeof(arr) / sizeof(arr[0])) and I have them in the middle of the code because they are to be used together with a class defined there.
|
|
|
|
|
Logged
|
|
|
|
|
Seattle, WA USA
Offline
Brattain Member
Karma: 312
Posts: 35483
Seattle, WA USA
|
 |
« Reply #3 on: August 03, 2010, 06:04:48 am » |
There is a bug, at least in 0017 (and not documented as fixed) in how the pre-processor scans the code for pre-process directives. It apparently stopped looking when the first executable statement was encountered. I have them in the middle of the code because they are to be used together with a class defined there. Why isn't the class defined in a separate file?
|
|
|
|
|
Logged
|
|
|
|
|
Knivsta,Sweden
Offline
Sr. Member
Karma: 0
Posts: 274
Low level's cool
|
 |
« Reply #4 on: August 04, 2010, 01:39:10 am » |
There is a bug, at least in 0017 (and not documented as fixed) in how the pre-processor scans the code You mean the pre-pre-processor which adds function prototypes? So as long as I declare functions before they are used, it is possible to have defines anywhere? Why isn't the class defined in a separate file? Because I don't have any Arduino hardware. I write the code and send it to a friend for testing. It's easier to send one file than several.
|
|
|
|
|
Logged
|
|
|
|
|
Phoenix, Arizona USA
Offline
Faraday Member
Karma: 27
Posts: 5079
Where's the beer?
|
 |
« Reply #5 on: August 04, 2010, 01:51:47 am » |
It's easier to send one file than several. ZIP file?
|
|
|
|
|
Logged
|
|
|
|
|
Knivsta,Sweden
Offline
Sr. Member
Karma: 0
Posts: 274
Low level's cool
|
 |
« Reply #6 on: August 04, 2010, 03:42:51 am » |
ZIP file? .. would introduce the extra steps of zipping and unzipping, and I'm lazy 
|
|
|
|
« Last Edit: August 04, 2010, 03:50:40 am by drhex »
|
Logged
|
|
|
|
|
UK
Offline
Sr. Member
Karma: 0
Posts: 304
Arduino rocks
|
 |
« Reply #7 on: August 04, 2010, 03:43:52 am » |
Sub-version repo? 
|
|
|
|
|
Logged
|
|
|
|
|
Seattle, WA USA
Offline
Brattain Member
Karma: 312
Posts: 35483
Seattle, WA USA
|
 |
« Reply #8 on: August 04, 2010, 06:00:41 am » |
You mean the pre-pre-processor which adds function prototypes? No, I mean the preprocessor that looks for # statements. So as long as I declare functions before they are used, it is possible to have defines anywhere? That you define function prototypes means that the pre-pre-processor does not need to. It does not mean that the IDE knows that it doesn't need to call the pre-pre-processor. I don't know what possible relationship this could have with the bug in the pre-processor, though.
|
|
|
|
|
Logged
|
|
|
|
|
Knivsta,Sweden
Offline
Sr. Member
Karma: 0
Posts: 274
Low level's cool
|
 |
« Reply #9 on: August 04, 2010, 08:40:12 am » |
No, I mean the preprocessor that looks for # statements. Alright. But isn't that preprocessor a part of the standard c/c++ compiler and thus independent of Arduino?
|
|
|
|
|
Logged
|
|
|
|
|
Seattle, WA USA
Offline
Brattain Member
Karma: 312
Posts: 35483
Seattle, WA USA
|
 |
« Reply #10 on: August 04, 2010, 10:51:46 am » |
But isn't that preprocessor a part of the standard c/c++ compiler It's part of the acr-gcc compiler, yes.
|
|
|
|
|
Logged
|
|
|
|
|
Knivsta,Sweden
Offline
Sr. Member
Karma: 0
Posts: 274
Low level's cool
|
 |
« Reply #11 on: August 04, 2010, 11:38:02 am » |
Forgive me for sounding stupid here, but... surely there is nothing avr-specific about the #define, #include and #ifdefs handled by the preprocessor. So it ought to be a part of gcc/g++. Therefore I do not understand why the bug in question is referred to as a "bug in Arduino 0017".
|
|
|
|
« Last Edit: August 04, 2010, 01:23:34 pm by drhex »
|
Logged
|
|
|
|
|
Seattle, WA USA
Offline
Brattain Member
Karma: 312
Posts: 35483
Seattle, WA USA
|
 |
« Reply #12 on: August 04, 2010, 12:20:34 pm » |
The thread I read that discussed the issue said the OP was using Arduino 0017. That is what I based my statement "at least in Arduino 0017" on. It may be more widespread than that. It may have been fixed, or not. I don't know. Or care. I put #defines at the top of the code where they belong.
If you don't, and it works for you to put them in the middle, great. If you put them in the middle, and they don't work, you know what to do about the problem.
|
|
|
|
|
Logged
|
|
|
|
|
|