Go Down

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

firegeek

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 {
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


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.

firegeek

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

CrossRoads

Not being an actual programmer: what does "new" do?
Designing & building electrical circuits for over 25 years.  Screw Shield for Mega/Due/Uno,  Bobuino with ATMega1284P, & other '328P & '1284P creations & offerings at  my website.

PaulS

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

CrossRoads

Seems like ovekill for a 1-byte variable, no?
Designing & building electrical circuits for over 25 years.  Screw Shield for Mega/Due/Uno,  Bobuino with ATMega1284P, & other '328P & '1284P creations & offerings at  my website.

michael_x

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;

Udo Klein

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 ;)
Check out my experiments http://blog.blinkenlight.net

CrossRoads

"template meta program"
I don't even know that means!
I like '1284's with 16K of SRAM tho.
Designing & building electrical circuits for over 25 years.  Screw Shield for Mega/Due/Uno,  Bobuino with ATMega1284P, & other '328P & '1284P creations & offerings at  my website.

Nick Gammon


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

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

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

Nick Gammon


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

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

Go Up