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?
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:
Also - putting them at the "top" of the code makes it easier for maintenance and debugging...
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.
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?
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.
It's easier to send one file than several.
ZIP file?
ZIP file?
.. would introduce the extra steps of zipping and unzipping, and I'm lazy
Sub-version repo?
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.
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?
But isn't that preprocessor a part of the standard c/c++ compiler
It's part of the acr-gcc compiler, yes.
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".
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.