Pages: [1]   Go Down
Author Topic: new operator is giving me trouble  (Read 544 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Jr. Member
**
Karma: 0
Posts: 69
Arduino Rocks!
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I wrote some C++ code that facilitates the use of moving average filters and can be used with Arduino to de-noise input signals. The code can be found here.

A new instance of MovingAverage is created as follows:

Code:
MovingAverage* movingAverage = new MovingAverage(5);

New data is added like this:

Code:
double measurement = analogRead(myPin)*scalingFactor;
movingAverage->add(measurement);

and the current average value is retrieved as follows:

Code:
movingAverage->getCurrentAverage();

The problem I'm facing is that the operator new fails. On the Arduino IDE, I tried the following code:

Code:
#include <new.h>
#include <MovingAverage.h>

MovingAverage * ma = new MovingAverage(10);

void setup() {
}

void loop() {
}

which throws the following error:
Code:
MovingAverage/MovingAverage.cpp.o: In function `MovingAverage::MovingAverage(unsigned short)':
.../Electronix/arduino-1.0.2/libraries/MovingAverage/MovingAverage.cpp:19: undefined reference to `operator new[](unsigned int)'

I suspected that this is because of the attempt to dynamically allocate memory in MovingAverage.cpp. I then tried the following:

Code:
double * vector = new double[10];

and I got exactly the same error message. This means that `new[]` cannot be used in Arduino. Is there some way to work this around?
Logged

An Arduino development board costs €20~60. A pack of 20 Zener diodes to protect your board from almost certain damage costs less than €1...

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 610
Posts: 49033
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

What version of the IDE are you using. Not all versions support new. What is in the new.h file you are including. The versions of the IDE that support new do not use an include file specifically to do so.
Logged

Offline Offline
Jr. Member
**
Karma: 0
Posts: 69
Arduino Rocks!
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

What version of the IDE are you using. Not all versions support new. What is in the new.h file you are including. The versions of the IDE that support new do not use an include file specifically to do so.

I'm using the Arduino IDE ver1.0.2 (2012.11.05) and my board is the Aduino Uno rev3. This new.h is a file I found in <arduino_base>/hardware/arduino/cores/arduino - it offers an implementation for new, but not for new[]. I think it is included by default in all projects anyway.
« Last Edit: November 18, 2012, 06:37:32 pm by chung » Logged

An Arduino development board costs €20~60. A pack of 20 Zener diodes to protect your board from almost certain damage costs less than €1...

Global Moderator
Offline Offline
Brattain Member
*****
Karma: 480
Posts: 18720
Lua rocks!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Add these lines to your project (eg. the main sketch):

Code:
void * operator new[] (size_t size) { return malloc(size); }
void operator delete[] (void * ptr) { free (ptr); }
Logged


Pages: [1]   Go Up
Jump to: