Go Down

Topic: new and delete now implemented in 1.0? (Read 1 time) previous topic - next topic


I've read a number of posts here in which it's claimed that C++ style "new" isn't implemented for Arduino. Which makes sense to me, since dynamic memory allocation in a memory-constrained environment is fraught with danger.

But I thought I would investigate for myself what the failure mode would be if I attempted to use "new." Imagine my surprise when I discovered that the following code compiles and runs on 1.0:

Code: [Select]
class Test {
  int param;
  Test(int arg) {
    param = arg;

void setup() {
void loop()
  static int done = 0;
  Test *t = 0;
  if (done==0) {
    Serial.print("1. t is ");
    Serial.println((uint16_t)t, HEX);
    t = new Test(123);
    Serial.print("2a. t is ");
    Serial.println((uint16_t)t, HEX);
    Serial.print("2b. t->param is ");
    delete t;
    done = 1;

I don't see anything on http://arduino.cc/en/Reference/Changes indicating that "new" is now supported. When did this change? (Perhaps an even more interesting question would be why did this change?)


Professor Chaos

Yup, they are implemented in 1.0, along with pure virtual functions.  See http://arduino.cc/forum/index.php/topic,102453.0.html

I have not found and documentation of this change.


Thanks! I didn't find that post among the search results I looked at.

And agreed: "a small number of small objects", even in a small space, can be a fine thing. :)



Not being an actual programmer: what does "new" do?
Designing & building electrical circuits for over 25 years. Check out the ATMega1284P based Bobuino and other '328P & '1284P creations & offerings at  www.crossroadsfencing.com/BobuinoRev17.
Arduino for Teens available at Amazon.com.


Not being an actual programmer: what does "new" do?

Dynamically allocates memory, to hold an instance of a class, and instantiates the class in that memory location, returning a pointer to it.

Go Up