Pages: [1]   Go Down
Author Topic: new and delete now implemented in 1.0?  (Read 979 times)
0 Members and 1 Guest are viewing this topic.
0
Offline Offline
Newbie
*
Karma: 0
Posts: 7
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Logged

0
Offline Offline
Full Member
***
Karma: 1
Posts: 225
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

0
Offline Offline
Newbie
*
Karma: 0
Posts: 7
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

M
Logged

Global Moderator
Boston area, metrowest
Offline Offline
Brattain Member
*****
Karma: 538
Posts: 27144
Author of "Arduino for Teens". Available for Design & Build services. Now with Unlimited Eagle board sizes!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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

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.

Seattle, WA USA
Online Online
Brattain Member
*****
Karma: 617
Posts: 49454
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Global Moderator
Boston area, metrowest
Offline Offline
Brattain Member
*****
Karma: 538
Posts: 27144
Author of "Arduino for Teens". Available for Design & Build services. Now with Unlimited Eagle board sizes!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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

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.

Germany
Offline Offline
Faraday Member
**
Karma: 59
Posts: 3060
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

0
Offline Offline
Faraday Member
**
Karma: 24
Posts: 3488
20 LEDs are enough
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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 smiley-wink
Logged

Check out my experiments http://blog.blinkenlight.net

Global Moderator
Boston area, metrowest
Offline Offline
Brattain Member
*****
Karma: 538
Posts: 27144
Author of "Arduino for Teens". Available for Design & Build services. Now with Unlimited Eagle board sizes!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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

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.

Global Moderator
Online Online
Brattain Member
*****
Karma: 485
Posts: 18815
Lua rocks!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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


Global Moderator
Online Online
Brattain Member
*****
Karma: 485
Posts: 18815
Lua rocks!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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


Pages: [1]   Go Up
Jump to: