运营商对于新的Arduino [英] Operator new for Arduino

查看:172
本文介绍了运营商对于新的Arduino的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经告诉(特别是在<一个href=\"http://electronics.stackexchange.com/questions/67500/c-standard-library-on-arduino/67509?noredirect=1#67509\">an回答的 C ++标准库上的Arduino 的,并在堆栈溢出问题的 C++字符串,字符串的Arduino,如何把它们结合起来? 的))的Arduino的编译器不执行运营商。不过,我写了一个程序,它使用它的Arduino(在Arduino的IDE),它完美的作品。

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?

推荐答案

删除被定义为一部分Arduino的分布:
/usr/share/arduino/hardware/arduino/cores/arduino/new.h

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

new.h 包含在 Printable.h 让你在你的Arduino基本包括得到它。这些运营商没有在AVR libc库中定义虽然。这些设计选择我的跨pretation:libc中的人认为这是一个坏主意,而Arduino的人都是关于易用性:如果你想要删除,请他们。

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.

这篇关于运营商对于新的Arduino的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆