Go Down

Topic: #defines in the middle of code (Read 2303 times) previous topic - next topic

drhex

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?

cr0sh

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...

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

drhex

Quote
you really should use the const keyword for constants


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

Code: [Select]
#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.

PaulS

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?

drhex

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.

cr0sh

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


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

drhex

#6
Aug 04, 2010, 10:42 am Last Edit: Aug 04, 2010, 10:50 am by drhex Reason: 1
Quote
ZIP file?


.. would introduce the extra steps of zipping and unzipping, and I'm lazy   ;)

Eight


PaulS

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.

drhex

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?

PaulS

Quote
But isn't that preprocessor a part of the standard c/c++ compiler

It's part of the acr-gcc compiler, yes.

drhex

#11
Aug 04, 2010, 06:38 pm Last Edit: Aug 04, 2010, 08:23 pm by drhex Reason: 1
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".

PaulS

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.

Go Up