First I’d like to say that it is totally cool to be able to talk to the author of this book! Thanks for making yourself available this way. :)
You’re very welcome! For me getting feedback from readers is as much fun as writing the book!
The hardest ‘ice-breaker’ for me getting into the book (and Arduino code in general) has been that when I run across something not yet
documented, and go to the Arduino forum for help, their is no clear path to a solution. I did not know until now that the code supported
by Arduino was a rather specific subset of the much larger C++ language.
That seems to be a big problem for many people who work with the Arduino environment for the first time. I’ve tried to find a balance between very Arduinoish code and the pure C++ stuff. For me as a software developer it was very important to show that you’re able to run full-blown object-oriented C++ code on the Arduino.
During that time I did not know there were any alternative resources. Had it not been for a few very friendly folks on the Arduino forum I might
have actually quit the whole project. I was close to that point of frustration.
Yes, the Arduino folks and the people on the forum are really great! I am happy to hear that you’re still on board!
One last question about the above. When I looked through some online material last night about Objects, Classes and Functions, it
had said that creating a new object made use of the word “new”. In one post the writer even referred to the lack of the word “new”
in a code segment and said it meant there were no new objects created in that segment of his tutorial. But I am thinking from your
statement above that is one more thing different about the Arduino IDE? I don’t need to use “new” to create an object?
In languages such as C and C++ you typically create objects in two different memory areas: the stack or the heap. Objects on the stack are created automatically whenever you define a variable in your code. For example,
This creates a new int variable on the stack. For many purposes this is just fine, but one disadvantage of stack objects is that you have to know the size and amount of all objects you’re going to use in your program up-front. Programmers often say that you have to know “at compile time” what you are going to need.
If you need to create objects “at runtime”, then you have to create them on the heap. For example, you might read a configuration file that tells your program how many int variables you need. In C++ you’d allocate these int objects using the new keyword:
int *myints = new int;
This piece of code allocates five int variables and assigns a pointer to the first of them to the variable named myints. To access the first of your five int variables you’d use
myints, to access the last one you’d use
myints. As soon as you no longer need them, you have to give them back using the delete keyword:
In principle that’s all you need to know about new and delete. There are many more aspects to pointers and dynamic memory management in C++ . Fortunately on the Arduino you do not have to care about them, because the Arduino C++ dialect does not support new and delete (I explain that in appendix B.1). But if you really need dynamic memory allocation someday on the Arduino, you can still use malloc and free. These are two C functions that are similar to new and delete.