new and delete now implemented in 1.0?

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:

class Test {
public:
  int param;
  
  Test(int arg) {
    param = arg;
  }
};

void setup() {
  Serial.begin(9600);
}
  
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 ");
    Serial.println(t->param);
    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?)

M

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

M

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

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.

Seems like ovekill for a 1-byte variable, no?

I don't have an unused Arduino available right now, so:

What happens if you remove the last line done = 1;

Event if the Test class were bigger, I agree: it's questionable to use dynamic memory management on such a processor/memory, intended to run in an infinite loop.

Using new without catching the memory allocation exception is a bug.

What happens if you remove the second last line delete t;

I also consider dynamic memory management somewhat critical with 2k of ram. Let's wait and see how long it takes for the first template meta program to hit the Arduino platform ;)

"template meta program" I don't even know that means! I like '1284's with 16K of SRAM tho.

PaulS:

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.

What PaulS implied but didn't state outright is that a side-effect of "new" is that the constructor of the class is called, plus the constructors of any members of the class. This makes an important difference between doing "new" and "malloc" as "malloc" does not call constructors.

Seems like ovekill for a 1-byte variable, no?

Indeed, but for larger variables you can save memory. Just as an example, if on the Mega2560 you wanted Serial1 but not Serial2 or Serial3, well the HardwareSerial library gives you all of them. Now if instead they let you do a "new" to create Serial1 if, and only if, you needed it, then the memory that it takes (eg. the buffers) are saved for perhaps more important uses ... that is, the memory is saved if you don't need it and don't call "new" on it.

firegeek: Imagine my surprise when I discovered that the following code compiles and runs on 1.0:

They didn't implement "placement new" which is a pity because some things like the STL use it.