Go Down

Topic: Is it possible to use "new" to create an object in Arduino (Read 1 time) previous topic - next topic

Nick Gammon

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:

Code: [Select]
// 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); }


Quote
Is it normal?


Absolutely.

Quote
Any remedy?


See above.


cobrabear

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?

Code: [Select]
  int (*button)[3]; 
  int no=3;
  button = new int [no][3];


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

Nick Gammon


I am sorry that I am new to Arduino environment.


It's just C++.


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

Code: [Select]
  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.

James C4S

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.
Capacitor Expert By Day, Enginerd by night.  ||  Personal Blog: www.baldengineer.com  || Electronics Tutorials for Beginners:  www.addohms.com

maniacbug


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.

Go Up