3

I've been told (specifically in an answer to C++ Standard Library on Arduino, and in Stack Overflow question C++ string and Arduino String. How to combine them?)) that the Arduino compiler does not implement the new operator. However, I've written a program for the Arduino (in the Arduino IDE) which uses it, and it works perfectly.

void setup() {
    Serial.begin(9600);
}

void loop() {
    char* array;
    char c;
    unsigned arraySize;

    Serial.write("Enter a 1 digit number.\n");

    do {
        c = Serial.read();
    } while(c < '0' or c > '9');
    arraySize = c-'0';

    Serial.write("You wrote ");
    Serial.write(c);
    Serial.write(".\n");
    Serial.write("Now enter ");
    Serial.write(c);
    Serial.write(" lower-case letters.\n");

    array = new char[arraySize];

    for (unsigned i = 0; i < arraySize;) {
        array[i] = Serial.read();
        if (array[i] >= 'a' and array[i] <= 'z')
            i++;
    }

    Serial.write("You entered: ");

    for (unsigned i = 0; i < arraySize; i++) {
        Serial.write(array[i]);
        Serial.write(" ");
    }
    Serial.write("\n");
}

Here is a sample output to demonstrate its functionality:

Enter a 1 digit number.
You wrote 5.
Now enter 5 lower-case letters.
You entered: h e l l o
Enter a 1 digit number.
You wrote 9.
Now enter 9 lower-case letters.
You entered: w a s s u p m a n
Enter a 1 digit number.
You wrote 9.
Now enter 9 lower-case letters.
You entered: h o w y a d o i n
Enter a 1 digit number.
You wrote 4.
Now enter 4 lower-case letters.
You entered: c o o l
Enter a 1 digit number.
You wrote 7.
Now enter 7 lower-case letters.
You entered: i t w o r k s
Enter a 1 digit number.

So why do I keep hearing this? Are these people wrong, or do I simply misunderstand their meaning?

1
  • 4
    As they say in the army, if the map doesn't match the terrain, trust the terrain. Commented Apr 29, 2013 at 12:25

3 Answers 3

14

new and delete is defined as part of the Arduino distribution: /usr/share/arduino/hardware/arduino/cores/arduino/new.h:

/* Header to define new/delete operators as they aren't provided by avr-gcc by default
   Taken from http://www.avrfreaks.net/index.php?name=PNphpBB2&file=viewtopic&t=59453 
 */
#ifndef NEW_H
#define NEW_H

#include <stdlib.h>

void * operator new(size_t size);
void operator delete(void * ptr); 

__extension__ typedef int __guard __attribute__((mode (__DI__)));

extern "C" int __cxa_guard_acquire(__guard *);
extern "C" void __cxa_guard_release (__guard *);
extern "C" void __cxa_guard_abort (__guard *); 

extern "C" void __cxa_pure_virtual(void);

#endif

The new.h is included in Printable.h so you are getting it in your Arduino basic includes. These operators are not defined in AVR Libc though. My interpretation of these design choices: the Libc people thought it was a bad idea, whereas Arduino people are all about ease of use: if you want new and delete, please have them.

9

As you’ve demonstrated, new and delete do work in principle on Arduino. The problem is that your Arduino has 2K of RAM for global variables, stack, and heap combined, so any substantial program you write for it has to be highly aware of memory usage.

Therefore, for an embedded system like this, it is generally preferable to use static allocation (or possibly per-class dynamic allocation in some situations) rather than general purpose dynamic allocation: For simple programs, you don’t need dynamic allocation, and for complex programs, you can’t afford it.

0
1

Just because the platform doesn't predefine operator new() doesn't mean that you can't define it. In your code, presumably someone will have written a definition for the allocation function, so all is well.

(I assume you're familiar with the difference between the new expression and the allocation function, which is confusingly called operator new().)


Here's a baby example definition that should get a short program going:

char buf[1024];
char * cur = buf;

void * operator new(std::size_t n)
{
    char * res = cur;
    std::size_t inc = (n + 15) / 16 * 16;

    if (std::distance(cur, buf + sizeof(buf)) < inc)
        throw std::bad_alloc();

    cur += inc;

    return res;
}

void operator delete(void * p) noexcept
{
}

This will obviously run out of memory very fast.

5
  • 1
    But I didn't define it. And I'm not using any special libraries. So if operator new in all of it's forms have definitions that work, isn't that the same as new being "implemented" on the Arduino platform? What do these people mean when they say that Arduino doesn't support new or delete? Obviously they work, so what's the problem? Commented Apr 29, 2013 at 8:53
  • I love that this is called a "baby example." Commented May 29, 2016 at 6:04
  • 1
    There are two problems with this example: 1: trying to use exceptions on an embedded system, 2: assuming Arduino has a std namespace (it doesn't).
    – Pharap
    Commented Aug 20, 2017 at 7:23
  • @Pharap: 1) A free-standing implementation is still required to provide a certain subset of the standard library, which includes for example std::size_t. 2) An alternative to throwing would be to terminate, or invoke whatever non-returning handler is appropriate for your use case.
    – Kerrek SB
    Commented Aug 20, 2017 at 14:44
  • @KerrekSB Arduino provides size_t, but not std::size_t. Despite using a C++11 compiler, Arduino does not implement any of the C++ stdlib, it implements only aspects of the C stdlib and a few of its own extensions. Exception handling is available, but discouraged and there are no stdlib exception classes either. Which is to say, the reason this answer has so few votes is because it's a generic answer that doesn't take Arduino's idiosyncrasies into account.
    – Pharap
    Commented Aug 20, 2017 at 14:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.