Offline
Newbie
Karma: 0
Posts: 5
|
 |
« on: May 30, 2012, 05:00:13 am » |
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
Offline
Shannon Member
Karma: 226
Posts: 14101
Lua rocks!
|
 |
« Reply #1 on: May 30, 2012, 05:04:00 am » |
Yes, but not like that: Morse *morse = new Morse;
|
|
|
|
|
Logged
|
|
|
|
|
Offline
Newbie
Karma: 0
Posts: 5
|
 |
« Reply #2 on: May 30, 2012, 05:51:02 am » |
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
Edison Member
Karma: 51
Posts: 2472
What a host of balls she had seen: gaity, the brass buttons...
|
 |
« Reply #3 on: May 30, 2012, 05:56:32 am » |
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
|
|
|
|
|
Offline
Newbie
Karma: 0
Posts: 5
|
 |
« Reply #4 on: May 30, 2012, 05:59:16 am » |
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)'
|
|
|
|
|
Logged
|
|
|
|
|
Global Moderator
Melbourne, Australia
Offline
Shannon Member
Karma: 226
Posts: 14101
Lua rocks!
|
 |
« Reply #5 on: May 30, 2012, 06:03:55 am » |
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.
|
|
|
|
|
Logged
|
|
|
|
|
Offline
Newbie
Karma: 0
Posts: 5
|
 |
« Reply #6 on: May 30, 2012, 12:22:39 pm » |
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)
|
|
|
|
|
Logged
|
|
|
|
|
Global Moderator
Melbourne, Australia
Offline
Shannon Member
Karma: 226
Posts: 14101
Lua rocks!
|
 |
« Reply #7 on: May 30, 2012, 03:53:11 pm » |
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.
|
|
|
|
|
Logged
|
|
|
|
|
Austin, TX
Online
Faraday Member
Karma: 42
Posts: 5244
CMiYC
|
 |
« Reply #8 on: May 30, 2012, 10:00:02 pm » |
new and delete are not part of avr-gcc. http://www.nongnu.org/avr-libc/user-manual/FAQ.html1.0+ must have added support for them, external of avr-gcc, if they are working.
|
|
|
|
|
Logged
|
|
|
|
|
Seattle, WA
Offline
God Member
Karma: 5
Posts: 673
|
 |
« Reply #9 on: May 30, 2012, 10:07:13 pm » |
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
Offline
Shannon Member
Karma: 226
Posts: 14101
Lua rocks!
|
 |
« Reply #10 on: May 30, 2012, 10:13:56 pm » |
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
|
|
|
|
|
Seattle, WA
Offline
God Member
Karma: 5
Posts: 673
|
 |
« Reply #11 on: May 30, 2012, 10:32:05 pm » |
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; }
|
|
|
|
|
Logged
|
|
|
|
|
Global Moderator
Melbourne, Australia
Offline
Shannon Member
Karma: 226
Posts: 14101
Lua rocks!
|
 |
« Reply #12 on: May 30, 2012, 10:45:20 pm » |
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)'
|
|
|
|
|
Logged
|
|
|
|
|
Global Moderator
Melbourne, Australia
Offline
Shannon Member
Karma: 226
Posts: 14101
Lua rocks!
|
 |
« Reply #13 on: May 30, 2012, 11:05:59 pm » |
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 () {}
|
|
|
|
|
Logged
|
|
|
|
|
Offline
Newbie
Karma: 0
Posts: 5
|
 |
« Reply #14 on: May 31, 2012, 03:15:46 am » |
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
|
|
|
|
|
|