Take the 2-minute tour ×
Arduino Stack Exchange is a question and answer site for developers of open-source hardware and software that is compatible with Arduino. It's 100% free, no registration required.

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

share|improve this question
    
What version of arduino is the Pi running? –  BrettAM Feb 17 at 0:20

2 Answers 2

up vote 3 down vote accepted

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.

share|improve this answer

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 );
}
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.