Is it possible to use "new" to create an object in Arduino

Hi everyone,

I wonder whether it is possible to use "new" to create an object. E.g.

Morse *morse = new Morse();

Thanks in advance!!

Yes, but not like that:

Morse *morse = new Morse;

Hi Nick, thanks a lot! I works well in 1.0.1. However, it does not work in 0022 version. Is it normal? Any remedy?

Hi Nick, thanks a lot! I works well in 1.0.1. However, it does not work in 0022 version. Is it normal? Any remedy?

In what way does it "not work"? Does it give you manchester encoding instead of morse encoding? Does it cause hundreds of machines in Iran to become infected? Does it cause a nuclear meltdown? Or does it just throw an error? If so, what is the error?

Sorry, I should specify the error.

SOS.cpp.o: In function `__static_initialization_and_destruction_0':
C:\Users\user\AppData\Local\Temp\build5016434056849990537.tmp/SOS.cpp:8: undefined reference to `operator new(unsigned int)'

One does not simply ... use "new".

lol

One makes sure one has version 1.0 of the IDE which implements the operator new.

However it doesn't implement "placement new" but that's another story.

You can add these lines:

// new
inline void * operator new (size_t size) { return malloc (size); }
// placement new
void * operator new (size_t size, void * ptr) { return ptr; }
// delete
void operator delete (void * ptr) { free (ptr); }

Is it normal?

Absolutely.

Any remedy?

See above.

Hi Nick, thanks a lot again! It works perfectly in 0022.

I am sorry that I am new to Arduino environment. May I ask whether it supports dynamic 2D array allocation?

  int (*button)[3];  
  int no=3;
  button = new int [no][3];
SOS.cpp.o: In function `setup':
C:\Users\user\AppData\Local\Temp\build5727991063192607334.tmp/SOS.cpp:26: undefined reference to `operator new[](unsigned int)

cobrabear:
I am sorry that I am new to Arduino environment.

It's just C++.

May I ask whether it supports dynamic 2D array allocation?

  button = new int [no][3];

Not the way you did it. If you have an array of 3 pointers you will have to do 3 x new.

Perhaps we should ask what you are doing? This is low-level stuff. Maybe you don't need to use new at all. You only have 2 Kb of RAM, doing lots of dynamic memory allocation will soon use it up.

new and delete are not part of avr-gcc.

http://www.nongnu.org/avr-libc/user-manual/FAQ.html

1.0+ must have added support for them, external of avr-gcc, if they are working.

Rather... They are not part of AVR-LIBC, which is absurd IMO. Arduino added the basic new operator in 1.0. Definitely they are part of avr-gcc.

They are defined in new.h and implemented in new.cpp.

new.h is included in Printable.h.

Printable.h is included in Print.h

I can't quite trace the includes all the way up to Arduino.h but it must be there somewhere.

cobrabear:

  int (*button)[3];  

int no=3;
  button = new int [no][3];

Oh one more thing. This is completely legal. The problem is that Arduino only included an implementation for ONE ‘operator new’. It does not include operator new, which is needed for that operation.

You can add the missing new operators thusly:

inline void* operator new(std::size_t, void* p) {return p; }
inline void* operator new[](std::size_t, void *p) { return p; }

Doesn't work, even if you get rid of std::

inline void* operator new(size_t, void* p) {return p; }
inline void* operator new[](size_t, void *p) { return p; }

void setup ()
{
  int (*button)[3];  
  int no=3;
  button = new int [no][3];
  
}
void loop () {}
sketch_may31b.cpp.o: In function `setup':
sketch_may31b.cpp:11: undefined reference to `operator new[](unsigned int)'

This, maybe?

void* operator new    (size_t, void* p) { return p; }
void* operator new [] (size_t, void *p) { return p; }
void* operator new [] (size_t size) { return malloc (size); }
void operator delete [] (void * p) { free (p); } 

void setup ()
{
  int (*button)[3];  
  int no=3;
  button = new int [no][3];
 
  delete [] button [no];
}
void loop () {}

Hi all, thanks a lot for all of your helps. Nick, it works well! Thanks!!!!

I love this forum! People are nice and helpful here.

Maybe you don't need to use new at all. You only have 2 Kb of RAM, doing lots of dynamic memory allocation will soon use it up.

  • new should throw a bad_alloc exception, which it does not on an Arduino.
  • Don't use new in loop()
  • Are you sure you need it in setup() ? ( In your sample code you don't )