I have the following line of code in my Arduino sketch:
static double *temps = new double[arraySize]; //Declare array to hold tempratures, place the array on the heap where it will not be deleted and keep the pointer as a static
//double temps[arraySize];
When I compile with the Arduino IDE on Ubuntu it compiles just fine. However, when I compile on the Raspberry Pi, it throws up the following error:
xxx.cpp.o: In function `last Function in file()':
xxx.cpp:[last line in file]: undefined referance to `operator new[] (unsigned int)'
collect2: error: ld returned 1 exit status
Is this a bug? If so, how do I report it? If not, why is it throwing this error or how else can I declare a static array of doubles (needs to be static, I calculate an average of some incoming data over several function calls)?
-
What version of arduino is the Pi running?BrettFolkins– BrettFolkins2015年02月17日 00:20:31 +00:00Commented Feb 17, 2015 at 0:20
2 Answers 2
The Arduino uses AVR libc which does not provide a complete C++ environment.
Among other things AVR libc does not support new
and delete
. See the FAQ for more information.
Some versions of the Arduino libraries may provide new and delete but they are just wrappers around malloc
and free
. I suggest not using new and delete on arduino because they do not have the same behavior as C++. For example destructors will not be called on the arduino when delete is called.
how else can I declare a static array of doubles (needs to be static, I calculate an average of some incoming data over several function calls)
When you find a bug in the Arduino software, please do report it to the appropriate place -- http://arduino.cc/en/Main/ContactUs has the contact details.
One way to declare a static array of doubles is like this:
double boxcar_average(double new_sample) {
const int arraySize = 3;
static double old_sample[ arraySize ] = {0};
double sum = new_sample;
for(int i=0; i<arraySize; i++){
sum += old_sample[i];
}
old_sample[0] = old_sample[1];
old_sample[1] = old_sample[2];
old_sample[2] = new_sample;
return( sum / 4 );
}