Pages: [1] 2   Go Down
Author Topic: Is it possible to use "new" to create an object in Arduino  (Read 1876 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 5
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi everyone,

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

Morse *morse = new Morse();

Thanks in advance!!
Logged

Global Moderator
Melbourne, Australia
Online Online
Brattain Member
*****
Karma: 511
Posts: 19359
Lua rocks!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Yes, but not like that:

Code:
Morse *morse = new Morse;
Logged

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

Please post technical questions on the forum - not to me by personal message. Thanks a lot.

Offline Offline
Newbie
*
Karma: 0
Posts: 5
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

UK
Offline Offline
Faraday Member
**
Karma: 100
Posts: 4153
Where is your SSCCE?!?!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Get 10% off all 4D Systems TFT screens this month: use discount code MAJENKO10

Offline Offline
Newbie
*
Karma: 0
Posts: 5
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Sorry, I should specify the error.

Code:
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)'
Logged

Global Moderator
Melbourne, Australia
Online Online
Brattain Member
*****
Karma: 511
Posts: 19359
Lua rocks!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
// 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.

Logged

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

Please post technical questions on the forum - not to me by personal message. Thanks a lot.

Offline Offline
Newbie
*
Karma: 0
Posts: 5
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
  int (*button)[3]; 
  int no=3;
  button = new int [no][3];

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

Global Moderator
Melbourne, Australia
Online Online
Brattain Member
*****
Karma: 511
Posts: 19359
Lua rocks!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
  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.
Logged

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

Please post technical questions on the forum - not to me by personal message. Thanks a lot.

Fort Lauderdale, FL
Offline Offline
Faraday Member
**
Karma: 71
Posts: 6144
Baldengineer
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Capacitor Expert By Day, Enginerd by night.  ||  Personal Blog: www.baldengineer.com  || Electronics Tutorials for Beginners:  www.addohms.c

Seattle, WA
Offline Offline
God Member
*****
Karma: 11
Posts: 673
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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


Global Moderator
Melbourne, Australia
Online Online
Brattain Member
*****
Karma: 511
Posts: 19359
Lua rocks!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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

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

Please post technical questions on the forum - not to me by personal message. Thanks a lot.

Seattle, WA
Offline Offline
God Member
*****
Karma: 11
Posts: 673
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
  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:
inline void* operator new(std::size_t, void* p) {return p; }
inline void* operator new[](std::size_t, void *p) { return p; }
Logged


Global Moderator
Melbourne, Australia
Online Online
Brattain Member
*****
Karma: 511
Posts: 19359
Lua rocks!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Code:
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:
sketch_may31b.cpp.o: In function `setup':
sketch_may31b.cpp:11: undefined reference to `operator new[](unsigned int)'
Logged

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

Please post technical questions on the forum - not to me by personal message. Thanks a lot.

Global Moderator
Melbourne, Australia
Online Online
Brattain Member
*****
Karma: 511
Posts: 19359
Lua rocks!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

This, maybe?

Code:
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 () {}
Logged

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

Please post technical questions on the forum - not to me by personal message. Thanks a lot.

Offline Offline
Newbie
*
Karma: 0
Posts: 5
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Pages: [1] 2   Go Up
Jump to: