Pages: [1]   Go Down
Author Topic: #defines in the middle of code  (Read 1715 times)
0 Members and 1 Guest are viewing this topic.
Knivsta,Sweden
Offline Offline
Sr. Member
****
Karma: 0
Posts: 274
Low level's cool
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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 Offline
Faraday Member
**
Karma: 36
Posts: 5519
Where's the beer?
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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/Define

Also - putting them at the "top" of the code makes it easier for maintenance and debugging...

 smiley
Logged

I will not respond to Arduino help PM's from random forum users; if you have such a question, start a new topic thread.

Knivsta,Sweden
Offline Offline
Sr. Member
****
Karma: 0
Posts: 274
Low level's cool
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
you really should use the const keyword for constants

I do. (promise!)  The defines are for things like:

Code:
#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 Offline
Brattain Member
*****
Karma: 549
Posts: 46107
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.

Quote
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 Offline
Sr. Member
****
Karma: 0
Posts: 274
Low level's cool
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
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?

Quote
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 Offline
Faraday Member
**
Karma: 36
Posts: 5519
Where's the beer?
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
It's easier to send one file than several.

ZIP file?
Logged

I will not respond to Arduino help PM's from random forum users; if you have such a question, start a new topic thread.

Knivsta,Sweden
Offline Offline
Sr. Member
****
Karma: 0
Posts: 274
Low level's cool
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
ZIP file?

.. would introduce the extra steps of zipping and unzipping, and I'm lazy   smiley-wink
« Last Edit: August 04, 2010, 03:50:40 am by drhex » Logged

UK
Offline Offline
Sr. Member
****
Karma: 0
Posts: 304
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Sub-version repo? smiley-wink
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 549
Posts: 46107
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
You mean the pre-pre-processor which adds function prototypes?
No, I mean the preprocessor that looks for # statements.

Quote
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 Offline
Sr. Member
****
Karma: 0
Posts: 274
Low level's cool
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
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 Offline
Brattain Member
*****
Karma: 549
Posts: 46107
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
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 Offline
Sr. Member
****
Karma: 0
Posts: 274
Low level's cool
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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 Offline
Brattain Member
*****
Karma: 549
Posts: 46107
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Pages: [1]   Go Up
Jump to: