2

我被告知(特别是在对 Arduino 上的 C++ 标准库的回答中,以及在 Stack Overflow 问题C++ 字符串和 Arduino 字符串中。如何组合它们?))Arduino 编译器没有实现new运算符。但是,我为使用它的 Arduino(在 Arduino IDE 中)编写了一个程序,它运行良好。

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");
}

以下是演示其功能的示例输出:

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.

那为什么我一直听到这个?这些人是错的,还是我只是误解了他们的意思?

4

3 回答 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.

于 2013-04-29T09:04:59.363 回答
9

正如你所展示的new,原则上在 Arduinodelete 工作。问题是您的 Arduino 有 2K 的 RAM 用于全局变量、堆栈和堆组合,因此您为它编写的任何实质性程序都必须高度了解内存使用情况。

因此,对于这样的嵌入式系统,通常最好使用静态分配(或在某些情况下可能是按类动态分配)而不是通用动态分配:对于简单的程序,您不需要动态分配,而对于复杂的程序,你买不起

于 2013-04-29T14:55:32.793 回答
1

仅仅因为平台没有预定义operator new()并不意味着不能定义它。在您的代码中,大概有人会为分配函数编写定义,所以一切都很好。

(我假设您熟悉new表达式和分配函数之间的区别,后者被混淆地称为operator new()。)


这是一个婴儿示例定义,应该可以让一个简短的程序运行:

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

这显然会很快耗尽内存。

于 2013-04-29T08:42:07.043 回答