Go Down

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

Hi everyone,

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

Morse *morse = new Morse();

Thanks in advance!!

Nick Gammon

Please post technical questions on the forum, not by personal message. Thanks!

More info:
http://www.gammon.com.au/electronics

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?

majenko

Quote
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.

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

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.

Please post technical questions on the forum, not by personal message. Thanks!

More info:
http://www.gammon.com.au/electronics

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.
Please post technical questions on the forum, not by personal message. Thanks!

More info:
http://www.gammon.com.au/electronics

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


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.

Nick Gammon

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.
Please post technical questions on the forum, not by personal message. Thanks!

More info:
http://www.gammon.com.au/electronics


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

Code: [Select]

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

Nick Gammon

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

Code: [Select]
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 () {}


Code: [Select]

sketch_may31b.cpp.o: In function `setup':
sketch_may31b.cpp:11: undefined reference to `operator new[](unsigned int)'
Please post technical questions on the forum, not by personal message. Thanks!

More info:
http://www.gammon.com.au/electronics

Nick Gammon

This, maybe?

Code: [Select]
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 () {}
Please post technical questions on the forum, not by personal message. Thanks!

More info:
http://www.gammon.com.au/electronics

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.

Go Up